Webhook Trigger using Azure DevOps and Jenkins

Sanjana Jasud
Globant
Published in
4 min readAug 30, 2022

In this article, we will learn how to trigger a Jenkins Job remotely.

Introduction-

Azure DevOps provides developer services for allowing teams to plan work, collaborate on code development, and build and deploy applications.

Jenkins is an open source automation server which enables the developer to build, test and deploy their software.

Trigger is an event occurrence to execute a job. There are five types of triggers in Jenkins:

1] Trigger builds remotely(i.e webhook) : It triggers when an event takes place on an endpoint URL and it needs an authentication token.

2] Build after other projects are built : As it says, you need to specify the list of other projects. When they are built then the current project starts to build.

3] Build periodically : It is like a cron job. It triggers at every specified interval.

4] Github hook trigger for GitScm polling : It is associated with the Github hook.

5] Poll SCM : Before being triggered, it initially checks if there have been any changes.

In this article, we have used the first trigger.

Polling means sending a request to a new event and waiting for an endpoint to respond to it. So basically when you use Poll SCM, it first sends a request to an endpoint to check for any commits. If any commits or new events take place then only it gets triggered. Whereas webhook gets triggered only when a new commit takes place at the endpoint.

The following sections will be covered in this article:

  • Prerequisites
  • Implementation
  • Note
  • Conclusion

Prerequisites

  • An AWS account
  • Azure DevOps account

Implementation

Step 1] Create an Azure Repository

Create an Azure Repository and add a Jenkinsfile. Below is the sample code.

Step 2] Installing and Configuring Jenkins

Connect to your EC2 instance(with Public IP) via SSH and then install Jenkins with the following steps, or you can refer the official documentation here

The status should be in running state.

Step 2a] Paste the Public URL in the browser and set up the Jenkins.

Step 2b] Connect to EC2 instance and enter

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy and paste that password and hit continue. And complete the setup.

Step 3] Creating a Service Hook

In Azure DevOps, Go to Project Settings –> Service hooks –> Create Subscription –> Select Jenkins and click next. Select an event to trigger on and configure the filters.

Step 3a] Before you click on Next, go to Jenkins –> Click on the username on the right topmost corner –> Select Configure –> Add a new token and temporarily keep that token somewhere else and save it.

Step 3b] Click next and fill the required details. Under API token, paste the token which was created in the last step.

Step 3c] Click on test. If it shows Succeeded, then we are good to go, finally click Finish.

Step 4] Creating and Configuring a Jenkins Pipeline

Create a new pipeline.

Step 4a] To use Webhook trigger, select the trigger build remotely option and give your Jenkins URL as shown in the screenshot.

At the end of the URL , the authentication token can be the text of your choice.

Step 4b] There are two ways to Run the script but I have used the second way.

i] Writing and embedding scripts directly in the pipeline.

ii] Accessing the Jenkinsfile by giving the Git Credentials from Azure DevOps.

(Go to Azure DevOps Repository–>Click on Clone->Generate Git Credentials)

Step 4c] Save the pipeline.

Step 4d] Make some changes in the Jenkinsfile and push the changes into the repo.

Within a few seconds the pipeline will trigger automatically.

Note:

In step 3, Code pushed trigger was selected so the pipeline got triggered when the code was pushed in the repo. Similarly, you can add triggers for Build completed, Pull request merge attempted and Release deploy completed.

Conclusion :

The task is completed by both Webhook and Poll SCM, although according to statistics, Poll SCM wastes nearly 98% of polls while Webhook completes the operation with 100% accuracy. So, the Webhook trigger is the most efficient trigger to use.

--

--