DEV Community

Cover image for Super easy Discord notification job for Gitlab CI
Ítalo Sousa
Ítalo Sousa

Posted on

Super easy Discord notification job for Gitlab CI

Gitlab CI is an amazing free alternative to run your pipelines without tears. It runs in a docker container, which comes in handy and also gives us superpowers.

Once you create a pipeline you wish that everything run automatically and just be notified when it finishes successfully or not.

To make a long story short, let's get our hands dirty.

First of all, we need a pipeline to work on. Here is one super simple job that runs over the lightweight Alpine docker image.

# .gitlab-ci.yml 

stages:
  - test
  - notification

test:
  image: alpine:latest
  stage: test
  script:
    - echo 🕵🏻 Checking...
Enter fullscreen mode Exit fullscreen mode

The next step is to configure our Discord channel that will show our notifications. Do the following path Your channel > Edit Channel > Integrations > Create Webhook then name your bot and click Copy Webhook URL. (Discord Webhook Guide)

Go to Gitlab CI configuration at your project settings and add a new variable: settings > CI / CD > Variables then create a new variable called WEBHOOK_URL and paste the copied URL from Discord as the value.

Now we gonna use the script created by DiscordHooks guys.

Then our pipeline will be as the following:

# .gitlab-ci.yml 

stages:
  - test
  - notification

test:
  image: alpine:latest
  stage: test
  script:
    - echo 🕵🏻 Checking...

success_notification:
  image: alpine:latest
  stage: notification
  script:
    - apk add --update git curl
    - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh
    - chmod +x send.sh
    - /bin/ash ./send.sh success $WEBHOOK_URL
  when: on_success

failure_notification:
  image: alpine:latest
  stage: notification
  script:
    - apk add --update git curl
    - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh
    - chmod +x send.sh
    - /bin/ash ./send.sh failure $WEBHOOK_URL
  when: on_failure
Enter fullscreen mode Exit fullscreen mode

Now just chill and enjoy! Check the full code here.

Note: Do not cache those notifications jobs.

Top comments (0)