axmol/tools/jenkins-scripts/post-build.py

37 lines
1.1 KiB
Python
Raw Normal View History

2014-01-23 00:21:58 +08:00
import os
import json
import requests
import jenkinsapi
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.constants import STATUS_SUCCESS
2014-01-23 00:21:58 +08:00
payload_str = os.environ['payload']
2014-01-23 00:21:58 +08:00
payload_str = payload_str.decode('utf-8','ignore')
#parse to json obj
payload = json.loads(payload_str)
#pr = payload['pull_request']
url = payload['html_url']
print "build pr:" + url
2014-01-23 00:21:58 +08:00
#get statuses url
statuses_url = payload['statuses_url']
2014-01-23 00:21:58 +08:00
J = Jenkins(os.environ['JENKINS_URL'])
target_url = os.environ['BUILD_URL']
build_number = int(os.environ['BUILD_NUMBER'])
2014-01-23 00:21:58 +08:00
data = {"state":"pending", "target_url":target_url}
access_token = os.environ['GITHUB_ACCESS_TOKEN']
Headers = {"Authorization":"token " + access_token}
result = J[os.environ['JOB_NAME']].get_build(build_number).get_status()
2014-01-23 00:21:58 +08:00
if(result == STATUS_SUCCESS):
2014-01-23 00:21:58 +08:00
data['state'] = "success"
else:
2014-01-23 12:57:18 +08:00
data['state'] = "failure"
2014-01-23 00:21:58 +08:00
http_proxy = ''
if(os.environ.has_key('HTTP_PROXY')):
http_proxy = os.environ['HTTP_PROXY']
proxyDict = {'http':http_proxy,'https':http_proxy}
requests.post(statuses_url, data=json.dumps(data), headers=Headers, proxies = proxyDict)