I'm trying to configure custom slack notification from GitLab but keep getting error in GitlabCI. I'm wondering what I'm doing wrong? Maybe that's an error in my function? Btw I've tested the weebhook itself and it's valid and working as expected
This is a piece from my .gitlab-ci-.yaml
ENVIRONMENT_NAME: "staging"
extends: .Deploy_image
only:
- master
after_script:
- if [ ${CI_JOB_STATUS} == "success" ]; then EXIT_STATUS=0; else EXIT_STATUS=1; fi
- envsubst </tmp/${CI_PIPELINE_ID}/${SCRIPT_PATH}/slackscript.sh> "slackscript.sh"
- source ./slackscript.sh; share_slack_update_deploy
and the script itself:
#!/bin/bash
set -euo pipefail
FAILURE=1
SUCCESS=0
SLACKWEBHOOKURL="my webhook url"
function print_slack_summary_deploy() {
local slack_msg_header
local slack_msg_body
local slack_channel
# Populate header and define slack channels
slack_msg_header=":x: *Deploy to ${ENVIRONMENT_NAME} failed*"
if [[ "${EXIT_STATUS}" == "${SUCCESS}" ]]; then
slack_msg_header=":heavy_check_mark: *Deploy to ${ENVIRONMENT_NAME} succeeded*"
slack_channel="channel-name"
fi
cat <<-SLACK
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${slack_msg_header}"
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Stage:*\nDeploy"
},
{
"type": "mrkdwn",
"text": "*Pushed By:*\n${GITLAB_USER_NAME}"
},
{
"type": "mrkdwn",
"text": "*Job URL:*\nGITLAB_REPO_URL/${CI_JOB_ID}"
},
{
"type": "mrkdwn",
"text": "*Commit Message:*\nGITLAB_REPO_URL/${CI_COMMIT_MESSAGE}"
{
"type": "mrkdwn",
"text": "*Commit URL:*\nGITLAB_REPO_URL$(git rev-parse HEAD)"
},
{
"type": "mrkdwn",
"text": "*Commit Branch:*\n${CI_COMMIT_REF_NAME}"
}
]
},
{
"type": "divider"
}
]
}
SLACK
}
function share_slack_update_deploy() {
local slack_webhook
slack_webhook="$SLACKWEBHOOKURL"
curl -X POST \
--data-urlencode "payload=$(print_slack_summary_deploy)" \
"${slack_webhook}"
}
