Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Post an Atlassian Bamboo build result to Slack
#!/usr/bin/python
"""
Create a stage in your project, make it the last stage.
Make a task in the stage with this inline script:
#! /bin/bash
/some/path/bamboo-to-slack.py "${bamboo.planKey}" "${bamboo.buildPlanName}" "${bamboo.buildResultsUrl}"
Remember to make a channel with a Service Integration "Incoming WebHooks." The Slacker site will tell you the correct URL to put in the script below.
Requirements for this Python script: Requests (pip install requests, http://docs.python-requests.org/)
"""
import sys
import requests
import json
buildName = str(sys.argv[1])
jobName = str(sys.argv[2])
buildUrl = str(sys.argv[3])
buildStateUrl = 'http://YOUR_BAMBOO_URL/bamboo/rest/api/latest/result/{0}/latest.json?buildstate'.format(buildName)
r = requests.get(buildStateUrl)
buildState = r.json()['buildState']
if buildState != 'Successful':
message = "Job \"{0}\" not successful! State: {1}. See {2}".format(jobName, buildState, buildUrl)
payload = {'channel': '#YOUR_CHANNEL', 'username': 'Bamboo', 'text': message}
r = requests.post('https://YOUR_COMPANY.slack.com/services/hooks/incoming-webhook?token=YOUR_TOKEN_HERE', data=json.dumps(payload))
print "Slacker output: {0}".format(r.text)
@scottrmercer

This comment has been minimized.

Copy link

commented Apr 25, 2014

Nice. Thank you!

@mpseay

This comment has been minimized.

Copy link

commented Feb 19, 2015

Wouldn't this grab the previous build state as you're still in the context of the current build?

@alkalinecoffee

This comment has been minimized.

Copy link

commented Apr 30, 2015

Agreed with @mpseay. This will pull the previous build--not the current one. It doesn't appear that you can get the test results of the current build while it's still in progress.

@awinder

This comment has been minimized.

Copy link

commented May 11, 2015

I'm not saying this is the greatest idea in the world, but one way to deal with that is in your script task, schedule the job to run in a detached environment after a few seconds as the last final task.

nohup /bin/bash -c 'sleep 10; /some/path/bamboo-to-slack.py ${bamboo.planKey} ${bamboo.buildPlanName}  ${bamboo.buildResultsUrl} > /dev/null 2>&1'
@jovton

This comment has been minimized.

Copy link

commented Feb 12, 2016

@awinder, any idea what a windows command would look like?

@pswartout

This comment has been minimized.

Copy link

commented Nov 3, 2016

@remmelt will this work on cloud based bamboo? (going to presume not but thought I would check)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.