-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
272 lines (228 loc) · 6.29 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
#!/usr/bin/env groovy
success = true
defaultNode = 'xcode-16'
sshKey = 'powerci-github-ssh-key'
prTitle = null
runwayBacklogItemId = null
githubPrDetails = null
releaseNotes = null
buildNum = null
// TODO: move all secrets out!
secrets = [
github: [
credentialsId: '62620542-b00d-4c1f-81dd-4d014369f07d',
variable: 'GITHUB_API_TOKEN'
],
runway: [
credentialsId: 'nitro-runway-api-token-tps-40',
variable: 'RUNWAY_API_TOKEN'
],
appcenter: [
credentialsId: 'appcenter-token',
variable: 'APPCENTER_API_TOKEN'
]
]
stg = [
buildiOS: 'Build iOS',
buildmacOS: 'Build macOS',
buildNum: 'Update Build Number',
checkout: 'Checkout Repo',
cleanup: 'Cleanup',
deps: 'Install Dependencies',
keychain: 'Setup Keychain',
provision: 'Provisioning Profiles',
runway: 'Create Runway Comment',
setup: 'Setup',
uploadiOS: 'Upload iOS to AppCenter',
uploadmacOS: 'Upload macOS to AppCenter'
]
node(defaultNode) {
setupEnv {
stage(stg.buildNum) {
getBuildNum()
}
stage(stg.checkout) {
checkout scm
}
stage(stg.setup) {
updateBuildNum()
jenkinsSetup()
getRunwayBacklogItemId()
getReleaseNotes()
}
stage(stg.deps) {
sh 'make dependencies'
}
stage(stg.provision) {
clearProvisioningProfiles()
downloadProvisioningProfiles()
fastlane('install_prov_profiles_ios')
fastlane('install_prov_profiles_macos')
}
stage(stg.keychain) {
setupKeychain()
}
stage(stg.buildiOS) {
fastlane("build_ios suffix:${buildSuffix()}")
}
stage(stg.buildmacOS) {
fastlane("build_macos suffix:${buildSuffix()}")
}
stage(stg.uploadiOS) {
uploadiOSToAppCenter()
}
stage(stg.uploadmacOS) {
uploadmacOSToAppCenter()
}
stage(stg.runway) {
writeRunwayComment()
}
stage(stg.cleanup) {
handleCleanup()
}
}
}
// Methods
def buildSuffix() {
isDevBuild() ? 'beta' : null
}
def jenkinsSetup() {
echo "Running Jenkins setup script..."
sh './.jenkins/jenkins-setup.sh'
}
def setupEnv(block) {
withCredentials([
string(secrets.github),
string(secrets.runway),
string(secrets.appcenter)
]) {
withEnv(['LC_ALL=en_US.UTF-8', 'LANG=en_US.UTF-8']) {
sshagent([sshKey]) {
block()
}
}
}
}
def getBuildNum() {
lock(resource: stg.buildNum) {
dir('.buildnumber') {
try {
sh 'git clone --depth 5 [email protected]:powerhome/nitro-buildnumber.git .'
buildNum = sh(returnStdout: true, script: './increment playbook-swift-version').trim().toInteger()
print "Build number: ${buildNum}"
}
finally {
deleteDir()
}
}
}
}
def updateBuildNum() {
sh "echo \"CURRENT_PROJECT_VERSION = ${buildNum}\" > ./PlaybookShowcase/Versioning.xcconfig"
}
def getRunwayBacklogItemId() {
runwayBacklogItemId = sh(script: './Tools/setup-story-details.sh', returnStdout: true).trim().replaceAll (/\"/,/\\\"/).readLines().last()
if (isDevBuild()) {
githubPrDetails = readJSON file: "./Build/pr-${env.CHANGE_ID}-details.json"
}
return runwayBacklogItemId
}
def getReleaseNotes() {
if (env.CHANGE_ID) {
releaseNotes = githubPrDetails['title']
} else {
releaseNotes = sh(script: 'git show-branch --no-name HEAD', returnStdout:true).trim().replaceAll (/\"/,/\\\"/)
}
echo "Release Notes: ${releaseNotes}"
}
def clearProvisioningProfiles() {
sh "rm -rf '~/Library/MobileDevice/Provisioning Profiles'"
sh 'rm -rf ./git_prov_profiles'
}
def downloadProvisioningProfiles() {
sh 'git clone --depth 1 [email protected]:powerhome/ios-provisioning-profiles.git ./git_prov_profiles'
}
def checkProvisioningProfiles() {
sh "cd ./git_prov_profiles && ./check-expire-and-install-provisioning-profiles.sh"
sh "cd ./git_prov_profiles/profiles_macos && ./check-expire-and-install-provisioning-profiles.sh"
}
def fastlane(String command) {
sh "asdf exec bundle exec fastlane ${command}"
}
def setupKeychain() {
withEnv([
"BUILD_NUMBER=${buildNum}",
"ROOT_DIR=./"
]) {
withCredentials([
string(credentialsId: 'ios-distribution-password', variable: 'KEY_PASSWORD'),
]) {
lock(resource: 'Nitro-iOS Keychain Search List') {
sh './.jenkins/jenkins-keychain.sh setup'
}
}
}
}
def deleteKeychain() {
withEnv([
"BUILD_NUMBER=${buildNum}",
"ROOT_DIR=./"
]) {
lock(resource: 'Nitro-iOS Keychain Search List') {
sh './.jenkins/jenkins-keychain.sh destroy'
}
}
}
def buildType() {
if (isMainBuild() == true) {
return 'production'
}
return 'beta'
}
def isMainBuild() {
return env.BRANCH_NAME == 'main'
}
def isDevBuild() {
return env.BRANCH_NAME != 'main'
}
def readyForTesting() {
def labels = githubPrDetails['labels']
return labels.find{it.name == "Ready for Testing"}
}
def uploadiOSToAppCenter() {
if (isDevBuild() && !readyForTesting()) return
def trimmedReleaseNotes = releaseNotes.trim().replaceAll (/\"/,/\\\"/)
fastlane("upload_ios suffix:${buildSuffix()} type:${buildType()} release_notes:\"${trimmedReleaseNotes}\" appcenter_token:${APPCENTER_API_TOKEN}")
}
def uploadmacOSToAppCenter() {
if (isDevBuild() && !readyForTesting()) return
def trimmedReleaseNotes = releaseNotes.trim().replaceAll (/\"/,/\\\"/)
fastlane("upload_macos suffix:${buildSuffix()} type:${buildType()} release_notes:\"${trimmedReleaseNotes}\" appcenter_token:${APPCENTER_API_TOKEN}")
}
def prTitleValid() {
def match = githubPrDetails['title'] =~ /\[PBIOS\-[0-9]+\]+/
return match.find()
}
def writeRunwayComment() {
if (isDevBuild() && !readyForTesting()) {
echo "PR is not ready for testing yet. Skipping Runway comment."
return
}
if (env.PR_USER_HANDLE in ['renovate[bot]', 'dependabot']) {
echo "Bot PR detected. Skipping Runway comment."
return
}
if (isDevBuild() && !prTitleValid()) {
echo "Invalid PR title detected. Skipping Runway comment."
return
}
fastlane("create_runway_comment build_number:${buildNum} type:${buildType()} runway_api_token:${RUNWAY_API_TOKEN} runway_backlog_item_id:${runwayBacklogItemId} github_pull_request_id:${env.CHANGE_ID}")
}
def deleteDerivedData(){
fastlane("run clear_derived_data")
}
def handleCleanup() {
try { deleteKeychain() } catch (e) { }
try { deleteDerivedData() } catch (e) { }
try { deleteDir() } catch (e) { }
}