GitLab CI/CD Integration with Slack (Part 2)

Neha Saini
5 min readFeb 12, 2024

--

This post is in continuation to my previous post that helps us in integrating GitLab CI/CD for Android Applications.

You can check Part 1 below link:

https://medium.com/@neha.saini89/git-lab-ci-cd-gitlab-ci-yml-b673b1bd0cfa

In this post, I will discuss how we can integrate the Slack tool with Gitlab CI/CD. We generally use Slack for communication in organizations.

While creating builds from the GitLab CI/CD pipeline we want to publish our build status with some key information to testers & developers.

To get this information in Slack we will add scripts to our .gitlab-ci.yml file to publish the status of builds like success, failure, etc.

We need to create a slackscript.sh file in the project directory to execute in our .gitlab-ci.yml file.

  1. To publish the build start status will add the below code in before_script:
before_script:
- source [...path to script folder]/script/slackscript.sh; share_slack_build_start

2. To publish the build Success/ Failure status will call another function from slackscript.sh in the after_script:

after_script:
- if [ ${CI_JOB_STATUS} == "success" ]; then EXIT_STATUS=0; else EXIT_STATUS=1; fi
- source [...path to script folder]/script/slackscript.sh; share_slack_update_build

Let's discuss the slackscript.sh file in detail

  1. The first step is to get the Slack Incoming Webhook URL

Steps to create the Webhook URL

  1. Navigate to https://api.slack.com/apps?new_app=1
  2. Create an app from scratch as below

Select the workspace from the drop-down list of your Slack organization.

3. After creating the app you will be navigated to the basic information page as below.

4. Click on Incoming Webhooks and toggle on the Activate Incoming Webhooks.

5. Scroll down and navigate to Webhook URLs for workspace on the same page.

6. Click on Add New Webhook to Workspace this will ask for permission to use the workspace from your organization.

Select the Slack channel you want to post the notifications from the list of channels. Click Allow will create the webhook.

7. You can check the Webhook URL in the below screen. Copy this URL to be used in the slackscript.sh file.

You will get something like this https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXX

8. Now going back to the slackscript.sh file

This file contains two functions that will be called on beforescript print_slack_build_start() and afterScript share_slack_update_build() inside our .gitlab-ci.yml file.

These functions will post notifications on the Slack channel with basic information like build environment, header, commit ID, build branch, and commit hash. You can add any information you want to show in the notification. I have used some of the predefined global variables available to all the jobs inside the GitlabCI/CD pipeline.

set -euo pipefail
FAILURE=1
SUCCESS=0
SLACKWEBHOOKURL="https://hooks.slack.com/services/xxxxxxxxxx"

function print_slack_summary_build() {
local slack_msg_header
local slack_channel

local variant="${TARGET_CLIENT}"
if [[ "${TARGET_CLIENT}" == "custom" ]]; then
variant="${VARIANT_NAME}"

fi

# Populate header and define slack channels

slack_msg_header=":x: *Build to ${DEPLOY_ENVIRONMENT} failed*"
if [[ "${EXIT_STATUS}" == "${SUCCESS}" ]]; then
slack_msg_header=":heavy_check_mark: *Build #${CI_PIPELINE_IID} for ${variant} on ${DEPLOY_ENVIRONMENT} succeeded*"

fi

cat <<-SLACK
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Stage:* ${slack_msg_header}"
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Stage:*\nBuild"
},
{
"type": "mrkdwn",
"text": "*Stage:*\n${CI_PIPELINE_IID}"
},
{
"type": "mrkdwn",
"text": "*Client:*\n${variant}"
},
{
"type": "mrkdwn",
"text": "*Environment:*\n${DEPLOY_ENVIRONMENT}"
},
{
"type": "mrkdwn",
"text": "*Build Time:*\n${CI_JOB_STARTED_AT}"
},
{
"type": "mrkdwn",
"text": "*Build Branch:*\n${CI_DEFAULT_BRANCH}"
},
{
"type": "mrkdwn",
"text": "*App Revision:*\n${CI_COMMIT_SHA}"
}
]
},
{
"type": "divider"
}
]
}
SLACK
}

function print_slack_build_start() {
local slack_msg_header
local slack_channel

local variant="${TARGET_CLIENT}"
if [[ "${TARGET_CLIENT}" == "custom" ]]; then
variant="${VARIANT_NAME}"


fi

slack_msg_header=":heavy_check_mark: *Build #${CI_PIPELINE_IID} for ${variant} on ${DEPLOY_ENVIRONMENT} Started*"
cat <<-SLACK
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Stage:* ${slack_msg_header}"
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Stage:*\nBuild"
},
{
"type": "mrkdwn",
"text": "*Stage:*\n${CI_PIPELINE_IID}"
},
{
"type": "mrkdwn",
"text": "*Client:*\n${variant}"
},
{
"type": "mrkdwn",
"text": "*Environment:*\n${DEPLOY_ENVIRONMENT}"
},
{
"type": "mrkdwn",
"text": "*Build Time:*\n${CI_JOB_STARTED_AT}"
},
{
"type": "mrkdwn",
"text": "*Build Branch:*\n${CI_DEFAULT_BRANCH}"
},
{
"type": "mrkdwn",
"text": "*App Revision:*\n${CI_COMMIT_SHA}"
}
]
},
{
"type": "divider"
}
]
}
SLACK
}

function share_slack_build_start() {
local slack_webhook
slack_webhook="$SLACKWEBHOOKURL"
curl -X POST \
--data-urlencode "payload=$(print_slack_build_start)" \
"${slack_webhook}"
}

function share_slack_update_build() {
local slack_webhook
slack_webhook="$SLACKWEBHOOKURL"
curl -X POST \
--data-urlencode "payload=$(print_slack_summary_build)" \
"${slack_webhook}"
}

You will get below notification in the channel

I hope this post will help you to integrate GitlabCI with Slack for build updates. In the next post, I will discuss how to create the docker image that we are using in the script file for build creation.

If this post helped you in any way or you like the information present please like and follow me on Medium.

Stay tuned for other informative posts on real project-level issues that we generally face while development

--

--