Created
October 26, 2018 07:38
-
-
Save krzysztof-miemiec/3cc6c36dd602b09c32a81f66944c332e to your computer and use it in GitHub Desktop.
Simple Slack notifications sample that can be used in GitLab, when you want to send notifications under certain conditions
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
#!/usr/bin/env bash | |
# Simple Slack notifications that can be used in GitLab, when you want to send notifications under certain conditions | |
# | |
# SLACK_HOOK - url to slack IncomingWebhook | |
# ENVIRONMENT - app name | |
# CI_* - env vars from GitLab | |
# | |
# --error flag sends the error message. | |
set -e | |
ARG=$1 | |
if [ "${ARG}" == "--error" ]; then | |
echo Sending error message to Slack | |
curl -X POST \ | |
${SLACK_HOOK} \ | |
-H 'Content-Type: application/json' \ | |
-d '{"attachments": [ | |
{ | |
"fallback": "Deployment of '"${ENVIRONMENT}"' app failed! Check what went wrong here: '"${CI_PIPELINE_URL}"'", | |
"pretext": ":disappointed: Sorry to say this, but...", | |
"title": "'"${ENVIRONMENT}"'", | |
"title_link": "'"${CI_PROJECT_URL}"'/-/jobs/'"${CI_JOB_ID}"'", | |
"text": "Deployment of '"${ENVIRONMENT}"' app failed! Click on the link to see what went wrong.", | |
"color": "#992222" | |
}, | |
{ | |
"fallback": "Commit: '"${CI_COMMIT_SHA}"'", | |
"text": "*Commit* `'"${CI_COMMIT_SHA}"'`\n*Message*\n'"${CI_COMMIT_MESSAGE}"'", | |
"color": "#333333" | |
} | |
] | |
}' | |
else | |
echo Sending success message to Slack | |
curl -X POST \ | |
${SLACK_HOOK} \ | |
-H 'Content-Type: application/json' \ | |
-d '{"attachments": [ | |
{ | |
"fallback": "Deployment of '"${ENVIRONMENT}"' app went well! It'\''s available here: '"${SLACK_URL}"'", | |
"pretext": ":tada: Hey, I'\''ve got some good news for you!", | |
"title": "'"${ENVIRONMENT}"'", | |
"title_link": "'"${SLACK_URL}"'", | |
"text": "Deployment of '"${ENVIRONMENT}"' app went well! Click on the link to see it.", | |
"color": "#7CD197" | |
}, | |
{ | |
"fallback": "Commit: '"${CI_COMMIT_SHA}"'", | |
"text": "*Commit* `'"${CI_COMMIT_SHA}"'`\n*Message*\n'"${CI_COMMIT_MESSAGE}"'", | |
"color": "#333333" | |
} | |
] | |
}' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment