Skip to content

Instantly share code, notes, and snippets.

@maedoc
Created November 25, 2016 13:53
Show Gist options
  • Save maedoc/800c16cd91a9a64c2746b1a7cd983240 to your computer and use it in GitHub Desktop.
Save maedoc/800c16cd91a9a64c2746b1a7cd983240 to your computer and use it in GitHub Desktop.
Notify Slack from Jenkins build script
# don't stop on error so we can catch and notify
set +x +e
# custom bot api token
export SLACK_API_SECRET=foo-bar-baz
# notify start
python<<EOF
import slackclient as sc, os
c = sc.SlackClient(os.environ['SLACK_API_SECRET'])
text = 'Building {GIT_BRANCH} {GIT_COMMIT}'.format(**os.environ)
c.api_call("chat.postMessage", channel="#ci", text=text, username='Jenkins')
EOF
# run your tests
python test.py
# notify result
OK=$? python<<EOF
import slackclient as sc, os
c = sc.SlackClient(os.environ['SLACK_API_SECRET'])
text = "Build {GIT_BRANCH} {GIT_COMMIT} has %s\n{BUILD_URL}"
text = text.format(**os.environ)
text %= "succeeded! :tada:" if os.environ['OK'] == '0' else "failed! :thunder_cloud_and_rain:"
c.api_call("chat.postMessage", channel="#ci", text=text, username='Jenkins')
EOF
# tell jenkins about status
exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment