Mac下Jenkins Android打包 一、安裝tomcat a、下載tomcat http://tomcat.apache.org/ 下載完成後解壓到: b、啟動tomcat: c、驗證 二、安裝Jenkins a、下載 jenkins.war https://jenkins.io/index ...
Mac下Jenkins Android打包
一、安裝tomcat
a、下載tomcat
下載完成後解壓到:
/Users/xiaxveliang/Library/
b、啟動tomcat:
sudo sh startup.sh
c、驗證
二、安裝Jenkins
a、下載 jenkins.war
下載完成後,放入以下路徑:
/Users/xiaxveliang/Library/ApacheTomcat/webapps
b、打開
c、找不到initialAdminPassword
找不到
/var/root/.jenkins/secrets/initialAdminPassword
解決方案:
mac finder訪問/var/root 目錄
mac 不能打開文件夾xxx 因為您沒有許可權查看其內容
d、修改jenkins job工作目錄
// 修改後的job目錄
/Users/xiaxveliang/a_Work/Jenkins/${JOB_NAME}
e、添加git 憑據
f、丟棄原則
g、選擇構建分支
*/$BRANCH
特別 特別 特別 註意
:下載 Persistent Parameter 插件 記住上一次構建成功的參數選擇
h、定時構建
// 定時構建:工作日(周一到周五)9~18點每隔2個小時構建一次
H 9-18/2 * * 1-5
// 輪詢:每隔5分鐘檢查一次是否有代碼更新
H/5 * * * *
i、構建
build apk
shell 腳本 build
# build
/Applications/Android\ Studio.app/Contents/gradle/gradle-4.10.1/bin/gradle clean assembleRelease
shell 腳本rename apk
# rename
mv $WORKSPACE/build/outputs/apk/test-release.apk $WORKSPACE/build/outputs/apk/$JOB_NAME-$BUILD_NUMBER-$BRANCH.apk
j、歸檔 (完成)
# 歸檔文件
**/$JOB_NAME-$BUILD_NUMBER-$BRANCH.apk
到這裡,可以說jenkins Android打包已經完成。
但我們還有其他一些細節需要完善,比如說生成二維碼、發郵件等
k、生成一個指向 JOB_NAME-BUILD_NUMBER-BRANCH.apk的Html
shell 腳本截圖
shell 腳本
#create html
cd $WORKSPACE/build/outputs/apk
rm -rf $JOB_NAME.html
touch $JOB_NAME.html
cat << EOF > $JOB_NAME.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width"/>
<title>$JOB_NAME</title>
</head>
<body>
<h3><a href="http://10.236.148.225:8080/jenkins/job/$JOB_NAME/$BUILD_NUMBER/artifact/build/outputs/apk/$JOB_NAME-$BUILD_NUMBER-$BRANCH.apk">$JOB_NAME-$BUILD_NUMBER-$BRANCH</a>
</h3>
</body>
</html>
EOF
#copy html to tomcat folder
歸檔JOB_NAME.html
**/$JOB_NAME-$BUILD_NUMBER-$BRANCH.apk,**/$JOB_NAME.html
l、生成二維碼——>指向 JOB_NAME.Html
下載QRcode.jar
shell 腳本截圖
shell 腳本
# java -jar xxx/QRcode.jar url=xxx/test.apk image=qrcode.png save=xxx/xxx/
java -jar /Users/xiaxveliang/Library/Tomcat/webapps/QRCode.jar url=http://10.236.148.225:8080/jenkins/job/$JOB_NAME/$BUILD_NUMBER/artifact/build/outputs/apk/$JOB_NAME.html image=qrcode.png save=$WORKSPACE/build/outputs/apk/
歸檔qrcode.png
**/$JOB_NAME-$BUILD_NUMBER-$BRANCH.apk,**/$JOB_NAME.html,**/qrcode.png
m、郵件發送
Jenkins/系統管理/系統設置
job_name/configure
郵件模版
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>[Android內測]${ENV, var="JOB_DESCRIPTION"}</title>
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
offset="0" >
<h3>以下是Jenkins自動發送的郵件,請勿回覆!</h3>
<div>
<table width="95%" cellpadding="0" cellspacing="0" border="1"
style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
<tr>
<th><br />
<h2>構建信息</h2>
</th>
</tr>
<tr>
<td>
<ul>
<li>項目名稱 : $JOB_NAME-$BUILD_NUMBER-$BRANCH</li><br />
<li>分支 : ${BRANCH}</a></li><br />
<li>版本build號 : ${BUILD_NUMBER}</li><br />
<li><font size="3" color="black"><b><a href="http://10.236.148.225:8080/jenkins/job/${JOB_NAME}/">點擊查看構建的詳細信息</b></a></li><br />
<li>Android客戶端下載圖片$BRANCH:<img src="http://10.236.148.225:8080/jenkins/job/$JOB_NAME/$BUILD_NUMBER/artifact/build/outputs/apk/qrcode.png"/></li><br />
<li><a href="http://10.236.148.225:8080/jenkins/job/$JOB_NAME/$BUILD_NUMBER/artifact/build/outputs/apk/$JOB_NAME-$BUILD_NUMBER-$BRANCH.apk"> Android客戶端下載地址</a> </li><br />
<li #right-panel .data { line-height: 19px; white-space: initial; }>git log: ${CHANGES_SINCE_LAST_SUCCESS}</li><br />
</ul>
</td>
</tr>
</table>
</div>
</body>
</html>
n、切換 test/online 環境
# test or online
# file
FILE_DIR=$WORKSPACE/src/com/test/constants/NetConstants.java
# code test or online
Test_Str="public static boolean TEST = true"
Online_Str="public static boolean TEST = false"
# test
# sed 's/123/456/g' replace 123 to 456
if [ $ENV_TEST = "true" ];then
`sed -i.bak "s/$Online_Str/$Test_Str/g" $FILE_DIR`
elif [ $ENV_TEST = "false" ];then
`sed -i.bak "s/$Test_Str/$Online_Str/g" $FILE_DIR`
fi
# build
/Applications/Android\ Studio.app/Contents/gradle/gradle-4.10.1/bin/gradle clean assembleRelease
註:關於sed命令的使用,可參考
sed命令在MAC和Linux下的不同使用方式