axmol/tools/jenkins-scripts/pull-request-builder.py

237 lines
7.3 KiB
Python
Raw Normal View History

2014-10-14 17:31:53 +08:00
#Github pull reqest builder for Jenkins
import json
import os
import urllib2
import urllib
import base64
import requests
2014-01-02 10:19:48 +08:00
import sys
2014-01-02 17:09:49 +08:00
import traceback
2015-03-10 18:42:26 +08:00
import subprocess
#set Jenkins build description using submitDescription to mock browser behavior
2015-03-10 15:44:02 +08:00
http_proxy = ''
if('HTTP_PROXY' in os.environ):
http_proxy = os.environ['HTTP_PROXY']
proxyDict = {'http': http_proxy, 'https': http_proxy}
branch = "v3"
pr_num = 0
workspace = "."
2015-03-10 17:03:03 +08:00
node_name = "ios"
2015-03-11 14:59:34 +08:00
# for local debugging purpose, you could change the value to 0 and run
# this scripts in your local machine
2015-03-10 17:47:00 +08:00
remote_build = 1
2015-03-10 15:44:02 +08:00
def set_jenkins_job_description(desc, url):
2015-03-10 14:40:04 +08:00
req_data = urllib.urlencode({'description': desc})
request = urllib2.Request(url + 'submitDescription', req_data)
#print(os.environ['BUILD_URL'])
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
user_name = os.environ['JENKINS_ADMIN']
password = os.environ['JENKINS_ADMIN_PW']
base64string = base64.encodestring(user_name + ":" + password).replace('\n', '')
request.add_header("Authorization", "Basic " + base64string)
2014-01-02 17:09:49 +08:00
try:
urllib2.urlopen(request)
2014-01-02 17:09:49 +08:00
except:
traceback.print_exc()
def check_current_3rd_libs(branch):
#run download-deps.py
2014-09-24 12:53:30 +08:00
print("prepare to downloading ...")
os.system('python download-deps.py -r no')
2015-03-10 15:44:02 +08:00
def send_notifies_to_github():
global branch
global pr_num
global workspace
global node_name
# get payload from os env
2014-01-02 17:09:49 +08:00
payload_str = os.environ['payload']
2015-03-03 16:36:42 +08:00
payload_str = payload_str.decode('utf-8', 'ignore')
2014-01-02 17:09:49 +08:00
#parse to json obj
payload = json.loads(payload_str)
#get pull number
pr_num = payload['number']
print 'pr_num:' + str(pr_num)
#build for pull request action 'open' and 'synchronize', skip 'close'
action = payload['action']
print 'action: ' + action
2014-01-06 20:58:00 +08:00
#pr = payload['pull_request']
2014-10-14 17:31:53 +08:00
url = payload['html_url']
2014-01-02 17:09:49 +08:00
print "url:" + url
2015-03-03 16:36:42 +08:00
pr_desc = '<h3><a href=' + url + '> pr#' + str(pr_num) + ' is ' + action + '</a></h3>'
2014-01-02 17:09:49 +08:00
#get statuses url
statuses_url = payload['statuses_url']
2014-01-02 17:09:49 +08:00
#get pr target branch
branch = payload['branch']
2015-03-10 15:44:02 +08:00
workspace = os.environ['WORKSPACE']
node_name = os.environ['NODE_NAME']
2014-01-02 17:09:49 +08:00
#set commit status to pending
# target_url = os.environ['BUILD_URL']
2014-01-23 00:21:58 +08:00
jenkins_url = os.environ['JENKINS_URL']
job_name = os.environ['JOB_NAME'].split('/')[0]
build_number = os.environ['BUILD_NUMBER']
target_url = jenkins_url + 'job/' + job_name + '/' + build_number + '/'
2014-01-06 20:58:00 +08:00
2015-03-10 15:44:02 +08:00
set_jenkins_job_description(pr_desc, target_url)
2014-10-14 17:31:53 +08:00
2015-03-03 16:36:42 +08:00
data = {"state": "pending", "target_url": target_url, "context": "Jenkins CI", "description": "Build started..."}
access_token = os.environ['GITHUB_ACCESS_TOKEN']
2015-03-03 16:36:42 +08:00
Headers = {"Authorization": "token " + access_token}
2014-01-02 17:09:49 +08:00
try:
2015-03-03 16:36:42 +08:00
requests.post(statuses_url, data=json.dumps(data), headers=Headers, proxies=proxyDict)
2014-01-02 17:09:49 +08:00
except:
traceback.print_exc()
2014-01-02 17:09:49 +08:00
2015-03-10 15:44:02 +08:00
def syntronize_remote_pr():
global workspace
global branch
global pr_num
#reset path to workspace root
os.system("cd " + workspace)
#pull latest code
os.system("git fetch origin " + branch)
2015-03-10 19:07:55 +08:00
os.system("git --version")
2015-03-03 16:36:42 +08:00
os.system("git checkout " + branch)
os.system("git merge origin/" + branch)
2014-01-07 15:41:18 +08:00
os.system("git branch -D pull" + str(pr_num))
#clean workspace
2014-10-14 17:31:53 +08:00
print "Before checkout: git clean -xdf -f"
os.system("git clean -xdf -f")
#fetch pull request to local repo
git_fetch_pr = "git fetch origin pull/" + str(pr_num) + "/head"
ret = os.system(git_fetch_pr)
if(ret != 0):
2015-03-11 00:59:21 +08:00
sys.exit(ret)
2014-10-14 17:31:53 +08:00
2015-03-03 16:36:42 +08:00
#checkout a new branch from v3 or v4-develop
git_checkout = "git checkout -b " + "pull" + str(pr_num)
os.system(git_checkout)
#merge pull reqeust head
2015-03-10 19:00:07 +08:00
subprocess.call("git merge --no-edit FETCH_HEAD", shell=True)
2015-03-10 18:42:26 +08:00
2015-03-10 18:24:38 +08:00
# The follow method is not working for Azure server
# p = os.popen('git merge --no-edit FETCH_HEAD')
# r = p.read()
# #check if merge fail
2015-03-10 19:00:07 +08:00
# if output.find('CONFLICT') > 0:
# print output
# raise Exception('There are conflicts in your PR!')
2014-10-14 17:31:53 +08:00
# After checkout a new branch, clean workspace again
2014-10-14 17:31:53 +08:00
print "After checkout: git clean -xdf -f"
os.system("git clean -xdf -f")
#update submodule
git_update_submodule = "git submodule update --init --force"
ret = os.system(git_update_submodule)
if(ret != 0):
2015-03-11 00:59:21 +08:00
sys.exit(ret)
2014-01-06 20:58:00 +08:00
2015-03-10 15:44:02 +08:00
def gen_scripting_bindings():
global branch
2014-01-08 16:21:11 +08:00
# Generate binding glue codes
2015-03-03 16:36:42 +08:00
if(branch == 'v3' or branch == 'v4-develop'):
2015-03-10 14:08:03 +08:00
ret = os.system("python tools/jenkins-scripts/slave-scripts/gen_jsb.py")
if(ret != 0):
2015-03-11 00:59:21 +08:00
sys.exit(ret)
2014-01-08 16:21:11 +08:00
2015-03-10 14:40:04 +08:00
2015-03-10 15:44:02 +08:00
def do_build_slaves():
global branch
global node_name
2014-10-14 17:31:53 +08:00
2015-03-10 15:44:02 +08:00
jenkins_script_path = "tools" + os.sep + "jenkins-scripts" + os.sep + "slave-scripts" + os.sep
2015-03-10 14:08:03 +08:00
2015-03-03 16:36:42 +08:00
if(branch == 'v3' or branch == 'v4-develop'):
2015-03-10 14:40:04 +08:00
slave_build_scripts = ""
if(node_name == 'android') or (node_name == 'android_bak'):
2015-03-10 15:44:02 +08:00
# patch_cpp_empty_test()
2015-03-10 14:40:04 +08:00
slave_build_scripts = jenkins_script_path + "android-build.sh"
elif(node_name == 'win32' or node_name == 'win32_win7' or node_name == 'win32_bak'):
2015-03-10 14:40:04 +08:00
if branch == 'v3':
slave_build_scripts = jenkins_script_path + "win32-vs2012-build.bat"
else:
slave_build_scripts = jenkins_script_path + "win32-vs2013-build.bat"
elif(node_name == 'windows-universal' or node_name == 'windows-universal_bak'):
2015-03-10 14:40:04 +08:00
if branch == 'v3':
slave_build_scripts = jenkins_script_path + "windows-universal-v3.bat"
else:
slave_build_scripts = jenkins_script_path + "windows-universal.bat"
elif(node_name == 'ios_mac' or node_name == 'ios' or node_name == 'ios_bak'):
2015-03-10 14:40:04 +08:00
slave_build_scripts = jenkins_script_path + "ios-build.sh"
elif(node_name == 'mac' or node_name == 'mac_bak'):
2015-03-10 14:40:04 +08:00
slave_build_scripts = jenkins_script_path + "mac-build.sh"
elif(node_name == 'linux_centos' or node_name == 'linux' or node_name == 'linux_bak'):
2015-03-10 14:40:04 +08:00
slave_build_scripts = jenkins_script_path + "linux-build.sh"
ret = os.system(slave_build_scripts)
2014-01-02 17:09:49 +08:00
#get build result
print "build finished and return " + str(ret)
2015-03-10 15:44:02 +08:00
return ret
2015-03-10 15:44:02 +08:00
def main():
global pr_num
global workspace
global branch
global node_name
global remote_build
if remote_build == 1:
2015-03-10 15:44:02 +08:00
send_notifies_to_github()
#syntronize local git repository with remote and merge the PR
syntronize_remote_pr()
#copy check_current_3rd_libs
check_current_3rd_libs(branch)
#generate jsb and luabindings
gen_scripting_bindings()
#start build jobs on each slave
ret = do_build_slaves()
2014-10-14 17:31:53 +08:00
exit_code = 1
2014-01-02 17:09:49 +08:00
if ret == 0:
exit_code = 0
else:
exit_code = 1
2014-10-14 17:31:53 +08:00
#clean workspace
if remote_build == 1:
2015-03-10 15:44:02 +08:00
os.system("cd " + workspace)
os.system("git reset --hard")
os.system("git clean -xdf -f")
os.system("git checkout " + branch)
os.system("git branch -D pull" + str(pr_num))
else:
print "local build, no need to cleanup"
2014-01-06 20:58:00 +08:00
return(exit_code)
2014-01-02 17:09:49 +08:00
# -------------- main --------------
if __name__ == '__main__':
2014-01-06 20:58:00 +08:00
sys_ret = 0
2014-10-14 17:31:53 +08:00
try:
2014-01-06 20:58:00 +08:00
sys_ret = main()
2014-01-02 17:09:49 +08:00
except:
traceback.print_exc()
2014-01-06 20:58:00 +08:00
sys_ret = 1
finally:
sys.exit(sys_ret)