Published on: June 10, 2024
8 min read
Configure GitLab webhooks with SMS alerts to instantly get feedback on new and existing issues within a project and enable teams to react quickly to project- and group-level changes.

We all strive to create the most robust and secure DevSecOps environments where everyone can collaborate to deliver amazing products for our customers. But no matter how robust and secure we design our environments we cannot exclude the possibility that something might go wrong. When an issue does occur we want to make sure we can remediate it quickly. To do that it's not only important to document the details of the issue but also get the right people notified immediately. In this article, we will set up GitLab webhooks together with Twilio's functionality to send SMS alerts to the right people, getting them up to date so they can mitigate problems quickly.
Now, let's get hands-on with building real-time SMS notifications.
At a high level, the workflow looks as follows:

We need to set up our Twilio environment to be able to send SMS. To do this, log in to your Twilio account. If you don't have one just follow the link provided in the prerequisites section above.
Once logged in you will see the Twilio Console, which will look something like this:

From here, we will head to the left sidebar menu and select United States (US1) > Phone Numbers > Manage > Active numbers and then click the "Buy a number" button.

You can select a phone number, which will be the number that notifications are sent from. There are some guidelines specific to which countries you can send SMS based on the Twilio phone number you purchase, so please keep that in mind. In this example, I will be using my personal U.S. phone number for this article as the recipient phone number, so, in this case, I will purchase a U.S. Twilio number. Just make sure your phone number has the SMS capability. Once selected, simply click the "Buy

Next, we just need to make sure Twilio can send SMS to our recipient phone number by allowing Twilio Programmable Messaging to send SMS to the country our recipient phone number is associated with. To do so, head to [United States (US1) > Messaging > Settings > Geo permissions and make sure that the country associated with the recipient's phone number is selected (for example, as I am using my U.S. phone number as the recipient phone number in this blog, I will select United States).

Click "Save geo permissions." With that we're all set up to send SMS.
Next, let's handle the processing of the webhook and the creation of our SMS alerts with Twilio Functions.
To process the webhook we will be sending to Twilio, we need to define a Twilio Function. To do this, select United States (US1) > Functions and Assets > Functions (Classic) > List and click "Create a Function." Select the "Hello SMS" option in the pop-up and click "Create."

Now, let's go ahead and configure our Twilio Function.
/handle-event-webhook. In my case this would result in the following path: https://daff-mac-7354.twil.io/handle-event-webhook.Check for valid Twilio signature.<your personal phone number> and <your Twilio Phone number>: exports.handler = function (context, event, callback) {
const twilioClient = context.getTwilioClient();
twilioClient.messages
.create({
body: `Hi there! There was an update to issue (${event["object_attributes"]["id"]}) with title "${event["object_attributes"]["title"]}" in project ${event["repository"]["name"]}. It was just ${event["object_attributes"]["action"]}.`,
to: "<your personal phone number>",
from: "<your Twilio Phone number>",
})
.then((message) => {
console.log("SMS successfully sent");
console.log(message.sid);
return callback(null, `Success! Message SID: ${message.sid}`);
})
.catch((error) => {
console.error(error);
return callback(error);
});
};
It should end up looking like the following:

Now, whenever our endpoint is hit, it should trigger an SMS with a custom message indicating a change to an existing issue which will represent an example of the various webhook events we can configure.
Next, let's set our webhooks within GitLab to trigger this endpoint whenever a change to an issue is made.
Log in to your GitLab instance and go to the project you would like to configure event webhooks in.
Once in the Project, go to Settings > Webhooks and click on "Add new webhook."

You will only need to configure the following fields:
https://daff-mac-7354.twil.io/handle-event-webhook.
We're all set to test our setup!
While in the project that was just configured to react to issues events, head to "Plan > Issues" and click on "New issue."

Add a title and click on "Create Issue."

If everything is configured correctly, you should get an SMS looking something like:
Sent from your Twilio trial account - Hi there! There was an update to issue (146735617) with title "GitLab webhook example" in project Webhooks Example. It was just opened.
We've leveraged Twilio's SMS functionality in combination with GitLab webhooks to instantly get feedback on new and existing issues within our project, allowing us to react quickly to any changes that might occur. This simple use case showed how one person could instantly get informed about a single type of event. However, often we want to inform more people about various events or be able to react to more than just one type of event (like issue creation and updates).
This functionality can be expanded by:
Start a free trial of GitLab Ultimate today to test-drive more DevSecOps features.
Enjoyed reading this blog post or have questions or feedback? Share your thoughts by creating a new topic in the GitLab community forum.
Share your feedback