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下的不同使用方式