DEV Community

KrishnaSai Polanki
KrishnaSai Polanki

Posted on

Trigger Jenkins Job on PR from Azure DevOps

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 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
  • 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 which is also supported by the Azure DevOps.

    Web Hook Service

    • All the events are supported. Image description2
    • Can make use of the Pull request created event Image description3
    • 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
    • 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.
  • 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
  • This plug in can capture the request content via the post content parameters Image description6
  • 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 convention) in the expression tab.
  • If the request payload is in xml then we can make use of X Path syntax to access the web hook posted data.

Top comments (0)