From 9f7021fd6c0795ad79ce2c7122519f1970d3689e Mon Sep 17 00:00:00 2001 From: lm Date: Tue, 22 Apr 2014 11:41:14 +0800 Subject: [PATCH 1/3] [Jenkins][ci skip] Add [ci release xxx] command --- tools/jenkins-scripts/ci-release-test.py | 162 +++++++++++++++++++ tools/jenkins-scripts/job-comment-trigger.py | 6 + 2 files changed, 168 insertions(+) create mode 100644 tools/jenkins-scripts/ci-release-test.py diff --git a/tools/jenkins-scripts/ci-release-test.py b/tools/jenkins-scripts/ci-release-test.py new file mode 100644 index 0000000000..8105dd0f5b --- /dev/null +++ b/tools/jenkins-scripts/ci-release-test.py @@ -0,0 +1,162 @@ +#CI release for Jenkins + +import json +import os +import re +import urllib2 +import urllib +import base64 +import requests +import sys +import traceback +import platform +import subprocess +import codecs + +#set Jenkins build description using submitDescription to mock browser behavior +#TODO: need to set parent build description +def set_description(desc, url): + req_data = urllib.urlencode({'description': desc}) + req = urllib2.Request(url + 'submitDescription', req_data) + #print(os.environ['BUILD_URL']) + 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) + try: + urllib2.urlopen(req) + except: + traceback.print_exc() +def main(): + #get payload from os env + payload_str = os.environ['payload'] + payload_str = payload_str.decode('utf-8','ignore') + #parse to json obj + payload = json.loads(payload_str) + + #get pull number + tag = payload['tag'] + print 'tag:' + tag + + url = payload['html_url'] + print "url:" + url + pr_desc = '

' + tag + ' is release' + '

' + + #get statuses url + statuses_url = payload['statuses_url'] + + #get pr target branch + branch = payload['branch'] + + #set parent build description + 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 + '/' + + set_description(pr_desc, target_url) + + #set commit status to pending + data = {"state":"pending", "target_url":target_url} + access_token = os.environ['GITHUB_ACCESS_TOKEN'] + Headers = {"Authorization":"token " + access_token} + + try: + requests.post(statuses_url, data=json.dumps(data), headers=Headers) + except: + traceback.print_exc() + + #pull origin develop + os.system('git reset --hard') + os.system("git clean -xdf -f") + os.system("git checkout develop") + os.system("git clean -xdf -f") + ret = os.system('git pull origin develop') + if(ret != 0): + return(2) + + #update submodule + git_update_submodule = "git submodule update --init --force" + ret = os.system(git_update_submodule) + if(ret != 0): + return(2) + + # Generate binding glue codes + if(branch == 'develop'): + ret = os.system("python tools/jenkins-scripts/gen_jsb.py") + if(ret != 0): + return(1) + + #make temp dir + print "current dir is: " + os.environ['WORKSPACE'] + os.system("cd " + os.environ['WORKSPACE']); + os.mkdir("android_build_objs") + #add symbol link + PROJECTS=["cpp-empty-test", "cpp-tests"] + + print platform.system() + if(platform.system() == 'Darwin'): + for item in PROJECTS: + cmd = "ln -s " + os.environ['WORKSPACE']+"/android_build_objs/ " + os.environ['WORKSPACE']+"/tests/"+item+"/proj.android/obj" + os.system(cmd) + elif(platform.system() == 'Windows'): + for item in PROJECTS: + p = item.replace("/", os.sep) + cmd = "mklink /J "+os.environ['WORKSPACE']+os.sep+"tests"+os.sep +p+os.sep+"proj.android"+os.sep+"obj " + os.environ['WORKSPACE']+os.sep+"android_build_objs" + print cmd + os.system(cmd) + + #build + #TODO: add android-linux build + #TODO: add mac build + node_name = os.environ['NODE_NAME'] + build_mode = ['debug', 'release'] + tests_dirs = ['tests/cpp-empty-test/proj.android', + 'tests/cpp-tests/proj.android', + 'tests/lua-empty-test/project/proj.android', + 'tests/lua-tests/project/proj.android'] + tests_names = ['cpp-empty-test', 'cpp-tests', 'lua-empty-test', 'lua-tests'] + remote_home = os.environ['REMOTE_HOME'] + for mode in build_mode: + if(branch == 'develop'): + if(node_name == 'android_mac') or (node_name == 'android_win7'): + print "Start build android..." + ret = os.system("python build/android-build.py -b " + mode + " -n -j10 all") + # create and save apk + if(ret == 0): + os.system('android update project -p cocos/2d/platform/android/java/ -t android-13') + for i, test in enumerate(tests_dirs): + os.system('android update project -p ' + test + ' -t android-13') + local_apk = test + '/' + tests_names[i] + '.apk' + remote_dir = '/data/download/' + tag + '/' + remote_apk = remote_dir + tests_names[i] + '-' + tag + '-' + mode + '.apk' + os.system('ant debug -f ' + test + '/build.xml' + ' -Dout.final.file=$PWD/' + local_apk) + os.system('ssh ' + remote_home + ' "mkdir -p ' + remote_dir + '"') + os.system('scp ' + local_apk + ' ' + remote_home + ':' + remote_apk) + + #get build result + print "build finished and return " + str(ret) + + exit_code = 1 + if ret == 0: + exit_code = 0 + else: + exit_code = 1 + + #clean workspace + os.system("cd " + os.environ['WORKSPACE']) + os.system("git reset --hard") + os.system("git clean -xdf -f") + os.system("git checkout develop") + + return(exit_code) + +# -------------- main -------------- +if __name__ == '__main__': + sys_ret = 0 + try: + sys_ret = main() + except: + traceback.print_exc() + sys_ret = 1 + finally: + sys.exit(sys_ret) diff --git a/tools/jenkins-scripts/job-comment-trigger.py b/tools/jenkins-scripts/job-comment-trigger.py index 9b3a802cd5..c33ac8eeae 100644 --- a/tools/jenkins-scripts/job-comment-trigger.py +++ b/tools/jenkins-scripts/job-comment-trigger.py @@ -93,6 +93,12 @@ def main(): job_trigger_url = os.environ['JOB_PULL_REQUEST_BUILD_TRIGGER_URL'] if('emptytest' in ciOper): job_trigger_url = os.environ['JOB_EMPTYTEST_TRIGGER_URL'] + if('release' in ciOper): + searchTag = re.search('\[ci release (.*)\]', ciOper) + if searchTag: + ci_tag = searchTag.group(1) + payload_forword['tag'] = ci_tag + job_trigger_url = os.environ['JOB_RELEASE_TRIGGER_URL'] if searchConsole: consoleOper = searchConsole.group() job_trigger_url = os.environ['JOB_CONSOLE_TEST_TRIGGER_URL'] From 7005d6664dbe2a7c9f3d371cb6771b6b1cbfef05 Mon Sep 17 00:00:00 2001 From: lm Date: Tue, 22 Apr 2014 12:36:40 +0800 Subject: [PATCH 2/3] [Jenkins][ci skip] Clean workspace before debug build or release build --- tools/jenkins-scripts/ci-release-test.py | 53 +++++++++++++----------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/tools/jenkins-scripts/ci-release-test.py b/tools/jenkins-scripts/ci-release-test.py index 8105dd0f5b..8195ed3d1b 100644 --- a/tools/jenkins-scripts/ci-release-test.py +++ b/tools/jenkins-scripts/ci-release-test.py @@ -26,6 +26,27 @@ def set_description(desc, url): urllib2.urlopen(req) except: traceback.print_exc() + +def make_temp_dir(): + #make temp dir + print "current dir is: " + os.environ['WORKSPACE'] + os.system("cd " + os.environ['WORKSPACE']); + os.mkdir("android_build_objs") + #add symbol link + PROJECTS=["cpp-empty-test", "cpp-tests"] + + print platform.system() + if(platform.system() == 'Darwin'): + for item in PROJECTS: + cmd = "ln -s " + os.environ['WORKSPACE']+"/android_build_objs/ " + os.environ['WORKSPACE']+"/tests/"+item+"/proj.android/obj" + os.system(cmd) + elif(platform.system() == 'Windows'): + for item in PROJECTS: + p = item.replace("/", os.sep) + cmd = "mklink /J "+os.environ['WORKSPACE']+os.sep+"tests"+os.sep +p+os.sep+"proj.android"+os.sep+"obj " + os.environ['WORKSPACE']+os.sep+"android_build_objs" + print cmd + os.system(cmd) + def main(): #get payload from os env payload_str = os.environ['payload'] @@ -80,31 +101,6 @@ def main(): if(ret != 0): return(2) - # Generate binding glue codes - if(branch == 'develop'): - ret = os.system("python tools/jenkins-scripts/gen_jsb.py") - if(ret != 0): - return(1) - - #make temp dir - print "current dir is: " + os.environ['WORKSPACE'] - os.system("cd " + os.environ['WORKSPACE']); - os.mkdir("android_build_objs") - #add symbol link - PROJECTS=["cpp-empty-test", "cpp-tests"] - - print platform.system() - if(platform.system() == 'Darwin'): - for item in PROJECTS: - cmd = "ln -s " + os.environ['WORKSPACE']+"/android_build_objs/ " + os.environ['WORKSPACE']+"/tests/"+item+"/proj.android/obj" - os.system(cmd) - elif(platform.system() == 'Windows'): - for item in PROJECTS: - p = item.replace("/", os.sep) - cmd = "mklink /J "+os.environ['WORKSPACE']+os.sep+"tests"+os.sep +p+os.sep+"proj.android"+os.sep+"obj " + os.environ['WORKSPACE']+os.sep+"android_build_objs" - print cmd - os.system(cmd) - #build #TODO: add android-linux build #TODO: add mac build @@ -117,7 +113,14 @@ def main(): tests_names = ['cpp-empty-test', 'cpp-tests', 'lua-empty-test', 'lua-tests'] remote_home = os.environ['REMOTE_HOME'] for mode in build_mode: + os.system('git reset --hard') + os.system("git clean -xdf -f") + make_temp_dir() if(branch == 'develop'): + # Generate binding glue codes + ret = os.system("python tools/jenkins-scripts/gen_jsb.py") + if(ret != 0): + return(1) if(node_name == 'android_mac') or (node_name == 'android_win7'): print "Start build android..." ret = os.system("python build/android-build.py -b " + mode + " -n -j10 all") From 2c55c740ba55091f674b1634bea20148cf34b00d Mon Sep 17 00:00:00 2001 From: lm Date: Tue, 22 Apr 2014 14:31:56 +0800 Subject: [PATCH 3/3] [Jenkins][ci skip] Git fetch tag --- tools/jenkins-scripts/ci-release-test.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/jenkins-scripts/ci-release-test.py b/tools/jenkins-scripts/ci-release-test.py index 8195ed3d1b..068875fcce 100644 --- a/tools/jenkins-scripts/ci-release-test.py +++ b/tools/jenkins-scripts/ci-release-test.py @@ -90,10 +90,21 @@ def main(): os.system('git reset --hard') os.system("git clean -xdf -f") os.system("git checkout develop") + os.system("git branch -D " + tag) os.system("git clean -xdf -f") - ret = os.system('git pull origin develop') + #fetch tag to local repo + git_fetch_pr = "git fetch origin tag " + tag + ret = os.system(git_fetch_pr) if(ret != 0): return(2) + + #checkout + git_checkout = "git checkout -b " + tag + " FETCH_HEAD" + os.system(git_checkout) + + # After checkout a new branch, clean workspace again + print "After checkout: git clean -xdf -f" + os.system("git clean -xdf -f") #update submodule git_update_submodule = "git submodule update --init --force"