Skip to content

Instantly share code, notes, and snippets.

@jacoor
Last active April 23, 2018 13:33
Show Gist options
  • Save jacoor/65855b4e4a5bf18018abaa90ae077f58 to your computer and use it in GitHub Desktop.
Save jacoor/65855b4e4a5bf18018abaa90ae077f58 to your computer and use it in GitHub Desktop.
Publishing to slack #channel as jenkins
"""
to set up auth token first authorize jenkins as an slack app and install
https://wiki.jenkins.io/display/JENKINS/Slack+Plugin
Afterwards, use https://plugins.jenkins.io/credentials-binding, which is most likely available in your jenkins install
and use the instructions from there to share credentials with job. Accessing credentials in python requires: os.environ['xxx']
"""
import os
import requests
import json
requests.post(' https://domain.slack.com/services/hooks/jenkins-ci/'+os.environ["slack-auth-token"], data =json.dumps({"channel": "portal","text":"my message"}))
#!/bin/bash -ex
# send notification about commits pushed to $1 branch
#!/bin/bash -ex
message="Following commits were just pushed to $1 branch:"
changelog=$(git log ${GIT_PREVIOUS_SUCCESSFUL_COMMIT}..HEAD)
fullmessage="$message $changelog"
curl -X POST -H "Content-type: application/json" \
https://fraudnet.slack.com/services/hooks/jenkins-ci/${SLACK_AUTH_TOKEN} \
--data-binary @- << EOF
{
"channel": "portal",
"text": " ${fullmessage//\"/$q_mid}"
}
EOF
@jacoor
Copy link
Author

jacoor commented Apr 23, 2018

If other ways of sending messages to slack are required, check their documentation for incoming webhooks. This allows to send basically any info to slack as "webhook".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment