forked from Mondtic/build-jenkins-job
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
78 lines (66 loc) · 2.67 KB
/
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
#!/usr/bin/env python3
import sys
import time
import jenkins
import json
import requests
import urllib3
# Arguments
optional_arg = lambda argv, index, default: default if (len(argv) <= index or argv == "") else argv[index]
if len(sys.argv) < 4:
raise ValueError("Required fields: jenkins url, jenkins api token, jenkins username and jenkins job")
JENKINS_URL = sys.argv[1]
JENKINS_TOKEN = sys.argv[2]
JENKINS_USERNAME = sys.argv[3]
JENKINS_JOB_NAME = sys.argv[4]
JENKINS_JOB_PARAMS = optional_arg(sys.argv, 5, '{}')
JENKINS_WAIT_JOB = optional_arg(sys.argv, 6, "wait")
JENKINS_SSL_VERIFY = optional_arg(sys.argv, 7, True)
if JENKINS_SSL_VERIFY.lower() == 'false':
print("**Warning*** SSL certificate validation disabled by arguments!")
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Setup Jenkins Connection and start build
connection = jenkins.Jenkins(JENKINS_URL, JENKINS_USERNAME, JENKINS_TOKEN)
connection._session.verify = JENKINS_SSL_VERIFY
queue_id = connection.build_job(JENKINS_JOB_NAME, parameters=json.loads(JENKINS_JOB_PARAMS), token=JENKINS_TOKEN)
print(f"Build queued with queue_id: {queue_id}")
# Wait for the build to start and store the build no
protocol, domain = JENKINS_URL.split("://")
build_number = None
while True:
try:
queue_info = requests.get(f"{protocol}://{JENKINS_USERNAME}:{JENKINS_TOKEN}@{domain}/queue/item/{queue_id}/api/json?pretty=true", verify=JENKINS_SSL_VERIFY).json()
build_number = queue_info["executable"]["number"]
break
except KeyError:
# todo: sometimes executable is in queue_info, but
# number is not, why?
if "executable" in queue_info:
print(queue_info["executable"])
time.sleep(3)
if build_number is None:
raise Exception("Build could not be started")
print(f"Build started with build_number: {build_number}")
print(f"::set-output name=job_build_number::{build_number}")
# early exit?
if JENKINS_WAIT_JOB == "no-wait" and build_number:
print(f"Build completed with status: EXECUTED")
print("::set-output name=job_status::EXECUTED")
exit(0)
# Wait for the build to complete
console_lines = 0
build_info = None
while True:
console = connection.get_build_console_output(JENKINS_JOB_NAME, build_number).splitlines()
for i in range(console_lines, len(console)):
print(console[i])
console_lines = len(console)
build_info = connection.get_build_info(JENKINS_JOB_NAME, number=build_number)
if (status := build_info["result"]):
break
time.sleep(3)
# Summary
print(f"Build completed with status: {status}")
print(f"::set-output name=job_status::{status}")
if status not in ['SUCCESS', 'UNSTABLE']:
exit(1)