2013-12-30 21:11:32 +08:00
|
|
|
#Github pull reqest builder for Jenkins
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
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
|
2014-01-06 20:58:00 +08:00
|
|
|
import platform
|
2014-01-24 14:40:15 +08:00
|
|
|
import subprocess
|
2013-12-30 21:11:32 +08:00
|
|
|
|
|
|
|
#set Jenkins build description using submitDescription to mock browser behavior
|
|
|
|
#TODO: need to set parent build description
|
2014-01-06 20:58:00 +08:00
|
|
|
def set_description(desc, url):
|
2013-12-30 21:11:32 +08:00
|
|
|
req_data = urllib.urlencode({'description': desc})
|
2014-01-06 20:58:00 +08:00
|
|
|
req = urllib2.Request(url + 'submitDescription', req_data)
|
2013-12-31 18:29:32 +08:00
|
|
|
#print(os.environ['BUILD_URL'])
|
2013-12-30 21:11:32 +08:00
|
|
|
req.add_header('Content-Type', 'application/x-www-form-urlencoded')
|
|
|
|
base64string = base64.encodestring(os.environ['JENKINS_ADMIN']+ ":" + os.environ['JENKINS_ADMIN_PW']).replace('\n', '')
|
|
|
|
req.add_header("Authorization", "Basic " + base64string)
|
2014-01-02 17:09:49 +08:00
|
|
|
try:
|
|
|
|
urllib2.urlopen(req)
|
|
|
|
except:
|
2014-01-04 16:25:31 +08:00
|
|
|
traceback.print_exc()
|
2014-01-02 17:09:49 +08:00
|
|
|
def main():
|
|
|
|
#get payload from os env
|
|
|
|
payload_str = os.environ['payload']
|
2014-01-17 17:13:33 +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
|
|
|
|
2014-01-02 17:09:49 +08:00
|
|
|
pr = payload['pull_request']
|
2014-01-09 12:47:46 +08:00
|
|
|
|
2014-01-02 17:09:49 +08:00
|
|
|
url = pr['html_url']
|
|
|
|
print "url:" + url
|
2014-01-06 20:58:00 +08:00
|
|
|
pr_desc = '<h3><a href='+ url + '> pr#' + str(pr_num) + ' is '+ action +'</a></h3>'
|
|
|
|
|
|
|
|
|
2013-12-30 21:11:32 +08:00
|
|
|
|
2014-01-02 17:09:49 +08:00
|
|
|
#get statuses url
|
|
|
|
statuses_url = pr['statuses_url']
|
|
|
|
|
|
|
|
#get pr target branch
|
2014-01-02 19:08:22 +08:00
|
|
|
branch = pr['base']['ref']
|
2014-01-02 17:09:49 +08:00
|
|
|
|
|
|
|
#set commit status to pending
|
2014-01-23 00:21:58 +08:00
|
|
|
#target_url = os.environ['BUILD_URL']
|
|
|
|
jenkins_url = os.environ['JENKINS_URL']
|
|
|
|
job_name = os.environ['JOB_NAME'].split('/')[0]
|
|
|
|
build_number = os.environ['BUILD_NUMBER']
|
2014-01-24 12:05:31 +08:00
|
|
|
target_url = jenkins_url + 'job/' + job_name + '/' + build_number + '/'
|
2014-01-06 20:58:00 +08:00
|
|
|
|
|
|
|
set_description(pr_desc, target_url)
|
|
|
|
|
2014-01-16 16:22:04 +08:00
|
|
|
if(action == 'closed'):
|
2014-01-06 20:58:00 +08:00
|
|
|
print 'pull request #' + str(pr_num) + ' is '+action+', no build triggered'
|
|
|
|
return(0)
|
2014-01-09 12:47:46 +08:00
|
|
|
|
|
|
|
r = requests.get(pr['url']+"/commits")
|
|
|
|
commits = r.json()
|
|
|
|
last_commit = commits[len(commits)-1]
|
|
|
|
message = last_commit['commit']['message']
|
2014-01-06 20:58:00 +08:00
|
|
|
|
2014-01-09 12:47:46 +08:00
|
|
|
pattern = re.compile("\[ci(\s+)skip\]", re.I)
|
|
|
|
result = pattern.search(message)
|
|
|
|
if result is not None:
|
|
|
|
print 'skip build for pull request #' + str(pr_num)
|
|
|
|
return(0)
|
|
|
|
|
2014-01-02 17:09:49 +08:00
|
|
|
data = {"state":"pending", "target_url":target_url}
|
2014-01-04 16:25:31 +08:00
|
|
|
access_token = os.environ['GITHUB_ACCESS_TOKEN']
|
|
|
|
Headers = {"Authorization":"token " + access_token}
|
2014-01-02 17:09:49 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
requests.post(statuses_url, data=json.dumps(data), headers=Headers)
|
|
|
|
except:
|
2014-01-04 16:25:31 +08:00
|
|
|
traceback.print_exc()
|
2014-01-02 17:09:49 +08:00
|
|
|
|
2014-01-03 10:59:37 +08:00
|
|
|
#reset path to workspace root
|
|
|
|
os.system("cd " + os.environ['WORKSPACE']);
|
2014-01-07 15:41:18 +08:00
|
|
|
os.system("git checkout develop")
|
|
|
|
os.system("git branch -D pull" + str(pr_num))
|
|
|
|
#clean workspace
|
|
|
|
print "git clean -xdf"
|
|
|
|
os.system("git clean -xdf")
|
|
|
|
|
2014-01-08 16:21:11 +08:00
|
|
|
|
2014-01-03 10:59:37 +08:00
|
|
|
#fetch pull request to local repo
|
|
|
|
git_fetch_pr = "git fetch origin pull/" + str(pr_num) + "/head"
|
|
|
|
os.system(git_fetch_pr)
|
|
|
|
|
|
|
|
#checkout
|
|
|
|
git_checkout = "git checkout -b " + "pull" + str(pr_num) + " FETCH_HEAD"
|
|
|
|
os.system(git_checkout)
|
|
|
|
|
|
|
|
#update submodule
|
|
|
|
git_update_submodule = "git submodule update --init --force"
|
|
|
|
os.system(git_update_submodule)
|
2014-01-06 20:58:00 +08:00
|
|
|
|
2014-01-08 16:21:11 +08:00
|
|
|
# Generate binding glue codes
|
|
|
|
if(platform.system() == 'Darwin'):
|
|
|
|
os.system("tools/jenkins-scripts/gen_jsb.sh")
|
2014-01-23 15:38:26 +08:00
|
|
|
elif(platform.system() == 'Windows'):
|
2014-01-23 19:02:19 +08:00
|
|
|
os.chdir("tools/jenkins-scripts")
|
2014-01-23 19:05:49 +08:00
|
|
|
os.system("gen_jsb_win32.bat")
|
2014-01-24 09:50:40 +08:00
|
|
|
os.chdir("../..")
|
2014-01-08 16:21:11 +08:00
|
|
|
|
2014-01-07 15:41:18 +08:00
|
|
|
#make temp dir
|
|
|
|
print "current dir is" + os.environ['WORKSPACE']
|
|
|
|
os.system("cd " + os.environ['WORKSPACE']);
|
|
|
|
os.mkdir("android_build_objs")
|
2014-01-06 20:58:00 +08:00
|
|
|
#add symbol link
|
|
|
|
PROJECTS=["Cpp/HelloCpp","Cpp/TestCpp","Cpp/SimpleGame","Cpp/AssetsManagerTest",
|
|
|
|
"Javascript/TestJavascript","Javascript/CocosDragonJS","Javascript/CrystalCraze",
|
|
|
|
"Javascript/MoonWarriors","Javascript/WatermelonWithMe","Lua/HelloLua","Lua/TestLua"]
|
2014-01-07 15:41:18 +08:00
|
|
|
print platform.system()
|
|
|
|
if(platform.system() == 'Darwin'):
|
2014-01-06 20:58:00 +08:00
|
|
|
for item in PROJECTS:
|
2014-01-07 15:41:18 +08:00
|
|
|
cmd = "ln -s " + os.environ['WORKSPACE']+"/android_build_objs/ " + os.environ['WORKSPACE']+"/samples/"+item+"/proj.android/obj"
|
|
|
|
os.system(cmd)
|
|
|
|
elif(platform.system() == 'Windows'):
|
2014-01-06 20:58:00 +08:00
|
|
|
for item in PROJECTS:
|
2014-01-07 16:27:23 +08:00
|
|
|
p = item.replace("/", os.sep)
|
|
|
|
cmd = "mklink /J "+os.environ['WORKSPACE']+os.sep+"samples"+os.sep +p+os.sep+"proj.android"+os.sep+"obj " + os.environ['WORKSPACE']+os.sep+"android_build_objs"
|
|
|
|
print cmd
|
|
|
|
os.system(cmd)
|
2014-01-06 20:58:00 +08:00
|
|
|
|
2014-01-02 17:09:49 +08:00
|
|
|
#build
|
|
|
|
#TODO: support android-mac build currently
|
|
|
|
#TODO: add android-windows7 build
|
|
|
|
#TODO: add android-linux build
|
|
|
|
#TODO: add ios build
|
|
|
|
#TODO: add mac build
|
|
|
|
#TODO: add win32 build
|
2014-01-24 14:40:15 +08:00
|
|
|
node_name = os.environ['NODE_NAME']
|
2014-01-02 17:09:49 +08:00
|
|
|
if(branch == 'develop'):
|
2014-01-24 14:40:15 +08:00
|
|
|
if(node_name == 'android_mac') or (node_name == 'android_win7'):
|
|
|
|
ret = os.system("python build/android-build.py -n -j10 all")
|
|
|
|
elif(node_name == 'win32_win7'):
|
|
|
|
ret = subprocess.call('"%VS110COMNTOOLS%..\IDE\devenv.com" "build\cocos2d-win32.vc2012.sln" /Build "Debug|Win32"', shell=True)
|
2014-01-02 17:09:49 +08:00
|
|
|
elif(branch == 'master'):
|
|
|
|
ret = os.system("samples/Cpp/TestCpp/proj.android/build_native.sh")
|
|
|
|
|
|
|
|
#get build result
|
|
|
|
print "build finished and return " + str(ret)
|
2014-01-23 00:21:58 +08:00
|
|
|
|
2014-01-04 16:25:31 +08:00
|
|
|
exit_code = 1
|
2014-01-02 17:09:49 +08:00
|
|
|
if ret == 0:
|
|
|
|
exit_code = 0
|
|
|
|
else:
|
|
|
|
exit_code = 1
|
2014-01-23 00:21:58 +08:00
|
|
|
|
2013-12-31 18:29:32 +08:00
|
|
|
#clean workspace
|
|
|
|
os.system("cd " + os.environ['WORKSPACE']);
|
|
|
|
os.system("git checkout develop")
|
|
|
|
os.system("git branch -D pull" + str(pr_num))
|
2013-12-30 21:11:32 +08:00
|
|
|
|
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-01-02 17:09:49 +08:00
|
|
|
try:
|
2014-01-06 20:58:00 +08:00
|
|
|
sys_ret = main()
|
2014-01-02 17:09:49 +08:00
|
|
|
except:
|
2014-01-04 16:25:31 +08:00
|
|
|
traceback.print_exc()
|
2014-01-06 20:58:00 +08:00
|
|
|
sys_ret = 1
|
|
|
|
finally:
|
|
|
|
sys.exit(sys_ret)
|