Report build status from Jenkins to Azure DevOps

1.9k views Asked by At

Is there a possibility to report build status from Jenkins to Azure DevOps?

I have a repository in Azure DevOps and a pipeline in Jenkins. Using Azure Service Hooks functionality, I trigger Jenkins pipeline when the repository gets updated. This works fine with both "Built-in Jenkins API" and "DevOps plugin for Jenkins" options.

Now I want a build status to be displayed in Azure DevOps on Commits page, as if native Azure DevOps pipeline was executed, and display a link to Jenkins build there if possible.

Is there any way to do that?

I was thinking about calling some Azure REST API as post-build action in Jenkins however it seems that there is no API that allows to set status of a commit, neither to insert data for a build that was never started by Azure DevOps itself.

1

There are 1 answers

1
Levi Lu-MSFT On BEST ANSWER

There is a restful api which allows you to create a git commits status for the commits in Commits page.

You can call below API as post-build action in Jenkins.

POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/statuses?api-version=5.1

The commitId is the commit SHA. And you can get the repositoryId from the Repositories under Repos in Project Settings. It is in the url. Check below screenshot.

enter code here

Below is an example to create a commit status in powershell scripts.

$url="https://dev.azure.com/Org/Proj/_apis/git/repositories/....-..-442d-9dbe-76debfba1c60/commits/....faac7aafeefb3f1b83c/statuses?api-version=5.1"

$connectionToken ="Person Access Token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$body ='{
  "state": "succeeded",
  "description": "The build is successful",
  "targetUrl": "https://dev.azure.com/.../.../_build/results?buildId=1577",
  "context": {
    "name": "Build123",
    "genre": "continuous-integration"
  }}'

Invoke-RestMethod -Uri $url -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method  Post -ContentType "application/json" -Body $body