大飞

大飞 关注TA

挑战一切!

大飞

大飞

关注TA

挑战一切!

  •  世界的顶端
  • 混口饭吃
  • 写了333,609字

该文章投稿至Nemo社区   Android  板块 复制链接


Jenkins Android打包配置pipeline

发布于 2019/09/06 15:48 24,166浏览 0回复 5,695


// 参数定义
// 产品名称
def product = "产品名称"
// 发布根目录
def distRootDir = ""
// 构建完成日期
def buildDate = ""
// 构建完成时间
def buildTime = ""
// 编译devOpsTaskId
def devOpsTaskId = "nil"
// app配置库地址
def appRepository = "项目地址"
// common配置库地址
def commonRepository = "项目地址"
// app 签出目录
def appCheckoutTarget = "签出目录"
// common签出目录
def commonCheckoutTarget = "签出目录"

pipeline {
//在可用的节点运行
agent any
options {
// 当前任务禁用编译
disableConcurrentBuilds()
skipDefaultCheckout()
timeout(time: 100, unit: 'MINUTES')
timestamps()
}

// 自定义参数
parameters {
// 以下选择为分支选择,依赖git parameter的jenkins插件
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'APP_BRANCH', type: 'PT_BRANCH_TAG', description: '选择Android App分支', useRepository: "$appRepository"
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'COMMON_BRANCH', type: 'PT_BRANCH_TAG', description: '选择Android 公共库分支', useRepository: "$commonRepository"
// 定义编译环境
choice(choices: ['Debug', 'Beta', 'Release'], description: '选择打包环境', name: 'BUILD_TYPE')
// 定义备注
string(defaultValue: "", description: '描述打包信息', name: 'MEMO')
// 钉钉webhook accessToken
string(defaultValue: "", description: '钉钉accessToken', name: 'DINGTALK_ACCESS_TOKEN')

// 以下选择根据app(build.gradle)进行配置
// 定义时请按照gradle task命名规则,首字母需要全部大写
// 定义编译类型,默认为Pos,如果有新增,请使用choiceParameters
// string(defaultValue: "", description: 'ProductFlavor Type', name: 'PRODUCT_FLAVOR')

// 定义渠道
// choice(choices: [''], description: 'Choose ProductFlavor(Channel)', name: 'SUB_PRODUCT_FLAVOR')

}

stages {
// 此场景仅仅用于显示系统变量
stage('检查环境') {
steps {
script {
devOpsTaskId = "${JOB_NAME}-${BUILD_NUMBER}-" + new Date().getTime()
}
echo "系统变量检查: PATH=$PATH"
echo "Android SDK: ANDROID_HOME=$ANDROID_HOME"
echo "GitSCM: CREDENTIALS_ID=$APP_CREDENTIALS_ID"
echo "生成本次DEVOPS TASKID => ${devOpsTaskId}"
}
}

//拉取Android代码,因为需要将webapp的assets拷贝到apk的assets中,所以需要提前签出
stage('检出代码') {
steps {
// 签出公共库代码
echo "checkout new-fubei-common branch:${params.COMMON_BRANCH}"
checkout([$class: 'GitSCM', branches: [[name: "${params.COMMON_BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "$commonCheckoutTarget"], [$class: 'CheckoutOption', timeout: 100], [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false, timeout: 100]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${APP_CREDENTIALS_ID}", url: "$commonRepository"]]])

// 签出app代码
echo "checkout fshows-frog branch:${params.APP_BRANCH}"
checkout([$class: 'GitSCM', branches: [[name: "${params.APP_BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "$appCheckoutTarget"], [$class: 'CheckoutOption', timeout: 100], [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false, timeout: 100]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${APP_CREDENTIALS_ID}", url: "$appRepository"]]])
}
}

// 编译Android App
stage('编译 App') {
steps {
script {
buildTimeStamp = new Date().getTime()
}

// 切换工作路径
dir("${workspace}/${appCheckoutTarget}") {
// sh "echo gradle clean assemble${params.PRODUCT_FLAVOR}${params.SUB_PRODUCT_FLAVOR}${params.BUILD_TYPE}"
// 因为App将gradle wrapper加入到了.gitignore中,
script {
if (fileExists('gradlew')) {
sh "chmod +x gradlew"
sh "./gradlew clean assemble${params.BUILD_TYPE} -PdevOpsTaskId=$devOpsTaskId"
} else {
sh "gradle clean assemble${params.BUILD_TYPE} -PdevOpsTaskId=$devOpsTaskId"
}
}

}
}
}

// 发布app
// 可以在此步对app进行发布
stage('发布APP') {
steps {
script {
//创建发布目录(请注意权限)
distRootDir = "${env.DISTRIBUTION_ROOT_DIR}"
date = sh(returnStdout: true, script: "echo `date '+%Y-%m-%d'`").trim()
time = sh(returnStdout: true, script: "echo `date '+%H%M%S'`").trim()

// 临时目录
def subDir = "$product/$date/$params.BUILD_TYPE"
def dir0 = "$distRootDir/$subDir"
echo "临时目录:$dir0"
// 创建最终发布目录
sh "mkdir -p $dir0"
// 将编译完成的文件复制至目标文件夹中
def apkPath = sh(returnStdout: true, script: "find ${workspace}/${appCheckoutTarget}/facepay/build/outputs/apk -iname \"*${devOpsTaskId}.apk\"").trim()
echo "apkPath:$apkPath"
sh "cp $apkPath $dir0"
// 生成apkurl
def apkUrl = "${env.DISTRIBUTION_ROOT_URL}/$subDir/${devOpsTaskId}.apk"
echo "$apkUrl"

// 复制完成,发送钉钉通知
if (params.DINGTALK_ACCESS_TOKEN != "") {
dingTalk accessToken: params.DINGTALK_ACCESS_TOKEN, imageUrl: '', jenkinsUrl: "$apkUrl?", message: " 【${params.MEMO}${params.BUILD_TYPE}】构建完成,点击下载", notifyPeople: ''
}
}
}
}
}
}

效果图如下:


本文标签
 {{tag}}
点了个评