Trigger Jenkins Job on PR from Azure DevOps
Tags: jenkin, devops
Published: 2022-06-10 10:24:27

## Triggering a Jenkins Job on PR in Azure DevOps

I have a requirement that whenever there a pull request(PR) created from a feature branch to main development branch in my Azure DevOps git, I need to trigger a **Jenkins** job.

Below are the steps followed to achieve the requirement.

### Capturing Pull Request Creation

- First we need to capture the request that whenever there is a PR created. To do so we can leverage the power of Azure DevOps and its **Service Hooks** integration (Refer **[Service Hooks in Azure DevOps](https://docs.microsoft.com/en-us/azure/devops/service-hooks/overview?view=azure-devops)** for more details) which can be used to integrate various services to get notified for the supported events.
- There so many services supported by Azure DevOps as shown below.
  ![Image description1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axepxxx91yllfn0t8fkg.JPG)
- From the above image, we can see that the Jenkins is also one of the supported service for the integration but it does support few event types like
  - Build Completed
  - Code pushed to a specific repository and branch
  - Pull request merge attempted for a specific target branch from a feature branch.
  - Release deployment completed for a specific environment.
- But our requirement is to send notification to the Jenkins whenever there is Pull request created event on specific repo and specific branch. In order to meet our requirement we can leverage the power of **[Web Hooks](https://docs.microsoft.com/en-us/azure/devops/service-hooks/services/webhooks?view=azure-devops)** which is also supported by the Azure DevOps.
  ### Web Hook Service
  - All the events are supported.
    ![Image description2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ky0b9s3vc1kkibjqvl5c.JPG)
  - Can make use of the Pull request created event
    ![Image description3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdsirq8bpxcdto8ex4ui.JPG)
    - We can also make use of the filters to filter
      - On Which repository PRs the trigger should send the notification
      - On to which branch PR creation should trigger the notification,etc.,
  - Web Hooks support POST http actions
    ![Image description4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/se06wg94gg1gx7ok5efh.JPG)
  - Under settings we can configure various things like
      - URL - which is the URL of _Web Hook listener_ (Should be configure in Jenkins)
      - Custom HTTP Headers and
      - Basic Authentication information to make the web hook request authenticated at Jenkins

### Receiving Web Hook notification in Jenkins

- In order to receive Web Hook request in Jenkins, we should install a Plugin called [Generic Webhook Trigger](https://github.com/jenkinsci/generic-webhook-trigger-plugin).
- The Web Hook listener URL should be like `JENKINS_URL/generic-webhook-trigger/invoke`. This should be registered in the Action Setting in Azure DevOps WebHooks integration portal.
- This plugin will be appear in the Build Triggers section of Jenkins as shown below
  ![Image description5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qth5sqp7o14qjp27d8ef.JPG)
- This plug in can capture the request content via the post content parameters
  ![Image description6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qtyngx3moc04a3spyx9.JPG)
- As shown above in order to access the root element of a JSON payload we can create variable (data) and assign the `$` (using [Json Path](https://github.com/json-path/JsonPath) convention) in the expression tab.
- If the request payload is in xml then we can make use of [X Path](https://www.w3schools.com/xml/xpath_syntax.asp) syntax to access the web hook posted data.