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

北战南征 提交于 2020-07-23 05:35:08

问题


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:

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:

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


回答1:


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)

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

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:

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




回答2:


I doubt there's a predefined variable for a pull request tag, but you can achieve the goal with the REST API. For example, you can have a build task (e.g. a PowerShell script) which:

  • gets all tags (aka labels) of a pull request
  • parses the list of tags to find out the necessary one
  • set the build variable to the name of the necessary tag


来源:https://stackoverflow.com/questions/62932941/is-it-possible-to-read-the-pr-tag-on-a-pipeline-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!