Skip to content

Instantly share code, notes, and snippets.

@scottrmercer
Forked from remmelt/bamboo-to-slack.py
Last active August 29, 2015 14:00
Show Gist options
  • Save scottrmercer/11302069 to your computer and use it in GitHub Desktop.
Save scottrmercer/11302069 to your computer and use it in GitHub Desktop.
#!/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}" "${bamboo.serviceusername}" "${bamboo.servicepassword}"
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])
username = str(sys.argv[4])
password = str(sys.argv[5])
buildStateUrl = 'http://YOUR_BAMBOO_URL/bamboo/rest/api/latest/result/{0}/latest.json?buildstate'.format(buildName)
r = requests.get(buildStateUrl, auth=(username, password))
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment