naregkar
naregkar

Reputation: 423

REST API, Get Work Item ID from CD Release Pipeline

In Azure DevOps, I have a CI pipeline which generates an artifact. As soon as the Pull Request gets approved, it triggers a CD release pipeline. In the CD pipeline, I have added a PowerShell task to perform some REST API functions.

I want to retrieve the associated work item ID from the artifact which was built during the CI phase.

I am using this API by feeding the Build ID (32 characters, e.g. de5017b5bf537a92397f54f5570783bf32cc6bf), however the result is: 404 - Page not found.

Any ideas? Maybe this API is used only in the CI build pipeline? Many thanks.

Edit

I got the Build ID from Initialize Job log of the CD pipeline and added a variable $(Release.Artifacts.ALIASNAME.BuildID).

enter image description here

Upvotes: 1

Views: 898

Answers (1)

Mengdi Liang
Mengdi Liang

Reputation: 18958

Maybe this API is used only in the CI build pipeline?

No, the API not just limited in build pipeline. It can also be used in release.

Since you did not shared your script about powershell, so I can share your my sample. I just tested it and its succeed on my side.

$buildid=$(build.buildid)
$url = "https://dev.azure.com/{org name}/{project name}/_apis/build/builds/$buildId/workitems?api-version=5.1"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{   
 Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
} -Method GET

Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

The build.buildid is the environment variable of relevant build with this release.

In the log of this pipeline, you will see the result of API :

enter image description here

Updated:

I try to check your logs details from backend, and found error. In the log of your initialize job, you can see that the build definition name is same with your repos name. This means that the source of this release is Repository instead of Build:

enter image description here

Updated 2:

Please select Continuous deployment trigger in the trigger type of release if you want to this release triggered after PR completed. For pull request, completed means that the changes is allowed to merge into the target branch. This is the precondition of Continuous deployment trigger and continuous integration.

And also, for the source of release, please choose relevant Build. In Trigger tab of build definition, please enable Enable continuous integration.

At this time, after the pull request, the changes from feature branch will merge into the target branch. And then, it will trigger the CI build. After build succeed, the artifact will produced and trigger CD release. The release will take this artifacts and deploy it into stages.

Upvotes: 3

Related Questions