It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Generalized post webhook from scriptrunner for bitbucketto trigger builds on any ci tool

We need a bit of guidance in creating a post commit webhook from bitbucket to trigger builds on ci tools . There are multiple ci tools such as teamcity ,Jenkins etc according to project we are trying to make a very generalized post commit webhook.

Just to explain the scenario:

---dev commits and pushes the code to bitbucket repo

---the post webhook will send the trigger event to ci tool(it can be any tool teamcity ,Jenkins,go etc)

--- once we get something on this , then we will script the trigger for other events as well such as pull requests, merges etc

1 answer

The particulars will depend on each API that you use, but ScriptRunner comes bundled with the HttpBuilder library, which makes making HTTP requests of external APIs pretty straightforward.

Here's a pretty basic example that I used to contact a local jenkins instance.


if ( == "myrepo") {
String jenkinsUrl = "http://localhost:8089/"
String userName = 'admin'
String password = 'admin'
String targetJob = 'job1'

def http = new HTTPBuilder(jenkinsUrl)
String uspass = "$userName:$password".bytes.encodeBase64()
http.setHeaders([Authorization: "Basic $uspass"])

def crumb
http.get(path: 'crumbIssuer/api/xml') { response, xml ->
crumb = xml.crumb.toString()
log.debug "Jenkins authorization crumb: $crumb"

http.headers += [
".crumb" : crumb, //different versions of Jenkins take different versions of this header
"Jenkins-Crumb": crumb

http.request(Method.POST, ContentType.TEXT) {
uri.path = "job/$targetJob/build"
query = [token: "62d07cb3d3571bebf23de309e88f1f3a6f7c93bd", crumb: crumb]

Naturally, you'll want to change the jenkinsUrl, repository name, target job name, and credentials used to match your use case. I'd also recommend that you store the jenkins credentials somewhere besides hard-coding them into your script (perhaps an environment variable or a credentials store on your server), then use the script to retrieve them.

I also assume that your Jenkins instance has CSRF protection enabled, since recent versions of Jenkins use it by default. If it's not enabled, you won't need the first get request to the crumbIssuer API, and you shouldn't need to set any authentication headers at all; the token should be sufficient to fire the Jenkins job.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder :  #!/bin/bash source "$(dirname "$0")/" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

4,142 views 4 22
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you