Sending custom Slack notification from GitLab at each Job or Stage

You might have come across different blogs or articles in which you might have seen how we can integrate Slack channel with GitLab and send out a notification to slack channel, these notifications are limited to Push request, merge request, pipeline request etc.. but GitLab does not provide the functionality to send out a notification after completion of each job in the pipeline.

In this article, I would like to walk you through on how we can send out a notification after completion of each job in the pipeline.

Below I have created rough workflow for better understanding on how the slack notification service works after completion of each job.

I hope now you have fair understanding on how this functionality works, so lets get started with the configuration.

Create a slack channel, this can be a Private or Public channel. You can refer the below link for creating a channel.

Integrate Slack notification service with GitLab, you can refer the below link.

Once the integration is completed, you can go ahead and configure GitLab pipeline, below is a skeleton yml template of a GitLab pipeline looked something like

stages:
- build
- test
- deploy
build:
image: < image >
script: < build script >
variables:
- ENVIRONMENTNAME: "dev"
after_script:
- if [ ${CI_JOB_STATUS} == "success" ]; then EXIT_STATUS=0; else EXIT_STATUS=1; fi
- source ./slackscript.sh; share_slack_update_build

test:
image: < image >
script: < test script >
variables:
- ENVIRONMENTNAME: "dev"
after_script:
- if [ ${CI_JOB_STATUS} == "success" ]; then EXIT_STATUS=0; else EXIT_STATUS=1; fi
- source ./slackscript.sh; share_slack_update_test
deploy:
image: < image >
script: < test script >
variables:
- ENVIRONMENTNAME: "dev"
after_script:
- if [ ${CI_JOB_STATUS} == "success" ]; then EXIT_STATUS=0; else EXIT_STATUS=1; fi
- source ./slackscript.sh; share_slack_update_deploy

Here you can see we are using after_script section which helps in sending out the notification in slack channel, basically it will check whether the particular job has run all the commands mentioned in the script, if the command runs successfully then it will send a success response and will assign EXIT_STATUS=0 else it will assign EXIT_STATUS=1, then we are running the source ./slackscript.sh; share_slack_update_build command which calls the function share_slack_update_build from slackscript.sh file that sends the notification on slack channel when build job is completed, similarly we are doing it for test and deploy job.

Finally you need to have generic slack alert template.

I figured out that slack has released Block Kit APIs where you can program/structure different blocks to generate a message template. They also offer a prototype builder playground that you can try here, below I have constructed a generic slack alert template with the help of Block Kit API which I have named as slackscript.sh. You can name it whatever you want :P.

#!/bin/bashset -euo pipefailFAILURE=1
SUCCESS=0
SLACKWEBHOOKURL="https://hooks.slack.com/services/XXXXXX/YYYYYYY/aabbcc"function print_slack_summary_build() {local slack_msg_header
local slack_msg_body
local slack_channel
# Populate header and define slack channelsslack_msg_header=":x: *Build to ${ENVIRONMENTNAME} failed*"if [[ "${EXIT_STATUS}" == "${SUCCESS}" ]]; then
slack_msg_header=":heavy_check_mark: *Build to ${ENVIRONMENTNAME} succeeded*"
#slack_channel="$CHANNEL_TEST"
fi
cat <<-SLACK
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${slack_msg_header}"
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Stage:*\nBuild"
},
{
"type": "mrkdwn",
"text": "*Pushed By:*\n${GITLAB_USER_NAME}"
},
{
"type": "mrkdwn",
"text": "*Job URL:*\nGITLAB_REPO_URL/${CI_JOB_ID}"
},
{
"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_build() {local slack_webhookslack_webhook="$SLACKWEBHOOKURL"curl -X POST \
--data-urlencode "payload=$(print_slack_summary_build)" \
"${slack_webhook}"
}

NOTE : This template is only specific to build job you need to create same template for test and deploy job mentioned in GitLab template.

So now we had a custom slack notification which notifies at each job. Further you can play around by adding variables in GitLab file and enhancing the given template depending on your use case.

That’s all I have in this blog, hope you find it useful.

Thank You :)

Cloud and DevOps enthusiast