Automating Azure DevOps Builds with Jenkins via Integray

Automating Azure DevOps Builds with Jenkins via Integray

Tired of manually triggering builds every time there’s a change in Azure DevOps? In this post, I’ll show you how to easily automate builds using Azure DevOps webhooks, integrated with Jenkins through Integray. This process will not only save you time but also ensure consistent builds without the need for manual intervention. You’ll learn how to set up webhooks, configure Integray to process data, and automatically trigger builds in Jenkins. By implementing this automation, you can optimize your CI/CD pipeline and focus on more important tasks.

Overview

The process is triggered by Azure DevOps Webhooks, sent to an Integray endpoint, which then processes the data and makes appropriate decisions. Jenkins is ultimately triggered to start the build based on specific conditions.

Triggering the Build from Azure DevOps

The automation starts when a user story or bug in Azure DevOps is marked as ‘Ready to Build.’ At this point, Azure DevOps triggers a webhook with relevant information, such as the build status, branch, and work item details.

Azure DevOps Webhook Setup

You’ll need to configure a webhook in Azure DevOps to fire whenever a change is made to the build status:

  • Trigger: When the Custom.Buildstatus field is changed to ‘1 - Ready to build.’
  • Webhook URL: This webhook will be directed to an Integray endpoint.

Example payload from Azure DevOps:

{
  "resource": {
    "fields": {
      "Custom.Buildstatus": { "newValue": "1 - Ready to build" },
      "Custom.Branch": "feature/new-feature",
      "System.WorkItemId": 12345
    }
  }
}

Processing the Webhook in Integray

Once the webhook is triggered, the data is sent to an Integray endpoint through POST method and a JSON body is passed into a task. You can use Integray to check the Epic Title, Branch, and other conditions before triggering Jenkins.

Example Integray Endpoint

Here’s the Integray endpoint that processes the webhook data:

  • Endpoint: https://example.integray.app/api/endpoint/trigger-jenkins-build
  • Method: POST

Example Code to Trigger Jenkins

The Integray service checks whether the build can proceed, and if the conditions are met, it triggers a build in Jenkins. Here’s the important part of the code that calls the Jenkins build API:

async function startBuild(itemId, branch) {
  const url = `https://jenkins.yourcompany.com/job/YourJenkinsJob/buildWithParameters?RELEASE_NAME=${branch}&WORK_ITEM_ID=${itemId}`;
  const auth = Buffer.from(`${#jenkins_username}:${#jenkins_pass}`).toString('base64');

  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Authorization': `Basic ${auth}`
    }
  });

  if (response.ok) {
    return "2 - Building";  // Build started successfully
  } else {
    return "4 - Build failed";  // Failed to start the build
  }
}

Updating Build Status in Azure DevOps

Once Jenkins starts or completes the build, it triggers Task in Integray which can update the build status back in Azure DevOps using another API call. The build status is updated based on the outcome from Jenkins.

  • Endpoint: https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{workItemId}?api-version=7.0
  • Method: PATCH

Example of updating the status:

const patchData = [
  {
    "op": "add",
    "path": "/fields/Custom.Buildstatus",
    "value": buildStatus // Either "3 - Build completed " or "4 - Build failed"
  }
];

const response = await fetch(url, {
  method: 'PATCH',
  headers: {
    'Authorization': `Basic ${auth}`,
    'Content-Type': 'application/json-patch+json'
  },
  body: JSON.stringify(patchData)
});

Key Endpoints in Integray and Jenkins

  1. Integray Endpoint to handle the webhook:
  • https://example.integray.app/api/endpoint/trigger-jenkins-build
  • Processes Azure DevOps data and triggers Jenkins if conditions are met.
  1. Jenkins Endpoint to start the build:
  • https://jenkins.yourcompany.com/job/YourJenkinsJob/buildWithParameters
  • Passes branch and work item ID to start the build.

Conclusion

By automating the build process through Azure DevOps webhooks, Integray, and Jenkins, you can streamline your CI/CD pipeline, reducing manual interventions and ensuring a more reliable build process. This setup is scalable, allowing you to add further enhancements such as notifications or additional status checks.

1 Like

Hi Robert,
thanks for the great guide! I was wondering, how do you handle scenarios where the webhook triggers but the Jenkins build fails due to configuration issues or missing parameters? Do you have a way to notify the team or retry the build automatically?

Thanks in advance for your reply :slight_smile:

Hi Adam,

Great question! In our setup, if the Epic Title or Branch isn’t properly configured, the system won’t even trigger the build. Instead, we update the BuildStatus field back to 0 - Not started, ensuring Jenkins is only called when everything is set up correctly.

For cases where the Jenkins build fails (due to configuration issues, missing parameters, or other errors), we handle this by automatically updating the BuildStatus field to 4 - Build failed. This gives us clear visibility into the failed builds directly within Azure DevOps.

Here’s a quick look at how it works:

  • If the Epic Title or Branch is invalid:
updateWorkItemField(itemId, "Custom.Buildstatus", "0 - Not started");
  • If the Jenkins build fails:
updateWorkItemField(itemId, "Custom.Buildstatus", "4 - Build failed");

This way, the team is always aware of the build status without needing to manually track failed builds or misconfigurations.

Let me know if you have more questions!

Best regards,
Robert

Hi Robert,

Thanks for the explanation! I appreciate the clarity, and I’ll definitely give this a try.

Best regards,
Adam