forked from Mondtic/build-jenkins-job
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_job.py
71 lines (53 loc) · 2.18 KB
/
build_job.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
#!/usr/bin/env python3
import sys
import time
import urllib3
import os
from src.apps.Jenkins.Infrastructure.ServerJenkinsRepository import ServerJenkinsRepository
from src.apps.Jenkins.Application.Build.JobBuilder import JobBuilder
from src.apps.Jenkins.Application.Find.BuildFinder import BuildFinder
from src.apps.Jenkins.Domain.JobParams import JobParams
def set_action_output(output_name, value):
"""Sets the GitHub Action output.
Keyword arguments:
output_name - The name of the output
value - The value of the output
"""
if "GITHUB_OUTPUT" in os.environ:
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
print("{0}={1}".format(output_name, value), file=f)
def mandatory_arg(argv):
if argv == "":
raise ValueError("Required fields: jenkins url, jenkins api token, jenkins username and jenkins job")
return argv
JENKINS_URL = mandatory_arg(sys.argv[1])
JENKINS_TOKEN = mandatory_arg(sys.argv[2])
JENKINS_USERNAME = mandatory_arg(sys.argv[3])
JENKINS_JOB_NAME = mandatory_arg(sys.argv[4])
# Optional args
JENKINS_JOB_PARAMS = sys.argv[5] if len(sys.argv) >= 5 else '{}'
JENKINS_WAIT_JOB = sys.argv[6] if len(sys.argv) >= 6 else "wait"
JENKINS_SSL_VERIFY = (sys.argv[7] == 'true') if len(sys.argv) >= 7 else true
if not JENKINS_SSL_VERIFY:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Set Jenkins Connection
repository = ServerJenkinsRepository(url=JENKINS_URL, token=JENKINS_TOKEN, username=JENKINS_USERNAME, ssl_verify=JENKINS_SSL_VERIFY)
# Build Job
builder = JobBuilder(repository=repository)
builder.exec(name=JENKINS_JOB_NAME, params=JobParams(JENKINS_JOB_PARAMS))
# Get build number
finder = BuildFinder(repository=repository, name=JENKINS_JOB_NAME)
build_number = finder.number()
print(f"BUILD NUMBER: {build_number}")
if JENKINS_WAIT_JOB == "no-wait" and build_number:
print("Job status is : EXECUTED")
set_action_output('job_status', 'EXECUTED')
exit(0)
# Get build status
while not (status := finder.exec(build_number)):
time.sleep(1)
print(f"Job status is : {status}")
set_action_output('job_status', status)
set_action_output('job_build_number', build_number)
#if status != 'SUCCESS':
# exit(1)