有多个 Android 项目,希望通过放在 Git 上的通用 pipeline 脚本实线出包 出包成功之后,上传到蒲公英并推送到企业微信。
Java SDK、环境变量,Android 相应版本SDK、ANDROID_SDK_ROOT环境变量,gradle已经配置完成
脚本依赖参数:PRODUCT_NAME(项目名称)、CODE_NAME(代码Git名称)、API_ENV(环境)、BUILD_TYPE(类型)
// 蒲公英 API Key
def PGYER_API_KEY = "ad85*****************e1c4"
// 蒲公英 User Key
def PGYER_U_KEY = "a172*******************eacf"
// 蒲公英 URL
def PGYER_URL = "https://www.pgyer.com/"
// 微信机器人URL
def WEIXIN_SEND_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=508***************26f"
/**
* 描述:Android出包公共pipeline
* 使用说明
* 必选字段:PRODUCT_NAME -> 项目名称
* CODE_NAME -> 代码Git名称
* API_ENV -> 构建环境
* BUILD_TYPE -> 构建类型
*/
pipeline {
agent any
stages {
stage('检查环境') {
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'
}
}
// 编译Android App
stage('编译 App') {
steps {
echo "Build APK.."
script {
sh "chmod +x gradlew"
sh "./gradlew clean assemble${API_ENV}${BUILD_TYPE} -PisJenkins=true -PjenkinsOpsTaskId=$opsTaskId"
}
}
}
// 发布app
stage('发布 APP') {
steps {
script {
def apkPath = sh(returnStdout: true, script: "find ${env.WORKSPACE}/app/build/outputs/apk -iname \"*${opsTaskId}.apk\"").trim()
echo "生成APK路径:$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 "上传蒲公英返回:$uploadJson"
def jsonObj = readJSON text: uploadJson
if (jsonObj.code == 0) {
// 上传成功
def appShortcutUrl = "$PGYER_URL" + jsonObj.data.appShortcutUrl
echo "蒲公英短连接:$appShortcutUrl"
// 发送企业群
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}环境出包完成。\n" +
" > 构建ID:$opsTaskId\n" +
" > 签名类型:${BUILD_TYPE}\n\n" +
"[点击下载安装]($appShortcutUrl) \"}\n" +
" }'"
} else {
// 上传失败
currentBuild.result = "FAILURE"
throw new Exception("发布APP失败:上传蒲公英失败!")
}
}
}
}
}
}
def getTime() {
return new Date().format("yyyyMMddHHmmss", TimeZone.getTimeZone("UTC"))
}
配置到 Jenkins
配置到 Git
android 项目配置 修改 build.gradle 配置,实现与 pipeline 脚本的联动(只截取联动代码)
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 : ''
// 与API_ENV相对应
productFlavors {
dev {
}
test {
}
test_new {
}
uat {
}
}
// 设置apk包名
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
}
}
}
1、CentOS 配置 android 环境
// 下载SDK
wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
tar xvf android-sdk_r24.4.1-linux.tgz
// 配置Android环境变量
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
// 下载Android各个版本SDK
android update sdk -u -a -t 1,2,3,5,6,51,53
// 下载和配置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/profile
2、企业微信机器人配置说明
扫一扫
在手机上阅读