因爲公司須要在服務器上自動打包安卓應用和系統固件,所以須要用到genkins,下面記錄一下從安裝到使用jenkins打包一個安卓應用。安卓應用使用gradle打包,而系統固件是運行shell腳本打包。java
從https://jenkins.io/下載jenkins.war,而後放到tomcat的webapps目錄下運行便可,安裝時選擇安裝推薦的插件。安裝過程比較簡單就不列出詳細的安裝過程,在安裝插件過程當中,由於網絡問題,可能會遇到提示網絡爲offline,只需從新打開一個新的插件管理的頁面,而後將升級的接口從https改成http便可,而後重啓一下tomcat。該問題具體能夠參考:https://blog.csdn.net/txyzqc/article/details/77885367linux
Jenkins默認在當前用戶的主目錄下建立.jenkins
目錄,全部的配置文件、數據庫都存放在裏面,只須要備份這個目錄就備份了整個CI配置。構建時,生成的文件在.jenkins目錄下是workspace目錄中。android
安裝好jenkins後須要配置一下全局工具配置和全局屬性git
主要配置的是JDK,Git,Gradle板塊,分別配置這三個的路徑。web
首先在服務器上分別安裝好jdk,git,gradle,而後在全局工具配置中配置它們。shell
git是在gitlab上拉取代碼到jenkins中,因此須要把公鑰放在gitlab上,把私鑰放到jenkins中。數據庫
一、首先在服務器上生成公鑰和私鑰api
二、將公鑰id_rsa.pub部署到gitlab上的Deploy Keys中tomcat
三、將私鑰id_rsa添加到全局憑據中,類型爲SSH Username with private keybash
打包android應用時,由於android須要使用android sdk的相關接口,因此須要在服務器上先安裝androidSdk。而後在全局屬性中配置ANDROID_HOME,配置的路徑爲,系統管理->系統設置->全局屬性版塊勾選上環境變量選項
服務上安裝androidSdk,首先打開https://developer.android.com/studio/而後在Command line tools only模塊下,下載sdk-tools-linux-4333796.zip工具,這是一個androidSdk管理工具,下載好以後使用sdkmanager去下載和管理androidSdk,sdkmanager具體使用方法參考官網https://developer.android.com/studio/command-line/sdkmanager
服務器上下載好androidSdk後的目錄以下build-tools licenses platforms platform-tools,在platforms 中爲具體安裝的androidSdk的版本。
參數化構建
一、在general中選擇jdk版本
二、配置git倉庫地址,默認選擇master做爲打包分支
三、構建中,選擇Invoke Gradle script,選擇gradle版本
四、構建完成以後歸檔,併發送消息給釘釘(發送消息給釘釘,須要安裝釘釘插件)
五、構建時,能夠選擇構建的參數,好比上圖中使用了SIGN_KEY,若是下面構建的時候使用執行shell腳本的方式構建的話,則能夠直接使用,構建時選擇的參數,shell中能夠
直接使用${SIGN_KEY}獲取,構建時選擇的值。
最後附上一個執行shell打包android固件系統的shell
#!/bin/bash set -x env export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64;export PATH=${JAVA_HOME}/bin:${JAVA_HOME}/lib:${JAVA_HOME}/jre/bin:${JAVA_HOME}/jre/lib:$PATH export PATH=$PATH:/opt/gcc-arm-none-eabi-6-2017-q2-update/bin:/opt/gcc-linaro-aarch64-none-elf-4.8-2013.11_linux/bin:/opt/gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux/bin git_url="" product_name="p211" build_time="$(date +%Y%m%d.%H%M%S)" build_time1="$(date +%Y-%m-%d\ %H:%M:%S)" build_number=$product_name"_"$build_time rm -rf * git_url="ssh://git@xxxxxxxx/mingwork/mingsqure_1.git" git clone $git_url $build_number cd $build_number git checkout ${BRANCH} echo "git_url=$git_url" > ../repo_url.txt if [ "${CHECK_COMMIT}" = "TRUE" ]; then if [ "" = "$LAST_BUILD_TIME" ]; then echo "has new commit, continue..." else new_commit="$(git log --since='$LAST_BUILD_TIME' --oneline --reverse)" if [ "" = "$new_commit" ]; then echo "There is no new commit, skip this build!" echo "git_url=$git_url" > ../repo_url.txt echo "BUILD_RESULT=SUCCESS" > ../build_result.txt echo "UPLOAD_OSS_URL=no new commit, skip this build!" > ../object_url.txt exit 0 fi fi fi if [ "yes" = "${WITH_ADBD}" ]; then export WITH_ADBD=yes adb_suffix="_withadb" else export WITH_ADBD=no fi export DINGBOX_VERSION=${VERSION} if [ "${SIGN_KEY}" = "debug" ]; then signkey_branch="debug" subdir=unsigned else cd build/target/product rm -rf security signkey_branch="release" subdir=signed git clone git@xxxxxxxxxxx/android_sign_key.git -b $signkey_branch security cd - rm -rf dingtalk/prebuilts/UUID/uuid mv dingtalk/prebuilts/UUID/uuid_rel dingtalk/prebuilts/UUID/uuid rm -rf dingtalk/prebuilts/Fota/Fota.apk mv dingtalk/prebuilts/Fota/Fota_release.apk dingtalk/prebuilts/Fota/Fota.apk fi mkdir external/chromium_org/third_party/angle/.git touch external/chromium_org/third_party/angle/.git/index source build/envsetup.sh lunch p211-${BUILD_VARIANT}-32 make otapackage -j24 build_version="$(cat out/target/product/$product_name/system/build.prop | grep ^ro.dingtalk.build.version | awk -F '=' '{print $2}')" if [ -f "$WORKSPACE/$build_number/out/target/product/$product_name/aml_upgrade_package.img" ]; then ./imgout $product_name echo "adding img to zip..." cd release-$product_name zip -qry -9 ../$build_version$adb_suffix.zip . cd - echo "uploading img to oss..." /usr/bin/oss/ossupload_mingbox.sh $build_version$adb_suffix.zip ${subdir} ${BUILD_VARIANT} md5="$(md5sum $build_version$adb_suffix.zip)" echo "MD5=$md5" > ../md5 git tag -f -a $build_version -m "jenkins build tag:$build_version" git push origin $build_version echo "build success !!!" echo "BUILD_RESULT=SUCCESS" > ../build_result.txt echo "LAST_BUILD_TIME=$build_time1" > ../.last_build_time else echo "build failed !!!" echo "BUILD_RESULT=FAILED" > ../build_result.txt exit 1 fi
一、gradle4.4使用jdk1.8.0時報錯
Caused by: java.lang.VerifyError: Uninitialized object exists on backward branch 70 Exception Details: Location: com/android/build/gradle/tasks/BuildArtifactReportTask.newArtifact(Lcom/android/build/api/artifact/BuildableArtifact;)Lcom/android/build/gradle/tasks/BuildArtifactReportTask$BuildableArtifactData; @119: goto Reason: Error exists in the bytecode Bytecode: 0000000: bb00 f459 2bb9 0139 0100 c000 6b2b b901 0000010: 3d01 0001 b901 4302 0059 1301 45b8 0148 0000020: c000 374d 3a0d 3a0c 3a0b 2c4e bb00 6859 0000030: 2c10 0ab8 003d b700 69c0 006b 3a04 2db9 0000040: 0054 0100 3a05 1905 b900 5a01 0099 002d 0000050: 1905 b900 5e01 003a 0619 0419 06c0 014a 0000060: 3a07 3a0e 1907 b901 4d01 003a 0f19 0e19 0000070: 0fb9 0075 0200 57a7 ffcf 1904 c000 773a 0000080: 0e19 0b19 0c19 0d19 0eb7 0150 b0
遇到這個問題後,後來將jdk版本切換至jdk1.8.0_181便可