安卓多分支流水线自动打包
整体思路,提交代码后触发jks triggers,拉去代码,根据分支名称来区分正式,预发,测试三分支打包流程,打包完成放在nginx指定目录,同时根据各个包下载地址生成对应二维码。
这边主要为了记录jenkinsfile,step阶段如何定义全局变量。
最后效果
直接放jenkinsfile
pipeline { agent { label '192.168.1.5' } parameters { string(name:'BUILD_URL_FOR_BODY',defaultValue:"${BUILD_URL}",description:'build uri for body') } environment { ANDROID_DOWNLOAK_URL = 'http://android.xxxxxx.lan/apk/' ANDROID_QRCODE_URL = 'http://qrcode_test.xxxxxxx.com/qr_code/' } triggers{ gitlab( triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: "NameBasedFilter", includeBranchesSpec: "ALL", secretToken: "028d848ab64f7895443" ) } stages { stage('Deploy-master') { when { branch 'master' } steps { script { try{ sh ''' /home/android/gradle-6.3/bin/gradle assembleRelease if [ "`ls -A /home/android/apk/`" != "" ]; then rm -rf /home/android/apk/master/*.apk fi TEMP_NAME=`ls ./DubbingShow/build/outputs/apk/official/release/ | grep apk|awk -F".apk" '{print $1}'` mv ./DubbingShow/build/outputs/apk/official/release/$TEMP_NAME.apk /home/android/apk/master/ myqr ${ANDROID_DOWNLOAK_URL}master/$TEMP_NAME.apk -n $TEMP_NAME.png -v 7 -l L -d "/home/android/er/master/" echo -n $TEMP_NAME >tempfile ''' TEMP_ENVIRONMENT= readFile 'tempfile' }catch(err) { echo "${err}" } env['ANDROID_BUILD_NMAE']="$TEMP_ENVIRONMENT" } } post { success { sh 'sh notice.sh "Android打包成功通知" "Dubbing_show" "成功" "Master" ${BUILD_URL_FOR_BODY} ${ANDROID_BUILD_NMAE} ${ANDROID_DOWNLOAK_URL}master/${ANDROID_BUILD_NMAE}.apk ${ANDROID_QRCODE_URL}master/${ANDROID_BUILD_NMAE}.png' } failure{ sh 'sh notice.sh "Android打包失败通知" "Dubbing_show" "失败" "Master" ${BUILD_URL_FOR_BODY}' } } } stage('Deploy-release') { when { branch 'release' } steps { script { try{ sh ''' /home/android/gradle-6.3/bin/gradle assembleRelease if [ "`ls -A /home/android/apk/`" != "" ]; then rm -rf /home/android/apk/release/*.apk fi TEMP_NAME=`ls ./DubbingShow/build/outputs/apk/official/release/ | grep apk|awk -F".apk" '{print $1}'` mv ./DubbingShow/build/outputs/apk/official/release/$TEMP_NAME.apk /home/android/apk/release/ myqr ${ANDROID_DOWNLOAK_URL}release/$TEMP_NAME.apk -n $TEMP_NAME.png -v 7 -l L -d "/home/android/er/release/" echo -n $TEMP_NAME >tempfile ''' TEMP_ENVIRONMENT= readFile 'tempfile' }catch(err) { echo "${err}" } env['ANDROID_BUILD_NMAE']="$TEMP_ENVIRONMENT" } } post { success { sh 'sh notice.sh "Android打包成功通知" "Dubbing_show" "成功" "Release" ${BUILD_URL_FOR_BODY} ${ANDROID_BUILD_NMAE} ${ANDROID_DOWNLOAK_URL}release/${ANDROID_BUILD_NMAE}.apk ${ANDROID_QRCODE_URL}release/${ANDROID_BUILD_NMAE}.png' } failure{ sh 'sh notice.sh "Android打包失败通知" "Dubbing_show" "失败" "Release" ${BUILD_URL_FOR_BODY}' } } } stage('Deploy-dev') { when { branch 'dev' } steps { script { try{ sh ''' /home/android/gradle-6.3/bin/gradle assembleRelease if [ "`ls -A /home/android/apk/`" != "" ]; then rm -rf /home/android/apk/dev/*.apk fi TEMP_NAME=`ls ./DubbingShow/build/outputs/apk/official/release/ | grep apk|awk -F".apk" '{print $1}'` mv ./DubbingShow/build/outputs/apk/official/release/$TEMP_NAME.apk /home/android/apk/dev/ myqr ${ANDROID_DOWNLOAK_URL}dev/$TEMP_NAME.apk -n $TEMP_NAME.png -v 7 -l L -d "/home/android/er/dev/" echo -n $TEMP_NAME >tempfile ''' TEMP_ENVIRONMENT= readFile 'tempfile' }catch(err) { echo "${err}" } env['ANDROID_BUILD_NMAE']="$TEMP_ENVIRONMENT" } } post { success { sh 'sh notice.sh "Android打包成功通知" "Dubbing_show" "成功" "Dev" ${BUILD_URL_FOR_BODY} ${ANDROID_BUILD_NMAE} ${ANDROID_DOWNLOAK_URL}dev/${ANDROID_BUILD_NMAE}.apk ${ANDROID_QRCODE_URL}dev/${ANDROID_BUILD_NMAE}.png' } failure{ sh 'sh notice.sh "Android打包失败通知" "Dubbing_show" "失败" "Dev" ${BUILD_URL_FOR_BODY}' } } } } }
通知脚本notice.sh
gitAuthor='' gitAuthor=`git show --stat | awk '$0~/Author/{print $2}'` if [ $6 ]; then response=`curl -X POST -H 'Content-Type:application/json; charset=utf-8' \ -d '{"msgtype":"markdown",\ "markdown":{"title":"'$1'",\ "text": "## '$1'\n\n**项目名称**:'$2'\n\n**提交人**:'$gitAuthor'\n\n**状态**:'$3'\n\n**分支**:'$4'\\n\\n有关更多构建的过程、错误信息、请参照 [构建日志]('$5')\n\n点击链接进行下载['$6']('$7')\\n\\n或扫描二维码进行下载\n\n![Pandao editor.md]('$8')" }}' https://oapi.dingtalk.com/robot/send?access_token=aba71a2b612d58aad0e0xxxxxxxxxxxxxxxxxxxxxxxxxxx30f3f789a23` echo $response else response=`curl -X POST -H 'Content-Type:application/json; charset=utf-8' \ -d '{"msgtype":"markdown",\ "markdown":{"title":"'$1'",\ "text": "## '$1'\n\n**项目名称**:'$2'\n\n**提交人**:'$gitAuthor'\n\n**状态**:'$3'\n\n**分支**:'$4'\\n\\n有关更多构建的过程、错误信息、请参照 [构建日志]('$5')" }}' https://oapi.dingtalk.com/robot/send?access_token=aba71a2b612d58axxxxxxxxxxxxxxxxxxxxxxxxxxx7350b1530f3f789a23` echo $response fi