1. 配置java环境
-
export ANDROID_HOME=/home/Software/sdk
export JAVA_HOME=/home/Software/jdk1.8.0_161
export SH_HOME=/home/Software/libs
export JENKINS_HOME=/home/jenkinsPATH=$JAVA_HOME/bin:$ANDROID_HOME:$ANDROID_HOME/platform-tools:$SH_HOME:$JENKINS_HOME:$PATH
export PATH
2. 安装Jenkins
- wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c ‘echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list‘
sudo apt-get update
sudo apt-get install jenkins
3. 配置
- Jenkins默认端口是8080,如需修改,打开/etc/default/jenkins文件,将HTTP_PORT=8080修改为你需要的端口即可
-
Set build description <a href="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILD_TIME/local_qr.jpg"><img src="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILD_TIME/local_qr.jpg" height="150" width="150"/></a><a href="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILD_TIME/pgy_qr.jpg"><img src="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILD_TIME/pgy_qr.jpg" height="150" width="150"/></a><br><a href="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$PACKAGE_NAME-$PRODUCT_FLAVORS-v$VERSION_NAME.apk">$PACKAGE_NAME-$PRODUCT_FLAVORS-v$VERSION_NAME.apk</a>
-
jenkins_shell.sh
#!/bin/sh export LANG=zh_CN.UTF-8 export LC_ALL=zh_CN.UTF-8 echo "-------------------Jenkins Execute Shell start -------------------" gradle -v # gradle tasks walle-cli-all -v # JOB VERSION PATH APK_PATH_JOB_VERSION=${JENKINS_HOME}/apks/${JOB_NAME}/$VERSION_NAME/$BUILD_TIME mkdir -p $APK_PATH_JOB_VERSION cp -r ${WORKSPACE}/app/build/outputs/channels/*.apk $APK_PATH_JOB_VERSION cp -r ${WORKSPACE}/app/build/outputs/mapping/$PRODUCT_FLAVORS/release/mapping.txt $APK_PATH_JOB_VERSION # show channel,version,param. cd $APK_PATH_JOB_VERSION walle-cli-all show * #create local qr to APK_PATH_JOB_VERSION QRCode url=http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILD_TIME/$PACKAGE_NAME-$PRODUCT_FLAVORS-v$VERSION_NAME.apk image=local_qr.jpg save=$APK_PATH_JOB_VERSION # upload to pgy if [ "$UPLOAD_PGY" = "true" ];then echo "-------------------upload to pgyer start-------------------" PGY_LOG=${WORKSPACE}/app/build/outputs/channels/pgyLog.txt # pgy User Key uKey="34c017e5db948a0f71ccd6bb42f95c41" # pgy API Key apiKey="51872344529398c9245a101e3cd50c7e" # apk path APK_PATH="${APK_PATH_JOB_VERSION}/$PACKAGE_NAME-$PRODUCT_FLAVORS-v$VERSION_NAME.apk" # upload commant if [ -f "$APK_PATH" ]; then curl -F "file=@${APK_PATH}" -F "uKey=${uKey}" -F "_api_key=${apiKey}" http://www.pgyer.com/apiv1/app/upload >${PGY_LOG} PGY_QR_URL=$(cat ${PGY_LOG}|awk -F‘"‘ ‘{print $(NF-1)}‘|sed ‘s#\\##g‘) #download qr to APK_PATH_JOB_VERSION wget $PGY_QR_URL -O $APK_PATH_JOB_VERSION/pgy_qr.jpg fi echo "-------------------upload to pgyer end-------------------" fi echo "-------------------Jenkins Execute Shell end -------------------"
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title> </head> <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0"> <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif"> <tr> <td>(本邮件由程序自动下发,请勿回复!)</td> </tr> <tr> <td> <h2><font color="#FF0000">构建结果 - ${BUILD_STATUS}</font></h2> </td> </tr> <tr> <td><br /> <b><font color="#0B610B">构建信息</font></b> <hr size="2" width="100%" align="center" /> </td> </tr> <tr> <td> <ul> <li>项目名称:${PROJECT_NAME}</li> <li>GIT路径:${GIT_URL}</li> <li>构建编号:${BUILD_NUMBER}</li> <li>GIT版本:${GIT_REVISION}</li> <li>触发原因:${CAUSE}</li> <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li> <li><img src="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILDE_TIME/local_qr.jpg" height="150" width="150"/>     <img src="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILDE_TIME/pgy_qr.jpg" height="150" width="150"/></li> <li>下载地址:<a href="http://192.168.190.66/${JOB_NAME}/$VERSION_NAME/$BUILDE_TIME/$PACKAGE_NAME-$PRODUCT_FLAVORS-v$VERSION_NAME.apk">$PACKAGE_NAME-$PRODUCT_FLAVORS-v$VERSION_NAME.apk</a></li> </ul> </td> </tr> <tr> <td> <b><font color="#0B610B">变更信息:</font></b> <hr size="2" width="100%" align="center" /> </td> </tr> <tr> <td> <ul> <li>上次构建成功后变化 : ${CHANGES_SINCE_LAST_SUCCESS}</a></li> </ul> </td> </tr> <tr> <td> <ul> <li>上次构建不稳定后变化 : ${CHANGES_SINCE_LAST_UNSTABLE}</a></li> </ul> </td> </tr> <tr> <td> <ul> <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li> </ul> </td> </tr> <tr> <td> <ul> <li>变更集:${JELLY_SCRIPT,template="html"}</a></li> </ul> </td> </tr> <!-- <tr> <td> <b><font color="#0B610B">Failed Test Results</font></b> <hr size="2" width="100%" align="center" /> </td> </tr> <tr> <td> <pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre> <br /> </td>appQRCodeURL:(.),(.*) </tr> <tr> <td> <b><font color="#0B610B">构建日志 (最后 100行):</font></b> <hr size="2" width="100%" align="center" /> </td> </tr>--> <!-- <tr> <td>Test Logs (if test has ran): <a href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a> <br /> <br /> </td> </tr> --> <!-- <tr> <td> <textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=100,escapeHtml=true}</textarea> </td> </tr>--> <hr size="2" width="100%" align="center" /> </table> </body> </html>
QRCode.jar 链接:https://pan.baidu.com/s/1Wo6TbxmWJufOpqX1iso6jA 提取码:wxd7 QRCode.jar使其放入环境变量成为可执行问题 链接:https://pan.baidu.com/s/1R0GKbjTLqqJO-uUI49hMpA 提取码:4j9r
4. 更改jenkins默认目录
- sudo mv /etc/init.d/jenkins/ /home/jenkins/
- 修改/etc/passwd中的jenkins jenkins:x:124:128:Jenkins,,,:/var/lib/jenkins:/bin/bash ---> jenkins:x:124:128:Jenkins,,,:/home/jenkins:/bin/bash
5. 启动Jenkins
- sudo service jenkins start
安装配置nginx
为了使得外部能下载相关资源,安装并配置本地目录映射
sudo apt-get install nginx sudo service nginx start sudo vi /etc/nginx/sites-available/default root /home/xx; location / { autoindex on; autoindex_exact_size on; autoindex_localtime on; }
重启Nginx
sudo service nginx reload
Jenkins卸载方法
//服务 sudo apt-get remove jenkins //安装包,注意这里如果不是ubuntu那就yum sudo apt-get remove --auto-remove jenkins //配置和数据 sudo apt-get purge jenkins sudo apt-get purge --auto-remove jenkins
Jenkins权限问题
- 将jenkins账户加入到root组
sudo gpasswd -a root jenkins
- 修改 /etc/default/jenkins 内容
# user and group to be invoked as (default to jenkins) JENKINS_USER=root JENKINS_GROUP=root
- 重启jenkins
sudo service jenkins restart
- 查看是否已经加入到root组
cat /etc/group |grep jenkins jenkins:x:127:root