DEV Community

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

Posted on

4 1

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay