-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcreatepipelinejobs.py
executable file
·68 lines (59 loc) · 2.09 KB
/
createpipelinejobs.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
#!/usr/bin/env python
import errno
import os
import yaml
TEMPLATE_CONFIG_XML = 'TEMPLATE-pipeline-job-config.xml'
JENKINS_JOBS_DIR = './home/jobs'
PROPERTIES = """\
<org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
<triggers>
{TRIGGERS}
</triggers>
</org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
"""
TRIGGER = """\
<jenkins.triggers.ReverseBuildTrigger>
<spec></spec>
<upstreamProjects>{AFTER_JOBNAME}</upstreamProjects>
<threshold>
<name>SUCCESS</name>
<ordinal>0</ordinal>
<color>BLUE</color>
<completeBuild>true</completeBuild>
</threshold>
</jenkins.triggers.ReverseBuildTrigger>
"""
def main():
with open(TEMPLATE_CONFIG_XML) as f:
template = f.read()
with open('pipeline-configs.yaml') as f:
cfg = yaml.safe_load(f)
for (jobname, jobcfg) in cfg['jobs'].items():
new_job_dir = os.path.join(JENKINS_JOBS_DIR, jobname)
new_job_cfg = os.path.join(new_job_dir, 'config.xml')
print('Creating {} with configuration {}'.format(new_job_cfg, jobcfg))
if 'after' in jobcfg:
triggers = [TRIGGER.format(AFTER_JOBNAME=j)
for j in jobcfg['after']]
properties = PROPERTIES.format(TRIGGERS='\n'.join(triggers))
else:
properties = ''
job_config_xml = template.format(
DESCRIPTION=jobcfg['description'],
REPOSITORY=jobcfg['repository'],
BRANCH=jobcfg['branch'],
CLONE_TIMEOUT=jobcfg.get('clone_timeout', 10),
JENKINSFILE=jobcfg.get('jenkinsfile', 'Jenkinsfile'),
PROPERTIES=properties,
)
try:
os.mkdir(new_job_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if os.path.exists(new_job_cfg):
raise Exception("exists: " + new_job_cfg)
with open(new_job_cfg, 'w') as f:
f.write(job_config_xml)
if __name__ == '__main__':
main()