[英]How do I build from a Git repository subdirectory with Jenkins & Ant?
我將Jenkins CI配置為可與GitHub一起使用。 它成功地填充了我的$ {workspace},但是我要構建的實際項目位於存儲庫的子目錄中:
https://github.com/androidfu/CodeExamples
從Jenkins內部調用Ant表示找不到build.xml。 好的,那是因為build.xml實際上存在於$ {workspace} / DeveloperDebugPreferences中。
您可以配置構建文件以供Ant在Jenkins中使用。 如果要瀏覽到Jenkins服務器上的作業,則可以選擇“配置”,然后選擇“ Ant”目標下面的“高級”選項。
如果您希望編輯config.xml
文件,則應該看到如下的構建設置:
<builders>
<hudson.tasks.Ant>
<targets>your_target_name</targets>
<buildFile>${workspace}/DeveloperDebugPreferences/build.xml</buildFile>
</hudson.tasks.Ant>
</builders>
我想進一步介紹一下,並制作一個從提交到Git開始就開始的構建系統,該系統將簽出代碼,構建並通過SSH發布。 另外,我想要提交以文件名標記的構建版本。 第一步是使用git中的post-receive和本地安裝的Jenkins開始構建過程。 如果它是遠程的,則需要指定一個URL並使用wget或curl。 作為參考,這是我們的Git分支模型 。
# This must be read using read because the values are passed on
# STDIN not as command-line arguments.
read OLDREV NEWREV REFNAME
BRANCH=${REFNAME#refs/heads/}
. /usr/share/doc/git-core/contrib/hooks/post-receive-email
if [ ${BRANCH} == "master" ]; then
/usr/local/bin/jenkins-start "Our Project - Android - Releases"
elif [ ${BRANCH} == "develop" ]; then
/usr/local/bin/jenkins-start "Our Project - Android - Development"
fi
事物的開發方面與發布基本相同,但是我們不是對主版本而是對開發分支的提交。 由於此服務器僅托管git和Jenkins,並且Mac Mini上安裝了Eclipse和Xcode環境,因此我們將其用作構建服務器Jenkins配置為使用帶有證書的SSH進行登錄。 Jenkins服務器SSH到構建服務器,簽出代碼,構建代碼,然后將結果SSH到我們的軟件架。 唯一的特殊部分是使此工作有效的Ant build.xml部分。
在Build-Execute Shell-我們放置的命令下
~/android-sdks/tools/android update project -p "$WORKSPACE/OurProject"
在調用螞蟻下
-buildfile "$WORKSPACE/OurProject/build.xml" clean debug
在這里我們放置有關密鑰庫的信息以對二進制文件進行簽名。
key.store=ourProject.keystore
key.alias=release
key.store.password=aBigSecret
key.alias.password=aBigSecret
因此,在項目build.xml中,我們重寫了兩個目標,以使git commit哈希顯示在文件名中。 例如,OurProject 1.0.0(deadbeef)-release.apk。 構建完成后,Jenkins將文件復制到我們的軟件架中。
<target name="-set-debug-files" depends="-set-mode-check">
<exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="">
<arg value="rev-parse"/>
<arg value="--short"/>
<arg value="HEAD"/>
</exec>
<xpath input="AndroidManifest.xml"
expression="/manifest/@android:versionName"
output="android.app.version.name"
default="Unknown" />
<property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
<property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-${android.app.version.name} (${git.revision})-debug.apk" />
<property name="build.is.mode.set" value="true" />
</target>
<target name="-set-release-mode" depends="-set-mode-check">
<exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="">
<arg value="rev-parse"/>
<arg value="--short"/>
<arg value="HEAD"/>
</exec>
<xpath input="AndroidManifest.xml"
expression="/manifest/@android:versionName"
output="android.app.version.name"
default="Unknown" />
<property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />
<property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-${android.app.version.name} (${git.revision})-release.apk" />
<property name="build.is.mode.set" value="true" />
<!-- record the current build target -->
<property name="build.target" value="release" />
<property name="build.is.instrumented" value="false" />
<!-- release mode is only valid if the manifest does not explicitly
set debuggable to true. default is false. -->
<xpath input="AndroidManifest.xml" expression="/manifest/application/@android:debuggable"
output="build.is.packaging.debug" default="false"/>
<!-- signing mode: release -->
<property name="build.is.signing.debug" value="false" />
<!-- Renderscript optimization level: aggressive -->
<property name="renderscript.opt.level" value="${renderscript.release.opt.level}" />
<if condition="${build.is.packaging.debug}">
<then>
<echo>*************************************************</echo>
<echo>**** Android Manifest has debuggable=true ****</echo>
<echo>**** Doing DEBUG packaging with RELEASE keys ****</echo>
<echo>*************************************************</echo>
</then>
<else>
<!-- property only set in release mode.
Useful for if/unless attributes in target node
when using Ant before 1.8 -->
<property name="build.is.mode.release" value="true"/>
</else>
</if>
</target>
這只是一個可公開訪問的網頁,其中PHP腳本按日期順序顯示構建。
<?php
require('header.html');
?>
<h2>All builds</h2>
<table>
<?php
$dir = dirname($_SERVER[SCRIPT_FILENAME]);
$filenames = scandir($dir);
$files = array();
$fileTimes = array();
$j = 0;
$n = count($filenames);
for ($i = 0; $i < $n; $i++) {
$filename = $filenames[$i];
if ( is_file($filename) && pathinfo($filename, PATHINFO_EXTENSION) == "apk" ) {
$time = filemtime($filename);
$files[$j] = array("name"=>$filename, "time"=>$time );
$fileTimes[$j] = $time;
$j++;
}
}
array_multisort($fileTimes, SORT_DESC, $files);
$tablerow_classes = array("t0", "t1");
$current_class = 0;
$m = count($files);
for ( $i = 0; $i < $m; $i++ ) {
$name = $files[$i]["name"];
$time = date ("d/m/Y H:i:s", $files[$i]["time"]);
$class = $tablerow_classes[$current_class];
$current_class++;
if ( $current_class > 1 ) {
$current_class = 0;
}
echo "<tr class=\"$class\"><td><a href=\"$name\">$name</a></td><td>$time<br /></tr>";
}
?>
</table>
<?php
require('footer.html');
總而言之,該系統提供了一個完整的構建系統,它允許您將特定的二進制文件追溯到特定的提交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.