[英]Report build status from Jenkins to Azure DevOps
Is there a possibility to report build status from Jenkins to Azure DevOps?是否可以将 Jenkins 的构建状态报告给 Azure DevOps?
I have a repository in Azure DevOps and a pipeline in Jenkins.我在 Azure DevOps 中有一个存储库,在 Jenkins 中有一个管道。 Using Azure Service Hooks functionality, I trigger Jenkins pipeline when the repository gets updated.
使用 Azure 服务挂钩功能,我会在存储库更新时触发 Jenkins 管道。 This works fine with both "Built-in Jenkins API" and "DevOps plugin for Jenkins" options.
这适用于“内置 Jenkins API”和“Jenkins 的 DevOps 插件”选项。
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.现在,我希望在提交页面上的 Azure DevOps 中显示构建状态,就像执行了本机 Azure DevOps 管道一样,并在可能的情况下显示指向 Jenkins 构建的链接。
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.我正在考虑在 Jenkins 中调用一些 Azure REST API 作为构建后操作,但是似乎没有允许设置提交状态的 API,也没有允许为 Azure DevOps 本身从未启动的构建插入数据。
There is a restful api which allows you to create a git commits status for the commits in Commits page.有一个宁静的 api,它允许您为提交页面中的提交创建一个git 提交状态。
You can call below API as post-build action in Jenkins.您可以在 Jenkins 中调用以下 API 作为构建后操作。
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/statuses?api-version=5.1
The commitId is the commit SHA. commitId 是提交 SHA。 And you can get the repositoryId from the Repositories under Repos in Project Settings .
您可以从Project Settings 中Repos下的Repositories 中获取 repositoryId。 It is in the url.
它在网址中。 Check below screenshot.
检查下面的屏幕截图。
Below is an example to create a commit status in powershell scripts.下面是在 powershell 脚本中创建提交状态的示例。
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.