1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
| pipeline {
agent any
parameters {
string(name: 'EMAIL', defaultValue: 'liang@xyz.cn', description: '编译任务结束时通知的邮箱')
string(name: 'PGYER_DESCRIPTION', defaultValue: '', description: '此次打包备注(可选,默认显示分支和此次任务Build ID)')
choice(choices: ['ANDROID','IOS'], description: '编译哪个平台?', name: 'BUILD_PLATFORM')
choice(choices: ['Debug', 'Preview', 'Release'], description: '编译什么版本?(Android P版请选择Preview不要用Release!!!)', name: 'BUILD_TYPE')
string(name: 'FILE_NAME', defaultValue: 'xyz_app', description: '输出安装包的文件名(尽量英文),不能为空')
string(name: 'API_HOST', defaultValue: '', description: '接口地址,不改就留空')
string(name: 'BUILD_VERSION_NAME', defaultValue: '', description: '版本号(格式:x.y.z),不改就留空')
string(name: 'PGYER_API_KEY', defaultValue: '43aad9a3e16b23382b6350e05546db49', description: '蒲公英分发api key,不懂请保持默认')
string(name: 'PGYER_USER_KEY', defaultValue: '35f87a2f64bde6cfed825ae188e2c1f0', description: '蒲公英分发user key,不懂请保持默认')
booleanParam(name:'UPDATE_POD',defaultValue:false,description:'是否更新Pod库?')
choice(name:'POD_REPO',choices:["xyz-appdev-spec","artsy","master"],description:'具体更新哪一个Pod库')
booleanParam(name:'GRADLE_OFFLINE',defaultValue:true,description:'是否采用Gradle离线模式?')
}
environment {
LANG = "en_US.UTF-8"
//sentry cli cdn 链接
SENTRYCLI_CDNURL = 'http://cdn.npm.taobao.org/dist/sentry-cli'
//蒲公英 ipa路径
PGYER_IPA = "./build/XYZ_iOS.ipa"
//蒲公英 上传描述
PGYER_UPDATE_DESCRIPTION = "Jenkins任务号: 【${BUILD_DISPLAY_NAME}】 本次编译的分支:【${BRANCH_NAME}】 编译类型:【${params.BUILD_TYPE}】 额外备注:【${params.PGYER_DESCRIPTION}】"
//ios 输出目录
GYM_OUTPUT_DIRECTORY = "./build"
//testFlight ipa 路径
PILOT_IPA = "${PGYER_IPA}"
//testFlight 升级描述
PILOT_BETA_APP_DESCRIPTION = "${PGYER_UPDATE_DESCRIPTION}"
FL_VERSION_NUMBER_VERSION_NUMBER = "${params.BUILD_VERSION_NAME}"
}
tools {
nodejs "nodejs"
}
stages {
stage('Prepare') {
steps {
sh "mkdir build && printenv && printenv > build/env.txt"
script {
if("${params.BUILD_TYPE}" !="Debug"){
sh '''
sed -i "s#sensorsAnalytics.disablePlugin=true#sensorsAnalytics.disablePlugin=false#g" android/gradle.properties
'''
sh "cat android/gradle.properties"
}
sh '''
sed -i "s#{...getPersistenceFunctions(pageName)}##g" app/entry/App.js
'''
sh "cat app/entry/App.js"
if("${params.BUILD_VERSION_NAME}" != ""){
sh '''
sed -i "s/\\(VersionName=\\)\\([0-9]\\{1,\\}\\.[0-9]\\{1,\\}\\.[0-9]\\{1,\\}\\)/\\1${BUILD_VERSION_NAME}/g" android/gradle.properties
'''
sh "cat android/gradle.properties"
sh "cd ios && fastlane build_version"
}
if("${params.API_HOST}" != ""){
sh '''
sed -i "s#https://app\\.xyz\\.cn#${API_HOST}#g" android/app/src/main/java/com/focustech/xyz/network/configuration/Host.java
'''
sh "cat android/app/src/main/java/com/focustech/xyz/network/configuration/Host.java"
sh '''
sed -i "s#https://app\\.xyz\\.cn#${API_HOST}#g" ios/XYZ_iOS/Resources/Plists/NetEnvironment.json
'''
sh "cat ios/XYZ_iOS/Resources/Plists/NetEnvironment.json"
}
if(params.UPDATE_POD && "${params.BUILD_PLATFORM}"=='IOS'){
sh "cd ios && pod repo update ${params.POD_REPO}"
}
}
}
}
stage('Build') {
when { expression { return !params.UPDATE_POD } }
steps {
script {
sh 'npm install'
if ("${params.BUILD_PLATFORM}" == 'ANDROID') {
if(params.GRADLE_OFFLINE){
if ("${params.BUILD_TYPE}" == "Preview"){
sh "cd android && bash gradlew --build-cache --offline assembleRelease"
}else if("${params.BUILD_TYPE}" == "Release"){
sh "cd android && bash gradlew --build-cache --offline buildReinforceRelease"
}else {
sh "cd android && bash gradlew --build-cache --offline assemble${params.BUILD_TYPE}"
}
}else{
if ("${params.BUILD_TYPE}" == "Preview"){
sh "cd android && bash gradlew --build-cache assembleRelease"
}else if("${params.BUILD_TYPE}" == "Release"){
sh "cd android && bash gradlew --build-cache buildReinforceRelease"
}else {
sh "cd android && bash gradlew --build-cache assemble${params.BUILD_TYPE}"
}
}
} else if ("${params.BUILD_PLATFORM}" == 'IOS'){
sh 'cd ios && pod install --verbose'
if("${params.BUILD_TYPE}" == 'Debug'){
sh 'cd ios && fastlane build'
} else{
sh 'cd ios && fastlane build_ts'
}
}
}
}
}
stage('Deploy') {
when { expression { return !params.UPDATE_POD } }
steps {
script {
archiveArtifacts artifacts: 'build/env.txt'
if ("${params.BUILD_PLATFORM}" == 'ANDROID') {
if("${params.BUILD_TYPE}" == 'Release'){
sh "mv android/app/build/outputs/apk/release/*.apk build/${FILE_NAME}-${BUILD_TYPE}.apk"
sh "zip -r android/app/build/outputs/apk/release/${FILE_NAME}.zip android/app/build/outputs/apk/release/channels/"
sh "mv android/app/build/outputs/apk/release/${FILE_NAME}.zip build/"
archiveArtifacts artifacts: "build/*.apk"
archiveArtifacts artifacts: "build/*.zip"
}else if("${params.BUILD_TYPE}" == 'Debug') {
sh "mv android/app/build/outputs/apk/debug/*.apk build/${FILE_NAME}-${BUILD_TYPE}.apk"
archiveArtifacts artifacts: "build/${FILE_NAME}-${BUILD_TYPE}.apk"
}else if("${params.BUILD_TYPE}" == 'Preview') {
sh "mv android/app/build/outputs/apk/release/*.apk build/${FILE_NAME}-${BUILD_TYPE}.apk"
archiveArtifacts artifacts: "build/${FILE_NAME}-${BUILD_TYPE}.apk"
}
sh '''
curl -F "file=@build/${FILE_NAME}-${BUILD_TYPE}.apk" -F "userKey=${PGYER_USER_KEY}" -F "_api_key=${PGYER_API_KEY}" -F "buildInstallType=2" -F "buildPassword=123456" -F "buildUpdateDescription=${PGYER_UPDATE_DESCRIPTION}" http://www.pgyer.com/apiv2/app/upload
'''
} else if ("${params.BUILD_PLATFORM}" == 'IOS'){
archiveArtifacts artifacts: "ios/build/*.ipa"
archiveArtifacts artifacts: "ios/build/*.app.dSYM.zip"
sh "mv ios/build/*.ipa build/${FILE_NAME}-${BUILD_TYPE}.ipa"
sh '''
curl -F "file=@build/${FILE_NAME}-${BUILD_TYPE}.ipa" -F "userKey=${PGYER_USER_KEY}" -F "_api_key=${PGYER_API_KEY}" -F "buildInstallType=2" -F "buildPassword=123456" -F "buildUpdateDescription=${PGYER_UPDATE_DESCRIPTION}" http://www.pgyer.com/apiv2/app/upload
'''
}
}
}
}
}
post {
failure {
emailext attachLog: true, attachmentsPattern: 'build/**', body: "jenkins任务失败! ${PGYER_UPDATE_DESCRIPTION} 详情请点击:${BUILD_URL} 或直接从附件下载编译log自行查看!", compressLog: true, replyTo: '', subject: 'jenkins任务失败,请排查问题', to: '${EMAIL}'
}
aborted {
emailext attachLog: true, attachmentsPattern: 'build/**', body: "jenkins任务中止! ${PGYER_UPDATE_DESCRIPTION} 详情请点击:${BUILD_URL}", compressLog: true, replyTo: '', subject: 'jenkins任务已中止', to: '${EMAIL}'
}
success {
script {
if(!params.UPDATE_POD){
if ("${params.BUILD_PLATFORM}" == 'ANDROID') {
def packPath = "build/${FILE_NAME}-${BUILD_TYPE}.apk"
echo "packPath:${packPath}"
def packInfo = """${sh(returnStdout: true, script: "du -s ${packPath}")}"""
def packSize = packInfo.substring(0, packInfo.indexOf(' '))
echo "packSize:${packSize}"
def packSizeMb = packSize.toInteger() / 2 / 1000
def packSizeMbStr = "" + packSizeMb
if("${params.BUILD_TYPE}" == 'Preview'){
if(packSizeMb > 40){
emailext body: "任务${BUILD_DISPLAY_NAME}安卓Preview包大小已经超过40Mb,现已达到${packSizeMbStr}Mb!", compressLog: true, replyTo: '', subject: '任务${BUILD_DISPLAY_NAME}安卓Preview包大小已经超过40Mb!', to: 'zhangwenxin@xyz.cn;guohanlin@xyz.cn;jinwenwu@xyz.cn;jinjianxin@xyz.cn'
}
}
}else if ("${params.BUILD_PLATFORM}" == 'IOS'){
def packPath = "build/${FILE_NAME}-${BUILD_TYPE}.ipa"
echo "packPath:${packPath}"
def packInfo = """${sh(returnStdout: true, script: "du -s ${packPath}")}"""
def packSize = packInfo.substring(0, packInfo.indexOf(' '))
echo "packSize:${packSize}"
def packSizeMb = packSize.toInteger() / 2 / 1000
def packSizeMbStr = "" + packSizeMb
if("${params.BUILD_TYPE}" == 'Preview'){
if(packSizeMb > 40){
emailext body: "任务${BUILD_DISPLAY_NAME}苹果Preview包大小已经超过40Mb,现已达到${packSizeMbStr}Mb!", compressLog: true, replyTo: '', subject: '任务${BUILD_DISPLAY_NAME}苹果Preview包大小已经超过40Mb!', to: 'zhangwenxin@xyz.cn;guohanlin@xyz.cn;jinwenwu@xyz.cn;jinjianxin@xyz.cn'
}
}
}
}
}
script {
if(!params.UPDATE_POD){
if("${params.BUILD_PLATFORM}"=="IOS"){
emailext body: "jenkins任务成功! ${PGYER_UPDATE_DESCRIPTION} 详情请点击:${BUILD_URL} 或直接通过蒲公英下载! iOS下载链接 http://www.pgyer.com/xyz_ios", compressLog: true, replyTo: '', subject: 'jenkins任务成功,请下载安装', to: '${EMAIL}'
} else{
emailext body: "jenkins任务成功! ${PGYER_UPDATE_DESCRIPTION} 详情请点击:${BUILD_URL} 或直接通过蒲公英下载! Android下载链接 http://www.pgyer.com/xyz_android", compressLog: true, replyTo: '', subject: 'jenkins任务成功,请下载安装', to: '${EMAIL}'
}
}
}
}
}
}
|