forked from distortedsignal/bd-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-build
executable file
·90 lines (71 loc) · 2.64 KB
/
jenkins-build
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
#!/usr/bin/python
# jenkins-build --tmp_args="{'asdlkjfasdf':'aldkfjlaksdfj'}" --username="" --password="" job_name
# jenkins-build -a="" -u="" -p="" job_name
# jenkins-build
import os
import sys
import json
import webbrowser
import jenkins
from git.repo.base import Repo
def decode_arg_pair(arg_pair):
if arg_pair[0] == 'a':
return ('tmp_args', arg_pair[1])
elif arg_pair[0] == 'u':
return ('username', arg_pair[1])
elif arg_pair[0] == 'p':
return ('password', arg_pair[1])
else:
return ('', '')
arg_list = sys.argv[1:]
job_name = 'Bluedata_bundle'
arg_dict = {}
print_config = False
for arg in arg_list:
if arg == '--config' or arg == '-c':
print_config = True
break
arg_pair = ()
if arg[0] != '-':
job_name = arg
continue
elif arg[1] == '-':
arg_pair = arg[2:].split('=')
else:
arg_pair = arg[1:].split('=')
arg_pair = decode_arg_pair(arg_pair)
arg_dict[arg_pair[0]] = arg_pair[1]
current_repo = Repo(path=os.getcwd())
branch_name = current_repo.head.ref.name
local_head = current_repo.head
remote_branch = current_repo.remote()
fetch_info = remote_branch.fetch(refspec=branch_name)[0]
if local_head.commit.hexsha != fetch_info.commit.hexsha:
push_y_n = input('You have un-pushed commits to your remote branch - would you like to push? [y/N]')
if 'y' == push_y_n.lower() or 'yes' == push_y_n.lower():
print "Pushing changes and building"
remote_branch.push()
# Load UN/PW from file.
# Load job details from a file.
script_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(script_path, 'un_pw.json')) as auth_file, \
open(os.path.join(script_path, 'default_jenkins_build.json')) as build_file:
if print_config:
print build_file.read()
sys.exit(0)
auth_dict = json.load(auth_file)
server = jenkins.Jenkins('http://10.3.47.5',
username=arg_dict.get('username', auth_dict['username']),
password=arg_dict.get('password', auth_dict['password'])
)
build_dict = json.load(build_file)
tmp_args_string = arg_dict.get('tmp_args', '{}').replace("'", '"')
tmp_args_dict = json.loads(tmp_args_string)
values = build_dict[job_name]
values['branch'] = branch_name
values.update(tmp_args_dict)
# Token unnecessary
server.build_job(job_name, parameters=values)
build_url = server.get_job_info(job_name)['builds'][0]['url'].replace('jenkins.lab.bluedata.com:8080', '10.3.47.5')
console_url = build_url + 'console'
webbrowser.open(console_url, autoraise=False)