-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotech-release.py
More file actions
executable file
·303 lines (249 loc) · 10.4 KB
/
Copy pathmotech-release.py
File metadata and controls
executable file
·303 lines (249 loc) · 10.4 KB
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
#!./env/bin/python2.7
# To run this script you need the following modules installed:
# jenkinsapi==0.2.16
# pytz==2013.9
# requests==2.2.0
# sh==1.09
# wsgiref==0.1.2
import getopt
from jenkinsapi import jenkins
import os
import sh
import shutil
import string
import sys
from xml.dom.minidom import parse
def usage(argv, msg=None):
if msg:
print >>sys.stderr, msg
print >>sys.stderr
print >>sys.stderr, """\
Usage: %s [options]
Required options
^^^^^^^^^^^^^^^^
--jenkinsUsername A user in jenkins who has permission to create new jobs
--jenkinsPassword The password for the jenkins user
--gerritUsername A username in gerrit who is in the Bypass Review group.
--version The version of the release i.e. 0.22
--developmentVersion The next development version on the branch i.e. 0.22.1-SNAPSHOT
--nextMasterVersion The next working version on master i.e. 0.23-SNAPSHOT
Optional
^^^^^^^^
--buildDirectory The base location to check out all source to. Will delete if it exists
defaults to ./builds
--verbose Be a little chatty on stdout
Standard options
^^^^^^^^^^^^^^^^
-h, --help show this help and exit
""" % (sys.argv[0])
def main():
baseBuildDir = "./builds/"
jenkinsUsername = None
jenkinsPassword = None
gerritUsername = None
version = None
developmentVersion = None
nextMasterVersion = None
branchName = None
scmTag = None
verbose = False
try:
opts, args = getopt.getopt(sys.argv[1:], 'h',
['help', 'jenkinsUsername=', 'jenkinsPassword=', 'buildDirectory=', 'developmentVersion=', 'version=', 'nextMasterVersion=', 'gerritUsername=', 'verbose'])
allopts = set(opt[0] for opt in opts)
if '-h' in allopts or '--help' in allopts:
usage(sys.argv)
return 0
except getopt.error, err:
usage(sys.argv, 'Error: %s' % err)
return 1
except IndexError:
usage(sys.argv, 'Error: Insufficient arguments.')
return 1
except UnicodeError:
print >>sys.stderr, (
'Error: Multibyte filename not supported on this filesystem '
'encoding (%r).' % fs_encoding)
return 1
for opt, val in opts:
if opt == '--jenkinsUsername':
jenkinsUsername = val
elif opt == '--jenkinsPassword':
jenkinsPassword = val
elif opt == '--buildDirectory':
baseBuildDir = val
elif opt == '--version':
version = val
branchName = "{0}.X".format(version)
scmTag = "release-{0}".format(version)
elif opt == '--gerritUsername':
gerritUsername = val
elif opt == '--developmentVersion':
developmentVersion = val
elif opt == '--nextMasterVersion':
nextMasterVersion = val
elif opt == '--verbose':
verbose = True
if verbose:
print "jenkinsUsername: %s" % jenkinsUsername
print "jenkinsPassword: %s" % jenkinsPassword
print "baseBuildDir: %s" % baseBuildDir
print "version: %s" % version
print "branchName: %s" % branchName
print "scmTag: %s" % scmTag
print "gerritUsername: %s" % gerritUsername
print "developmentVersion: %s" % developmentVersion
print "nextMasterVersion: %s" % nextMasterVersion
if not (jenkinsUsername and jenkinsPassword and version and developmentVersion):
usage(sys.argv)
return 0
builds = [
{
'name' : 'Platform-IntegrationTests',
'url' : "ssh://{0}@review.motechproject.org:29418/motech".format(gerritUsername),
'repository' : 'motech',
'jobName' : "Platform-{0}".format(branchName)
},
{
'name' : 'Platform-Modules',
'url' : "ssh://{0}@review.motechproject.org:29418/modules".format(gerritUsername),
'repository' : 'modules',
'jobName' : "Modules-{0}".format(branchName)
}
]
# Connect to Jenkins
ci = jenkins.Jenkins("http://ci.motechproject.org", jenkinsUsername, jenkinsPassword)
# First check if all the builds are passing
print "Checking build status"
brokenBuild = False
for b in builds:
job = ci.get_job(b['name'])
build = job.get_last_build()
isRunning = build.is_running()
# get_status returns None if building. is_good returns False if building
print "\t%s: %s" % (b['name'], build.get_status())
if not build.is_good():
print "\t\t*** Unable to continue: failed or in progress build ***"
brokenBuild = True
if brokenBuild:
return 1
if os.path.exists(baseBuildDir):
shutil.rmtree(baseBuildDir)
sh.mkdir(baseBuildDir)
sh.cd(baseBuildDir)
print "\nChecking out repositories"
for b in builds:
repository = b['repository']
url = b['url']
try:
print "\tCloning " + b['name']
sh.git("clone", url, repository)
sh.cd(repository)
sh.git("config", "remote.origin.push", "refs/heads/*:refs/for/*")
sh.scp("-p", "-P", "29418", "{0}@review.motechproject.org:hooks/commit-msg".format(gerritUsername), ".git/hooks/")
sh.cd('..')
except Exception, e:
print "\t\t*** Unable to continue: exception pulling %s ***" % b['repository']
print e
return 1
# run mvn release:branch command
print "\nBranching repositories"
for b in builds:
repository = b['repository']
print "\tBranching %s" % repository
sh.cd(repository)
branchNameArg = "-DbranchName={0}".format(branchName)
masterVersion = "-DdevelopmentVersion={0}".format(nextMasterVersion)
scmConnectionArg = "-Dscm.connection=scm:git:ssh://{0}@review.motechproject.org:29418/{1}".format(gerritUsername, repository)
scmDeveloperConnectinArg = "-Dscm.developerConnection=scm:git:ssh://{0}@review.motechproject.org:29418/{1}".format(gerritUsername, repository)
try:
for line in sh.mvn("release:branch", branchNameArg, masterVersion, scmConnectionArg, scmDeveloperConnectinArg, _iter=True):
if verbose:
print(line),
except Exception, e:
print "\t\t*** Unabe to continue: exception branching %s ***" % b['repository']
print e
return 1
sh.cd('..')
print "\nUpdate MOTECH Version for module repositories"
for b in builds:
if b['name'] is "Platform-IntegrationTests":
continue
repository = b['repository']
print "\tUpdateing version for %s" % repository
sh.cd(repository)
# Update motech.version to the release version
pom = open("pom.xml")
dom = parse(pom)
pom.close()
mv = dom.getElementsByTagName('motech.version')
mv[0].childNodes[0].data = nextMasterVersion
f = open("pom.xml", 'w')
dom.writexml(f)
f.close()
sh.cd('..')
print "\nCommit and Push changes"
for b in builds:
repository = b['repository']
print "\tUpdateing %s" % repository
sh.cd(repository)
# commit the file
sh.git("commit", "-am", "Update to latest released version")
sh.git("push", "origin", "master")
sh.cd('..')
# Leave the directory with the code folders
sh.cd('..')
# Create new jobs on jenkins for each of the builds
print "\nCreating Jenkins jobs"
for b in builds:
repository = b['repository']
# Use api and config.xml files downloaded from ci.motechproject.org (will need to process config.xml to update version numbers)
# Also use api to add new jobs to views
# Should old jobs be deprecated?
d = { 'branchName' : branchName, 'version' : version, 'developmentVersion' : developmentVersion, 'scmTag' : scmTag}
filein = open("build-configs/{0}/config.xml".format(repository))
src = string.Template(filein.read())
result = src.substitute(d)
jobName = b['jobName']
print "\tCreating %s" % jobName
try:
newJob = ci.create_job(jobName, result)
ci.views['Releases'].add_job(jobName, newJob)
except Exception, e:
print "\t\t*** Unabe to continue: exception creating job %s ***" % jobName
print e
return 1
return 0
if False:
# For the modules repositories I need to:
# - Change motech.version to the release version.
# - Trigger the build.
# - Change motech.version to the developmentVersion
# I think to trigger a release build post to:
# args = "name=releaseVersion&value=%s&name=developmentVersion&value=%s&name=scm.tag&value=%s&json=json:{\"parameter\": [{\"name\": \"releaseVersion\", \"value\": \"%s\"}, {\"name\": \"developmentVersion\", \"value\": \"%s\"}, {\"name\": \"scm.tag\", \"value\": \"%s\"}]}" % ("0.22", "0.22.1-SNAPSHOT", "release-0.22", "0.22", "0.22.1-SNAPSHOT", "release-0.22")
# curl --data-urlencode args http://ci.motechproject.org/view/Releases/job/Platform-Communications-0.22.X/release/submit
# releaseVersion, developmentVersion, scm.tag
# For each of the module before triggering it's release build I'll need to update motech.version in the pom
# look into http://docs.python.org/2/library/xml.dom.minidom.html for editing the pom file
for b in builds:
if b['name'] is "Platform-IntegrationTests":
continue
repository = b['repository']
sh.cd(repository)
# I'll need to be on the release branch
# Update motech.version to the release version
pom = open("pom.xml")
dom = parse(pom)
pom.close()
mv = dom.getElementsByTagName('motech.version')
mv[0].childNodes[0].data = version
f = open("pom.xml", 'w')
dom.writexml(f)
f.close()
# commit the file
# git commit -am "Update motech to latest released version"
# "git push origin {0}.format(branchName)
sh.cd('..')
if __name__ == '__main__':
main()
# MOTECH-Platform (Platform-IntegrationTests), Platform-Communications, Platform-Campaigns, Platform-MRS and Platform-Demo jobs are green on our CI server