Skip to content

Instantly share code, notes, and snippets.

@krzysztof-miemiec
Created October 26, 2018 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzysztof-miemiec/3cc6c36dd602b09c32a81f66944c332e to your computer and use it in GitHub Desktop.
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
#!/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