Created
November 25, 2016 13:53
-
-
Save maedoc/800c16cd91a9a64c2746b1a7cd983240 to your computer and use it in GitHub Desktop.
Notify Slack from Jenkins build script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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