Slack notification with Drone CI

4:32 AM Rizwan 0 Comments

There are thousands of CI/CD tool out there! And it's increasing every day I believe. I got chance to work with a few CI/CD tool myself, they are namely Jenkins, Bamboo CI, Go CD, Travis CI, Team City, VSTS and now working with Drone CI in my current organization where I am serving as QA Lead.

Time to time I require to configure new pipeline or some time needs to add an additional feature to the existing pipeline. I am not going to the detail of how to play with .drone.yml to create pipeline or update existing pipeline. This post is particularly for the audience who are working with Drone CI and at some point wanted to have some notification from Drone CI regarding the pipeline build status. Others feel free to read ahead (no harm to learning new stuff, right? :D ).

Assumptions

  1. You are well familiar with slack, slackbot and concept of slack notification.
  2. You are familiar with Drone CI pipeline (at least basic concept like how to trigger, events and such).
  3. You know what Docker image is :).


Setup


Slackbot


To maintain the communication between the CI and slack we would require a slack bot setup for your slack account (could be your personal account or organization account). To integrate slack, first of we would require to configure a new bot and enable the incoming webhooks (if you do not have one already). A very good read for this would be official slack documentation https://api.slack.com/messaging/webhooks.


Drone CI Step


Once you have your slackbot configured properly, you should be able the incoming webhook URL. In the Incoming Webhooks page of api.slack.com of your slack account, all the way down you should be able to see something like the screenshot below:

Go ahead and click the Copy button to have the webhook URL copied into the clipboard.
Now the main stuff. Open up the .drone.yml. Add a step there something like as follows:
slack-notify:
    image: plugins/slack
    secrets: [slack_webhook]
    webhook: $slack_webhook     
    channel: your-channel-name
    template: >
      
      {{#success build.status}} ✔ {{ else }} :x: {{/success}} {{ uppercasefirst build.status }}: Build #{{ build.number }} * (type: `{{ build.event }}`)
      
      Commit: <https://github.com/{{ repo.owner }}/{{ repo.name }}/commit/{{ build.commit }}|{{ truncate build.commit 8 }}>

      Branch: <https://github.com/{{ repo.owner }}/{{ repo.name }}/commits/{{ build.branch }}|{{ build.branch }}>

      Author: {{ build.author }}      

      Build: <{{ build.link }}| Drone Build {{ build.number }} ↗>
      
    when:
      status: [ success, failure ]
      branch: [ master, develop ]

For the template, I took help from https://gist.github.com/wzulfikar/e9064862d17467ed36b81d2f2c17389a. Now, what does this step even is doing? Well, it is actually sending the notification upon the build pipeline passed or failed. Please add this step as the last step in the .drone.yml file. Otherwise, you will see that notification being fired at the very beginning and it's always passing :). Some details of these step are as follows (only discussed steps related to the slack)
  • image: The docker image we are pulling into our drone CI agent to send any notification.
  • secrets: I have provided the webhook URL as the secret in the drone pipeline and providing the secret name (https://docs.drone.io/secret/).
  • webhook: Supposed to be the URL you have copied from your slack API page earlier, but here I am just using the secret name with a "$" sign, to define it as a parameter which will be replaced with the actual value during the pipeline execution time.
  • channel: Name of the slack channel where you would like this alert to be posted.
  • template: To beautify the message with proper formatting and information regarding your build and repo.


Output




Further reading







You Might Also Like

0 comments: