Project background

There are a number of Android projects, hoping to upload the package to dandelion and push it to the enterprise wechat after the successful package out through the general pipeline script on Git.

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

// API Key def PGYER_API_KEY = "ad85*****************e1c4" // User Key def PGYER_U_KEY = "A172 * * * * * * * * * * * * * * * * * * * eacf" / / dandelion URL def PGYER_URL URL = "https://www.pgyer.com/" / / WeChat robot def WEIXIN_SEND_URL = "Https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=508 * * * * * * * * * * * * * * * 26 f" / * * * description: public pipeline * Android out packets will be optional 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 "Generate 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'}} / / compile 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"}}} steps {script {def apkPath = sh(returnStdout: true, script: "Find ${env.WORKSPACE}/app/build/outputs/ apk-iname \"*${opsTaskId}.apk\"").trim() echo" $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 "). The trim () echo "upload dandelion return: $uploadJson" def jsonObj = readJSON text: UploadJson if (jsonObj. Code = = 0) {/ / upload success def appShortcutUrl = "$PGYER_URL" + jsonObj. Data. AppShortcutUrl echo Curl '$WEIXIN_SEND_URL' \\n" + "-h 'content-type: $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" +" > Signature type: ${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, implement linkage with pipeline script (only intercept linkage 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 : ProductFlavors {dev {} test {} test_new {} uat {}} // AppK 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. CentOS 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. Configuration description of enterprise wechat robot

Work.weixin.qq.com/api/doc/900…