Project background
  • There are multiple Android projects that want to line out packages with generic pipeline scripts placed on Git
  • After successful package delivery, upload to Dandelion and push to the enterprise wechat
The premise

Java SDK, environment variables, Android version SDK, ANDROID_SDK_ROOT environment variables, gradle has been configured

Jenkins configuration
1. Create a project

2. Set parameters

Script dependencies: PRODUCT_NAME (project name), CODE_NAME (Git name), API_ENV (environment), BUILD_TYPE (type)


3. Pipeline scripts
// Dandelion API Key
def PGYER_API_KEY = "ad85*****************e1c4"
// User Key
def PGYER_U_KEY = "a172*******************eacf"
// The dandelion URL
def PGYER_URL = "https://www.pgyer.com/"
// wechat robot URL
def WEIXIN_SEND_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=508***************26f"

/** * Description: Android package public pipeline * Instructions * Mandatory fields: PRODUCT_NAME -> project name * CODE_NAME -> code Git name * API_ENV -> build environment * BUILD_TYPE -> build type */
pipeline {
    agent any

    stages {
        stage('Check environment') {
            steps {
                script {
                    opsTaskId = "${BUILD_NUMBER}_" + getTime()
                }
                echo OPS TASKID => ${opsTaskId}
            }
        }

        stage('Pulling code') {
            steps {
                echo 'Pull.. '
                sh "rm -rf /home/jenkins/doc/workspace/$JOB_NAME/* && rm -rf /home/jenkins/doc/workspace/$JOB_NAME/.git"
                git branch: 'release'.credentialsId: 'ff5******acac'.url: 'http://git.gtech.asia/application/${CODE_NAME}.git'}}// Build Android App
        stage('compilation App') {
            steps {
                echo "Build APK.."

                script {
                    sh "chmod +x gradlew"
                    sh "./gradlew clean assemble${API_ENV}${BUILD_TYPE} -PisJenkins=true -PjenkinsOpsTaskId=$opsTaskId"}}}/ / release the app
        stage('release APP') {
            steps {
                script {

                    def apkPath = sh(returnStdout: true.script: "find ${env.WORKSPACE}/app/build/outputs/apk -iname \"*${opsTaskId}.apk\"").trim()
                    echo Generate APK path :$apkPath

                    // sh "curl -F 'file=@$apkPath' -F 'uKey=$pgyerApiKey' -F '_api_key=$pgyerUKey' https://qiniu-storage.pgyer.com/apiv1/app/upload"

                    def uploadJson = sh(returnStdout: true.script: "curl -F 'file=@$apkPath' -F 'uKey=$PGYER_U_KEY' -F '_api_key=$PGYER_API_KEY' https://qiniu-storage.pgyer.com/apiv1/app/upload").trim()
                    echo "Upload dandelion return: $uploadJson"

                    def jsonObj = readJSON text: uploadJson

                    if (jsonObj.code == 0) {
                        // Upload succeeded
                        def appShortcutUrl = "$PGYER_URL" + jsonObj.data.appShortcutUrl
                        echo "Dandelion Short Link: $appShortcutUrl"

                        // Send enterprise group
                        sh "curl '$WEIXIN_SEND_URL' \\\n" +
                                " -H 'Content-Type: application/json' \\\n" +
                                " -d '\n" +
                                " {\n" +
                                " \"msgtype\": \"markdown\",\n" +
                                " \"markdown\": {\n" +
                                "\" content \ ": \" # # # # # # ${PRODUCT_NAME} [Android] ${API_ENV} environment out of the bag. \n" +
                                "> build ID: $opsTaskId\n" +
                                "> ${BUILD_TYPE}\n\n" +
                                "($appShortcutUrl) \"}\n" +
                                "}"
                    } else {
                        // Upload failed
                        currentBuild.result = "FAILURE"
                        throw new Exception("Publish APP failed: upload dandelion failed!")}}}}}}def getTime() {
    return new Date().format("yyyyMMddHHmmss", TimeZone.getTimeZone("UTC"))}Copy the code
4. Configure pipelining
  • Configuration to Jenkins

  • Configuration to Git

Android Project Configuration

Modify build.gradle configuration to link with pipeline script (only intercepting link code)

def isWindows() {
    return System.properties['os.name'].contains('windows');
}

// Jenkins
def mIsJenkins = ' '
def mJenkinsOpsTaskId = ""

android {

    mIsJenkins = project.hasProperty('isJenkins')? isJenkins :'false'
    mJenkinsOpsTaskId = project.hasProperty('jenkinsOpsTaskId')? jenkinsOpsTaskId :' '
	
    // corresponds to API_ENV
    productFlavors {
        dev {
         
        }

        test {
         
        }
        
        test_new {
          
        }
        
        uat {
          
        }
    }
    
	// Set the apK package name
    applicationVariants.all { variant ->
        def buildType = variant.buildType.name
        def productFlavor = variant.productFlavors[0].name

        variant.outputs.all {
            def appFileName = ' '
            if (mIsJenkins.toBoolean()) {
                appFileName = productFlavor + buildType + "_v" + rootProject.ext.android["versionName"] + "_" + mJenkinsOpsTaskId + '.apk'
            } else {
                appFileName = productFlavor + buildType + "_v" + rootProject.ext.android["versionName"] + '.apk'
            }
            outputFileName = appFileName
        }
    }
}
Copy the code
other
1. CenOS configure the Android environment
/ / download the SDK wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz tar XVF android - sdk_r24. 4.1 - Linux..tgz / / Configure the Android environment variable CD android-sdk-linux/ export ANDROID_SDK_ROOT=/root/ Android /android-sdk-linux export PATH=$ANDROID_SDK_ROOT/tools:$PATH export PATH=$ANDROID_SDK_ROOT/platform-tools:$PATH source /etc/profile // 1,2,3,5,6,51,53 // download and configure gradle-6.5 wget https://services.gradle.org/distributions/gradle-6.5-bin.zip unzip gradle - 6.5 - bin.zip export PATH = $PATH: / root/android/gradle - 6.5 / bin source/etc/profileCopy the code
2,Enterprise wechat robot configuration description