forked from lhupfeldt/jenkinsflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_build_result.py
executable file
·115 lines (87 loc) · 4.76 KB
/
set_build_result.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
#!/usr/bin/env python
# Copyright (c) 2012 - 2014 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.
from __future__ import print_function
import sys, os, argparse, tempfile
jenkins_cli_jar = 'jenkins-cli.jar'
hudson_cli_jar = 'hudson-cli.jar'
def cli_jar_info(direct_url):
base_url = os.environ.get('JENKINS_URL')
cli_jar = jenkins_cli_jar
if base_url is None:
base_url = os.environ.get('HUDSON_URL')
cli_jar = hudson_cli_jar
base_url = base_url + '/' if base_url is not None and base_url[-1] != '/' else base_url
return cli_jar, direct_url or base_url, base_url if base_url != direct_url else None
def download_cli(cli_jar, base_url, public_base_url):
import urllib2
cli_url = base_url + 'jnlpJars/' + cli_jar
if public_base_url:
public_cli_url = public_base_url + 'jnlpJars/' + cli_jar
print("INFO: Downloading cli:", repr(public_cli_url), " (using direct url: ", cli_url, ')')
else:
print("INFO: Downloading cli:", repr(cli_url))
response = urllib2.urlopen(cli_url)
with open(cli_jar, 'w') as ff:
ff.write(response.read())
print("INFO: Download finished:", repr(cli_jar))
def set_build_result(username, password, result, direct_url=None, java='java'):
"""Change the result of a Jenkins job.
Note: set_build_result can only be done from within the job, not after the job has finished.
Note: Only available if URL is set in `Jenkins <http://jenkins-ci.org/>`_ system configuration.
This command uses the Jenkins `cli` to change the result. It requires a java executable to run the cli.
Args:
username (str): Name of jenkins user with access to the job
password (str): Password of jenkins user with access to the job
result (str): The result to set. Should probably be 'unstable'
direct-url (str): Jenkins URL. Default is JENKINS_URL env var value. Use this argument if JENKINS_URL is a proxy.
java (str): Alternative `java` executable. Use this if you don't wish to use the java in the PATH.
"""
print("INFO: Setting job result to", repr(result))
cli_jar, base_url, public_base_url = cli_jar_info(direct_url)
if base_url is None:
raise Exception("Could not get env variable JENKINS_URL or HUDSON_URL. Don't know whether to use " +
jenkins_cli_jar + " or " + hudson_cli_jar + " for setting result! " +
"You must set 'Jenkins Location' in Jenkins setup for JENKINS_URL to be exported. " +
"You must set 'Hudson URL' in Hudson setup for HUDSON_URL to be exported.")
import subprocess
def set_res():
command = [java, '-jar', cli_jar, '-s', base_url, 'set-build-result', result]
if username or password:
assert password and username, "You must specify both username and password if any"
fname = None
try:
fhandle, fname = tempfile.mkstemp()
fhandle = os.fdopen(fhandle, 'w')
fhandle.write(password)
fhandle.close()
subprocess.check_call(command + ['--username', username, '--password-file', fname])
finally:
try:
os.remove(fname)
fhandle.close()
except IOError: # pragma: no cover
pass
else:
subprocess.check_call(command)
try:
# If cli_jar is already present attempt to use it
set_res()
except subprocess.CalledProcessError:
# We failed for some reason, try again with updated cli_jar
download_cli(cli_jar, base_url, public_base_url)
set_res()
def args_parser():
parser = argparse.ArgumentParser(description='Change result of a Jenkins Job. Must be run from within the job!')
parser.add_argument('--username', help='Name of jenkins user with access to the job')
parser.add_argument('--password', help='Password of jenkins user with access to the job. *** Warning Insecure, will show up in process listing! ***')
parser.add_argument('--result', default='unstable', help="The result to set. Should probably be 'unstable'")
parser.add_argument('--direct-url', default=None, help="Jenkins URL. Default is JENKINS_URL env var value. Use this argument if JENKINS_URL is a proxy.")
parser.add_argument('--java', default='java', help="Alternative 'java' executable.")
return parser
def main(arguments):
args = args_parser().parse_args(arguments)
direct_url = args.direct_url + '/' if args.direct_url is not None and args.direct_url[-1] != '/' else args.direct_url
set_build_result(args.username, args.password, args.result, direct_url, args.java)
if __name__ == '__main__':
main(sys.argv[1:])