要求
自動化完成Android應(yīng)用打包五督、上傳蒲公英并推送釘釘群通知任務(wù)
準(zhǔn)備
Jenkins
下載插件
Manage Jenkins --> Manage Plugins -->搜索下載
- Upload to pgyer :蒲公英上傳插件
- Groovy Postbuild :Groovy后置任務(wù)插件
添加Gradle 版本
蒲公英
獲取ApiKey
釘釘
創(chuàng)建群機(jī)器人,選擇自定義
復(fù)制webhook地址
創(chuàng)建打包任務(wù)
-
打開Jenkins祭阀,選擇New Item傻工,填入任務(wù)名稱,選擇Freestyle project
-
點(diǎn)擊Add Parameter咙冗,添加構(gòu)建參數(shù)
-
Name填入:BUILD_TYPE
-
點(diǎn)擊Git Parameter 沾歪,添加Git 參數(shù)
- Name填入:VERSION
- 點(diǎn)擊Source Code Management,選擇Git 源碼管理方式雾消,并填入項目地址和用戶名灾搏,Branches to build 填入${VERSION}
- 點(diǎn)擊Add Build Step,添加 Gradle script 打包編譯腳本
- 選擇Gradle Version立润,并填入Gradle腳本
```gradle
app:clean
app:assemble${BUILD_TYPE}
```
-
點(diǎn)擊add post-build actions狂窑,選擇 upload to pgyer with apiV2
-
填入蒲公英ApiKey,設(shè)置scandir
${WORKSPACE}/VestAppWrapper/app/build/outputs/apk/$BUILD_TYPE
-
點(diǎn)擊add post-build actions桑腮,選擇Groovy Postbuild
-
填入Groovy腳本【注意替換釘釘WebHook鏈接】
import java.text.SimpleDateFormat //構(gòu)建結(jié)果 def buildResult = manager. getResult() //構(gòu)建用戶 def buildUser= manager.getEnvVariable("BUILD_USER") //項目名稱 def jobName= manager.getEnvVariable("JOB_NAME") //構(gòu)建結(jié)果頁面 def buildUrl= manager.getEnvVariable("BUILD_URL") //GIT分支 def gitBranch= manager.getEnvVariable("GIT_BRANCH") //構(gòu)建環(huán)境 def buildEnv= manager.getEnvVariable("BUILD_ENV") //構(gòu)建類型 def buildType= manager.getEnvVariable("BUILD_TYPE") //安裝密碼 def downloadPwd= "123456" manager.listener.logger.println("項目名稱:"+ jobName) manager.listener.logger.println("構(gòu)建用戶:"+buildUser) manager.listener.logger.println("構(gòu)建分支:"+gitBranch) manager.listener.logger.println("構(gòu)建環(huán)境:"+ buildEnv) manager.listener.logger.println("構(gòu)建類型:"+ buildType) manager.listener.logger.println("構(gòu)建結(jié)果:"+buildResult) if(buildResult == "SUCCESS"){ //解析蒲公英上傳返回數(shù)據(jù) //apk下載地址 apkDownloadUrl = "https://www.pgyer.com/"+ manager.getEnvVariable("buildShortcutUrl") //apk二維碼 apkQrCode = manager.getEnvVariable("appQRCodeURL") manager.listener.logger.println("apk下載地址"+apkDownloadUrl) manager.listener.logger.println("apk二維碼地址:"+apkQrCode) dingding("標(biāo)題","### 【"+jobName+"】構(gòu)建成功" + "\n\n構(gòu)建版本:"+gitBranch+ "\n\n構(gòu)建環(huán)境:"+buildEnv+ "\n\n構(gòu)建類型:"+buildType+ "\n\n下載地址:\n[下載地址]("+apkDownloadUrl+")" + "\n\n安裝密碼:"+downloadPwd+ "\n\n![]("+apkQrCode+")"+ "\n\n構(gòu)建用戶:"+buildUser+ "\n\n構(gòu)建時間:"+getNowTime()+ "\n\n查看詳情:[項目地址]("+buildUrl+")" ) }else if(buildResult == "ABORTED"){ dingding("標(biāo)題","### 【"+jobName+"】構(gòu)建被終止" + "\n\n構(gòu)建版本:"+gitBranch+ "\n\n構(gòu)建用戶:"+buildUser+ "\n\n構(gòu)建時間:"+getNowTime()+ "\n\n查看詳情:[項目地址]("+buildUrl+")" ) }else{ dingding("標(biāo)題","### 【"+jobName+"】構(gòu)建失敗" + "\n\n構(gòu)建版本:"+gitBranch+ "\n\n構(gòu)建用戶:"+buildUser+ "\n\n構(gòu)建時間:"+getNowTime()+ "\n\n查看詳情:[項目地址]("+buildUrl+")" ) } //發(fā)送釘釘消息 def dingding(p_title,p_text){ manager.listener.logger.println("--------------------------"+p_title+p_text) def json= new groovy.json.JsonBuilder() json { msgtype "markdown" markdown { title p_title text p_text } at { atMobiles([]) isAtAll false } } manager.listener.logger.println("發(fā)送釘釘數(shù)據(jù):"+json) def connection = new URL("你的釘釘WebHook鏈接").openConnection() connection.setRequestMethod('POST') connection.doOutput = true connection.setRequestProperty('Content-Type', 'application/json') def writer = new OutputStreamWriter(connection.outputStream) writer.write(json.toString()); writer.flush() writer.close() connection.connect() def respText = connection.content.text manager.listener.logger.println("釘釘返回結(jié)果:"+respText ) } //獲取當(dāng)前時間 def getNowTime(){ def str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar lastDate = Calendar.getInstance(); str = sdf.format(lastDate.getTime()); return str; }