YDL Android Jenkins 自动打包上传蒲公英并推送钉钉

要求

自动化完成Android应用打包、上传蒲公英并推送钉钉群通知任务

准备

Jenkins

下载插件

Manage Jenkins --> Manage Plugins -->搜索下载

  • Upload to pgyer :蒲公英上传插件
  • Groovy Postbuild :Groovy后置任务插件
添加Gradle 版本
image

蒲公英

获取ApiKey

钉钉

创建群机器人,选择自定义

image

复制webhook地址


image

创建打包任务

  • 打开Jenkins,选择New Item,填入任务名称,选择Freestyle project


    image
  • 点击Add Parameter,添加构建参数


    image
  • Name填入:BUILD_TYPE


    image
  • 点击Git Parameter ,添加Git 参数


    image
    • Name填入:VERSION
image
  • 点击Source Code Management,选择Git 源码管理方式,并填入项目地址和用户名,Branches to build 填入${VERSION}
image
  • 点击Add Build Step,添加 Gradle script 打包编译脚本
image
  • 选择Gradle Version,并填入Gradle脚本
image
```gradle
app:clean
app:assemble${BUILD_TYPE}
```
  • image
  • 点击add post-build actions,选择 upload to pgyer with apiV2


    image
    • 填入蒲公英ApiKey,设置scandir

      ${WORKSPACE}/VestAppWrapper/app/build/outputs/apk/$BUILD_TYPE
      
    • image
  • 点击add post-build actions,选择Groovy Postbuild

image
  • 填入Groovy脚本【注意替换钉钉WebHook链接】

    import java.text.SimpleDateFormat
    
    //构建结果
    def buildResult = manager. getResult()
    //构建用户
    def buildUser= manager.getEnvVariable("BUILD_USER")
    //项目名称
    def jobName= manager.getEnvVariable("JOB_NAME")
    //构建结果页面
    def buildUrl= manager.getEnvVariable("BUILD_URL")
    //GIT分支
    def gitBranch= manager.getEnvVariable("GIT_BRANCH")
    
    //构建环境
    def buildEnv= manager.getEnvVariable("BUILD_ENV")
    //构建类型
    def buildType= manager.getEnvVariable("BUILD_TYPE")
    //安装密码
    def downloadPwd= "123456"
    
    manager.listener.logger.println("项目名称:"+ jobName)
    manager.listener.logger.println("构建用户:"+buildUser)
    manager.listener.logger.println("构建分支:"+gitBranch)
    manager.listener.logger.println("构建环境:"+ buildEnv)
    manager.listener.logger.println("构建类型:"+ buildType)
    manager.listener.logger.println("构建结果:"+buildResult)
    
    if(buildResult == "SUCCESS"){
        //解析蒲公英上传返回数据
        //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("标题","### 【"+jobName+"】构建成功" +
                "\n\n构建版本:"+gitBranch+
                "\n\n构建环境:"+buildEnv+
                "\n\n构建类型:"+buildType+
                "\n\n下载地址:\n[下载地址]("+apkDownloadUrl+")" +
                "\n\n安装密码:"+downloadPwd+
                "\n\n![]("+apkQrCode+")"+
                "\n\n构建用户:"+buildUser+
                "\n\n构建时间:"+getNowTime()+
                "\n\n查看详情:[项目地址]("+buildUrl+")"
        )
    }else if(buildResult == "ABORTED"){
        dingding("标题","### 【"+jobName+"】构建被终止" +
                "\n\n构建版本:"+gitBranch+
                "\n\n构建用户:"+buildUser+
                "\n\n构建时间:"+getNowTime()+
                "\n\n查看详情:[项目地址]("+buildUrl+")"
        )
    }else{
        dingding("标题","### 【"+jobName+"】构建失败" +
                "\n\n构建版本:"+gitBranch+
                "\n\n构建用户:"+buildUser+
                "\n\n构建时间:"+getNowTime()+
                "\n\n查看详情:[项目地址]("+buildUrl+")"
        )
    }
    
    
    //发送钉钉消息
    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("发送钉钉数据:"+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("钉钉返回结果:"+respText )
    }
    //获取当前时间
    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;
    }
    

效果

image.png

你可能感兴趣的:(YDL Android Jenkins 自动打包上传蒲公英并推送钉钉)