Is it possible to read the PR tag on a pipeline task?

Viewed 1886

My question is basically the title. I couldn't find such information reading this page and searching the web.

My scenario is the following: whenever I create a PR to master I add a tag with some information, like the lib version seen here:

enter image description here

Then, during the build process, I generate the release notes and would like to access that PR tag inside a task like I do with $(Build.BuildId) here:

enter image description here

How can I accomplish that? Something like $(PullRequest.Tag) Thanks!

2 Answers

How can I accomplish that? Something like $(PullRequest.Tag) Thanks!

There's no predefined variable for Pull Request tag. (I use printenv command in CMD task to confirm this!)

Here're my workaround:

Use Powershell task to call this rest api, the response would contain the tag of specific PR

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:BUILD_REPOSITORY_NAME)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/labels?api-version=5.1-preview.1"
$response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}

Write-Host "##vso[task.setvariable variable=PullRequestTag;isOutput=true]$($response.value.name)"

Then pass the variable $response.value.name(come from rest api response, the name represents the tag name) to output variable PullRequestTag(custom variable) so that next tasks can access the returned tag name.

Notes:

1.Make sure the job which contains the Powershell task allow scripts to access the OAuth token cause my script uses OAuth token instead of PAT to call rest api. (Click Agent Job Name=>Additional options)

enter image description here

2.Since it's a output variable, we should use format $(referenceName.variablename).

enter image description here

enter image description here

After that, we can use $(PS.PullRequestTag) in next tasks to access the tag name.

3.Since your scenario is in a pipeline run triggered by PR, so actually the Powershell task should only run when current pipeline is triggered by PR instead of manual run/CI.

Use and(succeeded(), eq(variables['Build.Reason'], 'PullRequest')) in PS task's control options. See conditions and Build.Reason variable.

Update:

If I added several tags when creating the PR:

enter image description here

The $(PS.PullRequestTag)'s value would be:

enter image description here

Related