-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rb
executable file
·344 lines (293 loc) · 12.4 KB
/
build.rb
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
343
344
#!/usr/bin/ruby
# written by chris elliott on 2/25/2013
# dependancies:
# sudo gem install tlsmail
# sudo gem install tmail
# sudo gem install plist
# sudo gem install json
# requirements
require 'rubygems'
require 'plist'
require File.join(File.dirname(__FILE__), 'gmail_handler.rb')
# build script varibles
emailHandler=GMailHandler.new
buildStartedTime=Time.now
# hardcoded varibles
prefabPath = File.join(File.dirname(__FILE__), "assets/manifest_prefab.plist")
codeSigningIdentity="iPhone Distribution: Example Company"
archivesPath="/Users/Shared/Jenkins/archives/"
installDiskRootPath="/Library/Server/Web/Data/Sites/Default/install"
failureEmail="failure@example.com"
# pulled from jenkins
home = File.expand_path("..",Dir.pwd)
lastSuccessfulBuild="#{home}/lastSuccessfulBuild"
workspace=ENV['WORKSPACE']
buildBranch="#{ENV['buildBranch']}"
projectName="#{ENV['JOB_NAME']}"
if projectName.include? '-'
projName=projectName.slice(0..(projectName.index('-')))[0..-2]
else
projName = projectName
end
hockeykitVersion=buildBranch.sub( "origin/", "" )
hockeykitVersionCap=hockeykitVersion.to_s
hockeykitVersionCap[0] = hockeykitVersionCap.first.capitalize[0]
xcodeDir="#{projName}"
scheme="#{projName}"
target="#{projName}"
jobName="#{projName}"
xcworkspace="#{projName}"
configuration=ENV['configuration']
buildNumber=ENV['BUILD_NUMBER']
successEmail="#{ENV['successEmail']}".delete(' ')
jobUrl=ENV['JOB_URL']
lastSuccessBuildTimestamp = "http://build.example.com/jenkins/job/#{projectName}/lastStableBuild/buildTimestamp"
outputUrl="#{ENV['BUILD_URL']}/console"
infoPlistPath="#{workspace}/#{xcodeDir}/#{xcodeDir}/#{xcodeDir}-Info.plist"
displayName="#{projName}"
if ENV['displayName'].to_s.strip.length > 0
displayName=ENV['displayName']
end
ipaPath="/notExist"
provProfile="#{ENV['provisioningProfile']}.mobileprovision"
provProfilePath="/Library/MobileDevice/ProvisioningProfiles/#{provProfile}"
provProfile="#{ENV['provisioningProfile']}.mobileprovision"
bundleIdentifierRelease="#{ENV['bundleIdentifierRelease']}"
# gets commit messages since last sucessful build
lastSuccessfulBuildTimeStamp=''
if File.exist?(lastSuccessfulBuild)
f = File.open(lastSuccessfulBuild, "r")
f.each_line do |line|
lastSuccessfulBuildTimeStamp += line
end
f.close
end
lastSuccessCommitLog=`git log --pretty="- %s (%an: %h)" --max-parents=1 --since="#{lastSuccessfulBuildTimeStamp}"`
if lastSuccessCommitLog.to_s.strip.length == 0
lastSuccessCommitLog = "no new commits to display"
end
# if a step fails, it switches error to 1 and sets a brief message. all steps require
# error==0 to run except for send failure email.
error=0
# searches the root folder of the repo to find the .xcodeproj path
# and sets the xcodeproj name, scheme and target
xcodeprojPath=`find #{workspace}/#{xcodeDir} -name *.xcodeproj -maxdepth 1`
xcodeprojPath=xcodeprojPath.gsub(/\s+/, "")
if File.exist?(xcodeprojPath)
xcodeproj=File.basename(xcodeprojPath)
else
message="xcode project cannot be found"
error=1
end
# reads info from info.plist and injects build number and display name
if error==0
if File.exist?(infoPlistPath)
# reads info.plist
plistHash=Plist::parse_xml("#{infoPlistPath}")
# sets varibles from info.plist
bundleVersion=plistHash['CFBundleVersion']
bundleVersion="#{bundleVersion}.#{buildNumber}"
bundleIdentifier=plistHash['CFBundleIdentifier'].to_s
bundleShortVersionString=plistHash['CFBundleShortVersionString'].to_s
bundleVersion="#{bundleShortVersionString}.#{buildNumber}"
bundleVersion=bundleVersion.gsub(" ","")
# injects build number and display name into info.plist
plistHash = Plist::parse_xml("#{infoPlistPath}")
plistHash['CFBundleVersion']="#{bundleVersion}"
#plistHash['CFBundleDisplayName']="#{displayName}"
plistContent=plistHash.to_plist
f = File.new(infoPlistPath, "w")
f.puts plistContent
f.close
else
message="info.plist cannot be found"
error=1
end
end
# builds adhoc build configuration and outputs .app and .dSYM in the root of the workspace
if error==0
if ENV['configuration'] == "Adhoc" || ENV['configuration'] == "AdHoc"
buildPath=File.join(workspace,"build","#{configuration}-iphoneos")
if ENV['buildWorkspace'] == "Yes"
puts "build command: cd #{xcodeDir}; xcodebuild -workspace #{xcworkspace}.xcworkspace -scheme #{scheme} -configuration #{configuration} -sdk iphoneos CONFIGURATION_BUILD_DIR=#{workspace}"
output=`cd #{xcodeDir}; xcodebuild -workspace #{xcworkspace}.xcworkspace -scheme #{scheme} -configuration #{configuration} -sdk iphoneos CONFIGURATION_BUILD_DIR=#{workspace}`
else
puts "build command: cd #{xcodeDir}; xcodebuild -configuration #{configuration} -sdk iphoneos -target #{target} CONFIGURATION_BUILD_DIR=#{workspace}"
output=`cd #{xcodeDir}; xcodebuild -configuration "#{configuration}" -sdk iphoneos -target #{target} CONFIGURATION_BUILD_DIR=#{workspace}`
end
success=0
success=1 if output.include?("** BUILD SUCCEEDED **")
if output.include?("** BUILD SUCCEEDED **")
puts "(** FINISHED BUILDING ADHOC **)"
puts output
puts "---------------------"
elsif output.include?("** BUILD FAILED **")
puts "(** FAILED BUILDING ADHOC **)"
puts output
puts "---------------------"
error=1
message="unable to build adhoc. check build output for errors"
end
end
end
# archives project in the build server's xcode organizer. requires xcodeproj, and scheme to run.
if error==0
if ENV['configuration'] == "Release"
archiveStartedTime=Time.now
puts "cd #{xcodeDir}; xcodebuild -workspace #{xcworkspace}.xcworkspace -scheme #{scheme} -configuration Release archive"
output=`cd #{xcodeDir}; xcodebuild -workspace #{xcworkspace}.xcworkspace -scheme #{scheme} -configuration Release archive`
puts output
archivePath=Dir.glob("#{archivesPath}/*/**").max_by {|f| File.mtime(f)}
archiveDate=File.mtime(archivePath)
archivePath=archivePath.gsub(" ","\\ ")
success=0
success=1 if archiveStartedTime < archiveDate
if success==1
puts "(** FINISHED BUILDING ARCHIVE **)"
puts output
puts "---------------------"
else
puts "(** FAILED BUILDING ARCHIVE **)"
puts output
puts "---------------------"
error=1
message="unable to build archive. check build output for errors"
end
end
end
# searches the root of the workspace for the .app path for an adhoc build
if error==0
if ENV['configuration'] == "Adhoc" || ENV['configuration'] == "AdHoc"
appPath=`find #{workspace} -name *.app -maxdepth 1`
appPath=appPath.gsub(/\s+/, "")
if File.exist?(appPath)
else
message="cannot find .app"
error=1
end
end
end
# sets paths for the release archive
if error==0
if ENV['configuration'] == "Release"
archivesPath="/Users/Shared/Jenkins/Archives/"
archivePath=Dir.glob("#{archivesPath}/*/**").max_by {|f| File.mtime(f)}
archiveDate=File.mtime(archivePath)
archivePath=archivePath.gsub(" ","\\ ").to_s
appPath=`find #{archivePath}/Products/Applications -name *.app -maxdepth 1`
appPath.to_s
appPath=appPath.gsub(" ","\\ ").to_s
appPath=appPath.gsub(/\n+/, "")
configuration="Adhoc"
puts "releaseAppPath: #{appPath}"
end
end
# builds ipa with embedded profile
if error==0
ipaName="#{jobName}-#{bundleVersion}.ipa"
ipaPath="#{workspace}/#{ipaName}"
profileConfig="#{configuration}"
# builds ipa with embedded profile in workspace root and puts the build output in jenkins' console output
command="/usr/bin/xcrun -sdk iphoneos PackageApplication -v #{appPath} -o \"#{ipaPath}\" --sign \"#{codeSigningIdentity}\" --embed \"#{provProfilePath}\""
puts command
output= `#{command}`
puts "(** FINISHED BUILDING IPA **)"
puts output
f = File.new(lastSuccessfulBuild, "w")
f.puts buildStartedTime
f.close
else
puts "(** FAILED BUILDING IPA **)"
puts output
message="unable to build ipa. check build output for errors"
error=1
end
# builds the manifest-plist from a prefab and enters info from post-inject info.plist
if File.exist?(ipaPath)
if error==0
# sets manifest.plist path and name
manifestName="#{jobName}-#{bundleVersion}.plist";
manifestPath=File.join(workspace, manifestName)
# modifies manifest.plist with data from info.plist
plistHash = Plist::parse_xml(prefabPath)
plistHash['items'][0]['metadata']['bundle-identifier']=bundleIdentifier
plistHash['items'][0]['metadata']['bundle-version']=bundleVersion
plistHash['items'][0]['metadata']['title']="#{displayName}"
plistHash['items'][0]['metadata']['subtitle']="#{hockeykitVersionCap} ##{buildNumber}"
# saves manifest.plist
plistContent=plistHash.to_plist
f = File.new(manifestPath, "w")
f.puts plistContent
f.close
end
else
message="cannot find .ipa. unable to build manifest.plist"
error=1
end
# makes directory and copies manifest and ipa to hockeykit server
if error==0
# set paths and folder names
installAppFolder="#{bundleIdentifier}.#{hockeykitVersion}"
installBuildFolder="build-#{buildNumber}"
installDiskPath=File.join(installDiskRootPath, installAppFolder)
installDiskPathBuild=File.join(installDiskPath, installBuildFolder)
# creates app dir on install server if it doesn't exist
`mkdir -p #{installDiskPath}`
# creates build dir on install server if it doesn't exist
`mkdir -p #{installDiskPathBuild}`
# copies ipa to install server
`cp #{ipaPath} #{installDiskPathBuild}/#{ipaName}`
# copies the profile to install server
`cp #{provProfilePath} #{installDiskPath}/#{provProfile}`
puts "copying provProfile cp #{provProfilePath} #{installDiskPath}/#{provProfile}"
# copies manifest.plist to install server. copies after ipa so it won't show up in hockeykit until ipa upload is complete
`cp #{manifestPath} #{installDiskPathBuild}/#{manifestName}`
puts " "
puts "---------------------"
puts " "
puts "Finished building and uploading #{ipaName} and #{manifestName} to install server"
puts " "
puts "---------------------"
puts " "
message="ipa generated and uploaded to install successfully!"
end
# sends success email if error==0
if error==0
body=<<EOF
iOS Builds has finished building #{displayName} v#{bundleVersion}!
build server install options:
http://build.example.com
install on your device:
itms-services://?action=download-manifest&url=http%3A%2F%2Fbuild.example.com%2Finstall%2Fapi%2F2%2Fapps%2F#{installAppFolder}%3Fformat%3Dplist
commits since last successful build:
#{lastSuccessCommitLog}
EOF
if ENV['configuration'] == "Release"
subject="SUCCESS! #{displayName} v#{bundleVersion} Release finished building and uploading!"
else
subject="SUCCESS! #{displayName} v#{bundleVersion} finished building and uploading!"
end
emailHandler.sendMail(successEmail,subject,body)
puts "(** SENDING BUILD SUCCEEDED EMAIL **)"
puts "build succeeded!"
end
# sends failure email if error==1
if error==1
body=<<EOF
iOS Builds has failed building #{displayName} v#{bundleVersion}!
error message: #{message}
build output url:
#{outputUrl}
job url:
#{jobUrl}
EOF
if ENV['configuration'] == "Release"
subject="**FAILED** #{displayName} v#{bundleVersion} Release!"
else
subject="**FAILED** #{displayName} v#{bundleVersion}!"
end
emailHandler.sendMail(failureEmail,subject,body)
puts "(** SENDING BUILD FAILED EMAIL **)"
puts "build failed: #{message}"
exit
end