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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

22,452 views 26 12
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you