-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
342 lines (326 loc) · 15.4 KB
/
Jenkinsfile
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env groovy
@Library(value="prospera-jenkins-libraries", changelog=false)
@Library(value="xenv-jenkins-lib", changelog=false) _
def lastCommitInfo = ""
def skippingText = ""
def commitContainsSkip = 0
def slackMessage = ""
def slackChannel = '#ios_builds'
def sourceFile = "simulator_build/Openfield.xcarchive/Products/Applications/Openfield.app"
pipeline {
agent {
label 'ios_only'
}
environment {
shouldBuild = "true"
BLUE_OCEAN_JOB_URL = "${env.HUDSON_URL}blue/organizations/jenkins/ios_build/detail/${env.JOB_BASE_NAME}/${env.BUILD_NUMBER}/pipeline"
LANG = "en_US.UTF-8"
PATH = "$PATH:/usr/local/bin:$HOME/.rbenv/bin:$HOME/.rbenv/shims:/usr/bin"
}
options {
ansiColor("xterm")
timestamps()
timeout(time: 5, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr:'10'))
disableConcurrentBuilds abortPrevious: true
}
parameters {
gitParameter branch: '', branchFilter: 'origin/production|origin/qa', defaultValue: 'origin/qa', description: 'Source branch.', name: 'BRANCH', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition'
extendedChoice bindings: '', description: 'lanes from Fastlane file', groovyClasspath: '', groovyScript: '''
import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.*;
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
Jenkins.instance,
null,
null
);
def username
def password
for (creds in jenkinsCredentials)
{
if(creds.id == "[email protected]")
{
username = 'ci-prospera' //creds.username
password = creds.password
break
}
}
response = "curl -u $username:$password https://api.bitbucket.org/2.0/repositories/prosperaag/openfield-ios/src/28391a607fe3a154b48a02898be92486e58ed363/fastlane/Fastfile?at=automated_build_qa".execute().text
List<String> lanes_list = new ArrayList<String>()
lanes_list.add('SELECT A LANE!!!')
def build_arr = response.readLines().findAll({x -> x ==~ /.*lane\\s:(?<lane>.+?)\\s.*$/ }).
collect { it.stripIndent().replaceAll(\' do\', \'\').replaceAll(\'lane :\', \'\') }
for(item in build_arr){
lanes_list.add(item)
}
return lanes_list.sort()''', multiSelectDelimiter: ',', name: 'LANE', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', visibleItemCount: 30
}
stages {
stage('Checkout') {
steps {
script {
set_shared_lib()
color_print.blue("Git checkout to ${params.BRANCH}")
checkout([
$class: 'GitSCM',
branches: [[name: params.BRANCH]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'constantinpaigin_bitbucket', url: '[email protected]:prosperaag/openfield-ios.git']]
])
}
}
}
stage('Init') {
steps {
script {
lastCommitInfo = sh(script: "git log -1", returnStdout: true).trim()
commitContainsSkip = sh(script: "git log -1 | grep '.*\\[skip ci\\].*'", returnStatus: true)
color_print.green("Lane ${env.LANE} selected")
color_print.blue('Creating empty fastlane/.env.secret file')
sh 'touch fastlane/.env.secret'
color_print.blue('Setting up rbenv')
sh '''
eval "$(rbenv init -)"
rbenv global 2.7.5
bundle update --bundler
bundle update
'''
if(commitContainsSkip == 0) {
skippingText = "Skipping commit."
env.shouldBuild = false
currentBuild.result = "NOT_BUILT"
}
slackMessage = "*${env.JOB_NAME}* *${env.GIT_BRANCH}* started. ${skippingText}\nHere is commmit info:\n ${lastCommitInfo}"
}
}
}
stage('Notify Slack') {
steps {
slackSend channel: slackChannel, color: "#2222FF", message: slackMessage
}
}
stage('Run Tests') {
when {
expression {
return env.shouldBuild != "false"
}
}
steps {
script {
try {
withCredentials([string(credentialsId: 'KEYCHAIN_USER_PASS_MAC_2_PORTS', variable: 'KEYCHAIN_USER_PASS')]) {
color_print.blue("unlocking keychain")
sh "security unlock-keychain -p ${KEYCHAIN_USER_PASS}"
}
withCredentials([
string(credentialsId: 'FASTLANE_APPLE_PASSWORD', variable: 'FASTLANE_APPLE_PASSWORD'),
string(credentialsId: 'IOS_FIREBASE_APP_ID_QA', variable: 'FIREBASE_APP_ID_QA'),
string(credentialsId: 'IOS_FIREBASE_CLI_TOKEN_QA', variable: 'FIREBASE_CLI_TOKEN_QA'),
string(credentialsId: 'IOS_FIREBASE_APP_TOKEN_QA', variable: 'FIREBASE_APP_TOKEN_QA')]) {
sh '''
eval "$(rbenv init -)"
rbenv global 2.7.5
bundle exec fastlane pod_install
bundle exec fastlane generate_r_swift
bundle exec fastlane test_app
'''
env.IOS_VERSION_NUMBER = sh(script: "eval \"\$(rbenv init -)\" && bundle e fastlane run get_version_number | grep Result | awk '{print \$3}'", returnStdout: true).replaceAll("\\.*", "").trim()
env.IOS_BUILD_NUMBER = sh(script: "eval \"\$(rbenv init -)\" && bundle e fastlane run get_build_number | grep Result | awk '{print \$3}'", returnStdout: true).replaceAll("\\.*", "").trim()
}
} catch(exc) {
currentBuild.result = "UNSTABLE"
error('Tests failed')
}
}
}
}
stage('Build and deploy QA') {
when {
allOf {
environment name: 'shouldBuild', value: 'true';
environment name: 'LANE', value: 'build_for_firebase_qa'
}
}
environment {
ENV = "qa"
}
steps {
script{
withCredentials([string(credentialsId: 'KEYCHAIN_USER_PASS_MAC_2_PORTS', variable: 'KEYCHAIN_USER_PASS')]) {
color_print.blue("unlocking keychain")
sh "security unlock-keychain -p ${KEYCHAIN_USER_PASS}"
}
withCredentials([
string(credentialsId: 'FASTLANE_APPLE_PASSWORD', variable: 'FASTLANE_APPLE_PASSWORD'),
string(credentialsId: 'IOS_FIREBASE_APP_ID_QA', variable: 'FIREBASE_APP_ID_QA'),
string(credentialsId: 'IOS_FIREBASE_CLI_TOKEN_QA', variable: 'FIREBASE_CLI_TOKEN_QA'),
string(credentialsId: 'IOS_FIREBASE_APP_TOKEN_QA', variable: 'FIREBASE_APP_TOKEN_QA')]) {
sh '''
eval "$(rbenv init -)"
rbenv global 2.7.5
bundle exec fastlane "${LANE}"
'''
}
}
}
post{
success{
script {
if (fileExists(file: sourceFile)) {
env.IOS_ARTIFACT = "Openfield-${env.IOS_VERSION_NUMBER}.${env.IOS_BUILD_NUMBER}-${env.ENV}.app.zip"
sh """
zip -vr ${IOS_ARTIFACT} simulator_build/Openfield.xcarchive/Products/Applications/Openfield.app
"""
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: "AWS_OF_AUTOMATION", accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
color_print.blue("Uploading ${env.IOS_ARTIFACT} file to S3")
sh "aws s3 cp ${env.IOS_ARTIFACT} s3://prospera-mobile-uploads/ios/${env.ENV}/"
}
}
}
}
}
}
stage('Build and deploy Staging') {
when {
allOf {
environment name: 'shouldBuild', value: 'true';
environment name: 'LANE', value: 'build_for_firebase_staging'
}
}
environment {
ENV = "staging"
}
steps {
script{
withCredentials([string(credentialsId: 'KEYCHAIN_USER_PASS_MAC_2_PORTS', variable: 'KEYCHAIN_USER_PASS')]) {
color_print.blue("unlocking keychain")
sh "security unlock-keychain -p ${KEYCHAIN_USER_PASS}"
}
withCredentials([
string(credentialsId: 'FASTLANE_APPLE_PASSWORD', variable: 'FASTLANE_APPLE_PASSWORD'),
string(credentialsId: 'IOS_FIREBASE_APP_ID_STAGING', variable: 'FIREBASE_APP_ID_STAGING'),
string(credentialsId: 'IOS_FIREBASE_CLI_TOKEN_STAGING', variable: 'FIREBASE_CLI_TOKEN_STAGING'),
string(credentialsId: 'IOS_FIREBASE_APP_TOKEN_STAGING', variable: 'FIREBASE_APP_TOKEN_STAGING')]) {
sh '''
eval "$(rbenv init -)"
rbenv global 2.7.5
bundle exec fastlane "${LANE}"
'''
}
}
}
post {
success {
script {
if (fileExists(file: sourceFile)) {
env.IOS_ARTIFACT = "Openfield-${env.IOS_VERSION_NUMBER}.${env.IOS_BUILD_NUMBER}-${env.ENV}.app.zip"
sh """
zip -vr ${IOS_ARTIFACT} simulator_build/Openfield.xcarchive/Products/Applications/Openfield.app
"""
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: "AWS_OF_AUTOMATION", accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
color_print.blue("Uploading ${env.IOS_ARTIFACT} file to S3")
sh "aws s3 cp ${env.IOS_ARTIFACT} s3://prospera-mobile-uploads/ios/${env.ENV}/"
}
}
}
}
}
}
stage('Build and deploy TestFlight PROD') {
when {
allOf {
environment name: 'shouldBuild', value: 'true';
environment name: 'LANE', value: 'build_for_testflight'
}
}
environment {
ENV = "prod"
}
steps {
script {
withCredentials([string(credentialsId: 'KEYCHAIN_USER_PASS_MAC_2_PORTS', variable: 'KEYCHAIN_USER_PASS')]) {
color_print.blue("unlocking keychain")
sh "security unlock-keychain -p ${KEYCHAIN_USER_PASS}"
}
withCredentials([string(credentialsId: 'FASTLANE_APPLE_PASSWORD', variable: 'FASTLANE_APPLE_PASSWORD')]) {
sh '''
eval "$(rbenv init -)"
rbenv global 2.7.5
bundle exec fastlane "${LANE}"
'''
}
}
}
post{
success{
script {
if (fileExists(file: sourceFile)) {
env.IOS_ARTIFACT = "Openfield-${env.IOS_VERSION_NUMBER}.${env.IOS_BUILD_NUMBER}-${env.ENV}.app.zip"
sh """
zip -vr ${IOS_ARTIFACT} simulator_build/Openfield.xcarchive/Products/Applications/Openfield.app
"""
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: "AWS_OF_AUTOMATION", accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
color_print.blue("Uploading ${env.IOS_ARTIFACT} file to S3")
sh "aws s3 cp ${env.IOS_ARTIFACT} s3://prospera-mobile-uploads/ios/${env.ENV}/"
}
}
}
}
}
}
}
post {
success {
echo "========pipeline execution succeded========"
script {
color_print.green("Build: ${env.JOB_NAME} ${BUILD_ID} success! \nLane: ${env.LANE}")
recipients = ''
channel = slackChannel
subject = ''
body = "Build: ${env.JOB_NAME} ${BUILD_ID} success! \nLane: ${env.LANE}"
send_notification.message(
recipients,
channel,
subject,
body,
'green'
)
}
}
failure{
echo "========pipeline execution failed========"
script {
color_print.red("Build: ${env.JOB_NAME} ${BUILD_ID} Failed! \nLane: ${env.LANE}")
send_notification.message(
'', //recipients
slackChannel, // channel
'', // subject
"Build: ${env.JOB_NAME} ${BUILD_ID} Failed!\nLane: ${env.LANE}", // body
'red' // message color
)
}
}
unstable{
echo "========pipeline execution unstable========"
script {
color_print.yellow("Build: ${env.JOB_NAME} ${BUILD_ID} is unstable! \nLane: ${env.LANE}")
send_notification.message(
'',
slackChannel,
'',
"*Build:* ${env.JOB_NAME} ${BUILD_ID} *Unstable!* \nLane: ${env.LANE}",
'yellow'
)
}
}
always {
sh 'printenv'
cleanWs()
}
}
}