Trigger Jenkins via Webhook REST API
Currently, you can only remotely trigger builds that are connected to a pull request.
API
Use Webhook to Jenkins to trigger Jenkins via using its REST API via the following endpoint:
/rest/jenkins/latest/projects/{BITBUCKET_PROJECT_SLUG}/repos/{BITBUCKET_REPOSITORY_SLUG}/triggerJenkins?pr_id={PULL_REQUEST_ID}
We've created a sample python script that updates the settings for you. Refer to the Python example.
The REST API requires the following inputs
BITBUCKET_PROJECT_SLUG | The Bitbucket project string id |
BITBUCKET_REPOSITORY_SLUG | The Bitbucket repository string id |
PULL_REQUEST_ID | The ID of the pull request |
Python Example
#!/usr/bin/python
import httplib
import sys
import json
import argparse
from urlparse import urlparse
from base64 import b64encode
parser = argparse.ArgumentParser(description='Sample script to trigger jenkins via plugin api.')
parser.add_argument('--bitbucket-url', type=str, help='Url for Bitbucket instance.',
default='http://localhost:7990/bitbucket', dest='bitbucket_url')
parser.add_argument('--username', type=str, help='Username for user with access to repository',
default='admin')
parser.add_argument('--password', type=str, help='Password for user with access to repository',
default='admin')
parser.add_argument('--project', type=str, help='Project key where repository is located',
default='PROJECT_1')
parser.add_argument('--repository', type=str, help='Repository key for the instance',
default='rep_1')
parser.add_argument('--pull-request-id', type=str, help='Pull request ID.',
default='1', dest='pull_request_id')
args = parser.parse_args()
bb_url = args.bitbucket_url
username = args.username
password = args.password
project_key = args.project
repository_slug = args.repository
pull_request_id = args.pull_request_id
end_point = "/rest/jenkins/latest/projects/{}/repos/{}/triggerJenkins?pr_id={}".format(project_key, repository_slug, pull_request_id)
url_obj = urlparse(bb_url)
headers = {
'authorization': "Basic " + b64encode(username + ':' + password),
'Content-Type': 'application/json'
}
connection = httplib.HTTPConnection(url_obj.hostname, url_obj.port)
try:
connection.request("POST", url_obj.path + end_point, headers=headers)
r1 = connection.getresponse()
print r1.status, r1.reason, r1.getheaders()
data1 = r1.read()
print data1
finally:
connection.close()