Triggering a custom webhook after continuous integration

- By Manisha Sahasrabudhe

BACK TO BLOG HOME

Even with the best continuous integration platform on the planet, you will still have scenarios when you want to trigger custom workflows after your CI finishes. These workflows could be very targeted and should have the ability to be customized based on whether CI passed.

Today, I am going to introduce you to a very versatile feature we added recently - the ability to add a custom webhook to your CI workflows that will be triggered after your build finishes. You can configure this trigger based on several factors, including build result, branch, etc and also include one or more parameters.  

This new feature is documented here in greater detail

Let's look at a simple example to see how this works. I have a project manishas/sample_nodejs and I want to open a GitHub issue in the repository if my build fails. I also want to customize the issue title and description to include a the project name, build number, commit message, and build URL.

For this example, I have a GitHub repositories - manishas/sample_nodejs which is already enabled for continuous integration on Shippable

We will first add an account integration for the custom webhook trigger and then add the integration to the yml in order to configure the details.

1. Creating a GitHub personal access token 

Since our goal is to create an issue in the GitHub repository if a CI run fails, we need a personal access token from GitHub to set up the integration on Shippable. Instructions on creating a token are available in GitHub's documentation

Please note that you only need a personal access token because we're walking through the scenario of creating an issue in a GitHub repository. If you're calling your own custom webhook and are just following this for guidance, you can skip this and go to the next step.

2. Adding an account integration of type Event Trigger

From the Project page for manishas/sample_nodejs, click on Settings and click on Integrations->Select notifications integration

Create a new notification integration as shown below:

 event_trigger_webhook.png

- Name your integration with an easy to remember friendly name.

- In the next dropdown, select Generic Webhook. This will show an input for the Webhook URL and the Authorization header for your URL.  

- For the webhook URL, enter your custom URL. For our example, this is the URL for the GitHub issues for the repository: https://api.github.com/repos/manishas/sample_nodejs/issues

- For Authorization, you can include the HTTP authorization header for your custom URL. For our example, we will need the personal access token you created in the previous step - 

 token 5434vcbecafhgbgtut8toitjn9c49ed

Click on Save integration.

3. Editing the yml

Next, edit the yml for manishas/sample_nodejs

integrations:
  notifications:
    - integrationName: trigger_gh_issue
      type: webhook
payload:
- title=Failure - $REPO_FULL_NAME - $BUILD_NUMBER
- body=Shippable Run $BUILD_NUMBER ($BUILD_URL) failed for the following change: $COMPARE_URL
on_failure: always
on_success: never

- For integrationName, enter the name of the account integration you created in the step above.

type for this scenario is always webhook.

- In the payload section, you can enter any number of parameters you want to pass in the webhook. For this example, I've included the title and body of the GitHub issue I want to open when the build fails.  

- on_failure is set to always since I want to trigger this webhook only if the build fails. on_success is set to never because I do not want to open an issue if the build succeeds.

Now, trigger a build for the project and make it fail. And voila! It created an issue in the repository:

eventTrigger_webhook_issue.png

Imagine the power of this feature. You can launch space shuttles (or not...) depending on the results of your CI workflow.

Try this out today for your custom webhook URLs and let us know what you think! Happy Shipping!


Try Shippable

Topics: CI/CD, Webhooks, Workflows