-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·113 lines (87 loc) · 5.45 KB
/
run
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
#!/usr/bin/env python
import argparse
import package
import subprocess
import teamcity
import github
import time
import sys
import os
parser = argparse.ArgumentParser(description = 'Reaver Project CI scripts: configuration matrix runner')
parser.add_argument('package', help = 'Package to use as set of arguments.')
parser.add_argument('command', help = 'Command to run. Substitutes {} for the version.')
parser.add_argument('-t', '--teamcity', required = 'true', help = 'TeamCity server to use.')
parser.add_argument('-u', '--user', required = 'true', help = 'TeamCity user to login as.')
parser.add_argument('-p', '--password', required = 'true', help = 'Password for the selected user.')
parser.add_argument('-i', '--id', required = 'true', help = 'ID of the build configuration to trigger.')
parser.add_argument('-s', '--style', default = 'solid', help = 'Style of configuration argument passing. Currently supported: "solid".')
parser.add_argument('-B', '--branch', required = False, help = 'Logical branch to use.')
parser.add_argument('-G', '--github', action = 'store_true', help = 'Enable Github statuses.')
parser.add_argument('-O', '--github-owner', help = 'Target github repository owner.')
parser.add_argument('-R', '--github-repo', help = 'Target github repository name.')
parser.add_argument('-C', '--github-commit-sha', help = 'The commit hash.')
parser.add_argument('-U', '--github-user', help = 'Github API user.')
parser.add_argument('-P', '--github-token', help = 'Github API token.')
parser.add_argument('-M', '--mr', action = 'store_true', help = 'Treat the commit as an MR head and set commit status instead of adding a comment on the commit.')
group = parser.add_mutually_exclusive_group(required = False)
group.add_argument('-b', '--built-only', action = 'store_true', help = 'Only run for built versions.')
group.add_argument('-v', '--not-built-only', action = 'store_true', help = 'Only run for not built versions.')
group.add_argument('-V', '--not-built-built-bases', action = 'store_true', help = 'Only run for not built versions that have their base versions built.')
parser.add_argument('version', nargs = '?')
args = parser.parse_args()
if args.github and not (args.github_owner and args.github_repo and args.github_commit_sha and args.github_user and args.github_token):
parser.error('Github options enabled, but not specified.')
package = package.Package(args.package)
versions = package.combined_versions(built_bases = args.not_built_built_bases)
if args.built_only or args.not_built_only or args.not_built_built_bases:
versions = [ version for version in versions if (version in package.built_versions()) == args.built_only ]
if args.style == 'solid':
pass
else:
print 'Style %s not supported.' % args.style
exit(1)
session = teamcity.Session(args.teamcity + '/httpAuth/app/rest', args.user, args.password)
ghsession = github.Session('https://api.github.com/', args.github_user, args.github_token)
if 'version' in args and args.version is not None:
current_url = '%s&guest=1' % session.get_url({ 'buildType': args.id, 'number': os.environ['BUILD_NUMBER'], 'branch': '(default:any)', 'running': 'any' })
if args.github:
ghsession.set_status(args.github_owner, args.github_repo, args.github_commit_sha, {
'state': 'pending',
'target_url': current_url,
'description': 'Job started (branch: %s, environment: %s).' % (args.branch or "default", args.version),
'context': 'ci/%s' % args.version
})
command = args.command.replace('{}', args.version)
session.add_tag({ 'buildType': args.id, 'number': os.environ['BUILD_NUMBER'], 'branch': '(default:any)', 'running': 'any' }, args.version)
exit_code = subprocess.call('/usr/bin/env bash -c \'' + command + '\'', shell = True)
if args.github:
state = 'success' if exit_code == 0 else 'failure'
ghsession.set_status(args.github_owner, args.github_repo, args.github_commit_sha, {
'state': state,
'target_url': current_url,
'description': 'Job %s (branch: %s, environment: %s).' % (state, args.branch or "default", args.version),
'context': 'ci/%s' % args.version
})
if not args.mr and exit_code != 0:
ghsession.add_comment(args.github_owner, args.github_repo, args.github_commit_sha,
'CI job has failed for environment `%s`: %s.' % (args.version, current_url))
exit(exit_code)
else:
responses = {}
session.add_tag({ 'buildType': args.id, 'number': os.environ['BUILD_NUMBER'], 'branch': '(default:any)', 'running': 'any' }, "trigger")
change_id = session.get_change_id({ 'buildType': args.id, 'number': os.environ['BUILD_NUMBER'], 'branch': '(default:any)', 'running': 'any' })
print 'Internal change id to use: %d.' % change_id
for version in versions:
responses[version] = session.trigger(args.id,
properties = { 'version': version },
change = change_id,
branch = args.branch)
print 'Started build for %s: %s.' % (version, responses[version]['webUrl'])
sys.stdout.flush()
if args.github:
ghsession.set_status(args.github_owner, args.github_repo, args.github_commit_sha, {
'state': 'pending',
'target_url': '%s&guest=1' % responses[version]['webUrl'],
'description': 'Job queued (branch: %s, environment: %s).' % (args.branch or "default", version),
'context': 'ci/%s' % version
})