-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-build.py
executable file
·365 lines (302 loc) · 12.6 KB
/
jenkins-build.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/python -B
import sys
sys.dont_write_bytecode = True
import argparse
import os
import requests
import shutil
import subprocess
import build_tools
default_workspace = os.environ.get(
'WORKSPACE',
os.path.abspath(os.path.dirname(__file__)))
default_build_number = os.environ.get(
'BUILD_NUMBER',
'1337')
repos_failing_tests = []
def get_version(workspace):
# load up the version number
version_file = open(os.path.join(workspace, 'mb_version'), 'r')
version = version_file.readline()
version = version.strip()
version_file.close()
return version
def very_clean_repo(repo):
# ignore repos not checked out yet
if repo.exists():
return repo.cmd(['git', 'clean', '-dfx'])
def update_sources(toolchain):
# Quick hack because the jenkins builders don't seem to be able to create
# this folder automatically as part of cloning?
if not os.path.exists(build_tools.paths.toolchain_dir()):
os.makedirs(build_tools.paths.toolchain_dir())
print('\nUpdating toolchain...')
with build_tools.utils.timer('updating sources'):
rv = toolchain.switch_version()
return rv
def parse_args(args, jenkins_parser):
arg_parser = argparse.ArgumentParser(
description='Builds an installer for the current platform',
parents=(
[build_tools.get_parent_parser(), jenkins_parser] +
build_tools.builders.argument_parsers))
arg_parser.add_argument(
'-w',
'--workspace',
metavar='PATH',
default=default_workspace,
help='absolute path of Toolchain-Release')
arg_parser.add_argument(
'-n',
'--build-number',
metavar='N',
default=default_build_number)
arg_parser.add_argument(
'-C',
'--very-clean-build',
action='store_true',
help='use git to clean absolutely everything from each repo')
arg_parser.add_argument(
'--upload-docs',
action='store_true',
help='Upload docs to docs.soft.makerbot.net, implies --docs')
arg_parser.add_argument(
'--no-clean',
action='store_true',
help='skip some of the basic cleaning that would normally happen')
arg_parser.add_argument(
'--skip-update',
action='store_true',
help='skip update before building')
arg_parser.add_argument(
'--installer-only',
action='store_true',
help='assume the repos have been built, just packaging scripts')
arg_parser.add_argument(
'--publish-artifacts',
action='store_true',
help='publish artifacts from this build')
arg_parser.add_argument(
'--no-revs',
dest='revs',
action='store_false',
help="don't write a revs file")
if build_tools.utils.is_windows:
angle_help = 'override the OpenGL version for qt with an angle version'
else:
# We should use argparse.SUPPRESS here, but it seems to cause
# assertion errors in argparse. (Looks like it's adding a space
# when it shouldn't and then checking that the thing with and
# without the space are the same. They're not.)
angle_help = 'Only used on windows'
arg_parser.add_argument(
'--use-angle',
action='store_true',
help='override the OpenGL version for qt with an angle version')
return arg_parser.parse_args(args)
def path_for_toolchain(toolchain):
"""Find the path to the jenkins module for building a given toolchain"""
if toolchain == build_tools.toolchain_list.desktop:
if 'darwin' == sys.platform:
jenkins_path = 'Bundle-Mac'
elif 'win32' == sys.platform:
jenkins_path = 'Bundle-Win'
elif sys.platform.startswith('linux'):
import platform
(distro, version, codename) = platform.linux_distribution()
if distro in ['Ubuntu']:
jenkins_path = 'Bundle-Debian'
elif distro in ['Fedora']:
jenkins_path = 'Bundle-Redhat'
else:
raise ValueError(distro)
else:
raise ValueError(sys.platform)
elif toolchain == build_tools.toolchain_list.argo:
if 'win32' == sys.platform:
jenkins_path = 'Bundle-Win'
else:
raise ValueError(sys.platform)
elif toolchain == build_tools.toolchain_list.morepork:
jenkins_path = 'Bundle-Morepork'
else:
raise Exception('toolchain not supported by jenkins-build.')
return jenkins_path
def setup_build_args(all_args, toolchain):
# handle the build arguments
build_args = build_tools.builders.extract_build_args(all_args)
# override certain args
build_args['install'] = True
# special case for SCons asks things to depend on the installed
# versions of the other repos, rather than as siblings
if 'scons_args' not in build_args:
build_args['scons_args'] = []
build_args['scons_args'].append('--no-devel-libs')
if build_tools.utils.is_windows:
# special case for windows toggle between console and windowed app
# I would have come up with some special way to stick this in
# the windows jenkins, but we're probably going to rip this out
# in the not-too-distant future.
build_args['scons_args'].append('--hide-console')
# turn up the verbosity on cmake tests
if 'cmake_test_args' not in build_args:
build_args['cmake_test_args'] = []
build_args['cmake_test_args'].append('-V')
# we want --upload-docs to imply the --docs flags to ensure that
# that docs are built when we try to upload them
if build_args['upload_docs'] and 'docs' not in build_args:
build_args['docs'] = True
for source in toolchain:
source.builder.bind_args(**build_args)
def upload_artifact(repo):
rv = repo.upload(stable=True)
if not rv:
print("publish artifacts failed, passing")
return rv
def build(fake_root, toolchain, additional_args):
'''
@param fake_root the install destination of the build
@param toolchain Toolchain object representing the repo list
@param additional_args, any extra building args
@return True on success. Terminates script on failure
'''
if build_tools.utils.is_windows and additional_args.use_angle:
# Hack to make mw-scons-tools work with the angle/opengl versions of QT
os.putenv('QT5_ANGLE', 'TRUE')
if os.path.exists(fake_root):
shutil.rmtree(fake_root)
result = build_tools.builders.SUCCESS
for repo in toolchain:
rv = repo.build()
if rv is build_tools.builders.FAILURE:
print('building ' + repo.name + ' failed')
return rv
elif rv is build_tools.builders.FAILED_TESTS:
result = build_tools.builders.FAILED_TESTS
repos_failing_tests.append(repo.name)
# Sort of a hack, just do the install if the tests
# fail. I think we only want this behaviour on
# jenkins, otherwise I'd put this in builders.py
repo.build(build=False, test=False, clean=False)
# this being in an else block makes it so that we don't upload when
# a repo fails to build or fails tests
elif additional_args.publish_artifacts:
if not upload_artifact(repo):
repos_failing_tests.append(repo.name + "_artifacts")
result = build_tools.builders.FAILED_TESTS
return result
def run(args=[]):
# We break arg parsing into two stages, because the initial
# toolchain selection can affect further argument parsing
toolchain_args, unparsed_args = (
build_tools.get_parent_parser().parse_known_args(args))
toolchain = build_tools.get_toolchain(toolchain_args)
# Load the appropriate jenkins file
jenkins_path = path_for_toolchain(toolchain)
sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), jenkins_path)))
import jenkins
# finish parsing
arguments = parse_args(unparsed_args, jenkins.argument_parser())
# hack to propogate platform because the platform gets set to default
# which can be (and is) wrong sometimes
arguments.platform = toolchain_args.platform
# we have to do the default manually here because the
# build argparser defaults most things to None
if arguments.install_dir is None:
# TODO(ted): pull from builders.py
arguments.install_dir = os.path.join(arguments.workspace,
"Install")
version = '.'.join(
[get_version(arguments.workspace), arguments.build_number])
if arguments.very_clean_build:
# should we be cleaning distributables?
toolchain.for_each_repo(very_clean_repo)
# no reason to clean if we just very cleaned
arguments.clean = False
if not arguments.no_clean and not arguments.installer_only:
# clean the fake root
if os.path.exists(arguments.install_dir):
shutil.rmtree(arguments.install_dir, ignore_errors=True)
# update (if applicable)
if not arguments.skip_update and not arguments.installer_only:
rv = update_sources(toolchain)
if rv != 0:
print('Updating failed!')
return rv
# jenkins checks out a commit hash when building,
# however we want to upload it to artifactory with the branch
# this is kind of hacky, and we make sure to do it after updating
if 'Toolchain-Release' in toolchain:
branch = os.environ.get('GIT_BRANCH')
if branch:
# jenkins stores the branch as `origin/[branch]`
# so basename should drop the `origin/` part
toolchain.toolchain_release.version = os.path.basename(branch)
setup_build_args(arguments, toolchain)
if arguments.revs:
import json
info = build_tools.build_info(toolchain)
with open('revs', 'w') as revs:
json.dump(info, revs, indent=2)
print('')
for source_dict in info:
# HACKS: do all the hacky things!
# we make some modifications to print more nicely in jenkins
if 'modified' in source_dict and source_dict['modified']:
source_dict['modified'] = ' (modified)'
else:
source_dict['modified'] = '\t'
for arg in source_dict['build_args'].keys():
# strip empty lists
if source_dict['build_args'][arg] == []:
del source_dict['build_args'][arg]
# Makes Distributables print more useful version info
if 'versions' in source_dict:
source_dict['version'] = (
source_dict['versions'][source_dict['version']])
print('{name}:\t{version}{modified}\t{builder}'
'({build_args})'.format(**source_dict))
print('')
# We frequently benefit from knowing the env for a broken build
print('Environment (parts may be overridden in code after this point):')
print('')
for k, v in sorted(os.environ.iteritems(), key=lambda t: t[0]):
print('{}: {}'.format(k, v))
print('')
if not arguments.installer_only:
with build_tools.utils.timer('build'):
rv = build(fake_root=arguments.install_dir,
toolchain=toolchain,
additional_args=arguments)
if rv is build_tools.builders.FAILED_TESTS:
rv = build_tools.utils.mark_build_unstable(arguments.workspace)
if rv != 0:
return -1
elif rv is not build_tools.builders.SUCCESS:
return -1
with build_tools.utils.timer('Platform-specifc packaging'):
# Actually do the platform-specific packaging
rv = jenkins.run(
workspace=arguments.workspace,
version=version,
toolchain=toolchain,
fake_root=arguments.install_dir,
additional_args=arguments)
if rv is not build_tools.builders.SUCCESS:
return -1
if arguments.upload_docs:
with build_tools.utils.timer('Publishing docs'):
upload_url = '[email protected]:repos/'
upload_url += build_tools.paths.DEFAULT_REF + '/'
copy_command = ['scp',
'-r',
os.path.join(arguments.install_dir, 'docs', '*'),
upload_url]
subprocess.check_call(' '.join(copy_command), shell=True)
if len(repos_failing_tests) > 0:
for r in repos_failing_tests:
print r + " is failing unit tests"
return 0
if '__main__' == __name__:
sys.exit(run(sys.argv[1:]))