Sending CircleCI job notifications to Discord
By: Antonio Nedanoski
Tags: Orb, DevOps, Software, Circleci, Discord

When your goal is to completely automate your CI/CD pipeline, it comes very natural and convenient to have your pipeline tool sending you certain notifications and messages about the status of the jobs running. Of course, there are a lot of different CI/CD services you can use: Jenkins, Travis, CodeShip, CircleCI, Chef. Also, there are quite a few instant messaging apps as well like Slack, Teams, Skype, Discord… Finding the best combination is a decision solely made on the company’s needs and preferences.

CircleCI to Discord

What if you decide to use CircleCI and Discord? Your whole dev team is in Discord, the company’s main communication tool, so it comes as a need to integrate the pipeline job notifications into a Discord channel. Well if this is the case, you are in the right place!

CircleCI Orbs

Those of you out there who are familiar with CircleCI are probably also familiar with the orbs concept that the company introduced back in Nov 2018. The orbs are already “packaged” lines of code which save you the pain of writing a huge configuration file, all of that open-sourced. From their introduction until now, the orbs community has grown a lot with orbs used for installing nodejs/gradle/go/python, slack notifications, AWS cli commands and a lot more.

Discord

Well, we all know Discord, right? Probably most of you have used it with your gaming buddies, creating channels so you can chit-chat while playing your favorite game (CS:GO for me). Being a freeware, Discord provides a very good alternative to other well known instant messaging applications like Slack, Teams, Skype and others. Also, if you are looking to build your application’s community, it might be the right place where the “cool” people hang out.

Integration

In order to make this work, I did what pretty much everybody else does, asked my best buddy Google. Of course, Discord has a documentation when it comes to their webhooks, and there are some pretty solid examples of how the POST requests should be created to send messages to a channel. There is one small issue here though, CircleCI supports slack integration natively, via their Chat notifications in the Project settings or via their slack orb, but nothing for Discord.

This meant that I would’ve to either input the commands into the CircleCI config.yml file, which would add more than 150 lines of code to the file itself, or just go down the road to create and publish my own CircleCI orb for discord. I chose the latter.

This discord CircleCI orb is based on the verified slack orb, with some modifications to the json structure, of course, matching the one that Discord needs. It is pretty easy to use, you can find the source code in CircleCI’s orb registry and my github repo. An example would be:

- discord/status:

fail_only: true

failure_message: "Triggered by: **${CIRCLE_USERNAME}** \

\

Ooops! The **$CIRCLE_JOB** job has failed!"

webhook: "${DISCORD_STATUS_WEBHOOK}"

This simple example above will send the failure_message to the webhook set only if the job fails. The parameters supported within the status command are:

webhook - The discord channel webhook you have set on your server.

- The discord channel webhook you have set on your server. success_message - The message sent when job is successful.

- The message sent when job is successful. failure_message - The message sent when job has failed.

- The message sent when job has failed. success_only - If true, failed jobs notifications will not be sent.

- If true, failed jobs notifications will be sent. fail_only - If true, successful jobs notifications will not be sent.

- If true, successful jobs notifications will be sent. mentions - Mentions of users in the channel.

- Mentions of users in the channel. only_for_branches - If set, a comma-separated list of branches for which to send notifications. No spaces.

- If set, a comma-separated list of branches for which to send notifications. No spaces. channel - Discord channel, overrides the webhook set.

The End

The DevOps way is to automate everything! Automate deployments, automate how we manage servers, how we create AMIs, how we create infrastructure. Automate the notifications coming to our messaging apps to help us use our CI/CD pipeline more efficient and faster!

Having more options and tools like this provides teams and companies with the means to do this way easier and helps the whole flow be super stable and transparent.

Happy orb-ing!

Links to the orb code:

https://circleci.com/orbs/registry/orb/antonioned/discord

https://github.com/antonioned/circleci-discord-orb