9

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!

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
eestein
  • 4,914
  • 8
  • 54
  • 93
  • Let me know if there's any issue when trying my workaround~ (It works well in my test) – LoLance Jul 17 '20 at 08:23
  • How about hard-coding the url in that task? `$url = "https://dev.azure.com/YourOrgName/YourProjectName/_apis/git/repositories/YourRepoName/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/labels?api-version=5.1-preview.1"` – LoLance Jul 22 '20 at 09:28

2 Answers2

9

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

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Thank you, it was pretty thorough and since this is a brand new territory for me, I appreciate the detailed answer. I've set everything up and will test it in a few, but I've got a question. What if the tag is an array? Should I account for that? That's not a big problem for me, because I can just set a rule for all devs making PRs to always use only the version as the tag. I just need to know in order to properly handle the issue. – eestein Jul 17 '20 at 11:18
  • @eestein Do you mean adding several tags to the PR, if so, the variable would represents a string variable like: `Tag1 Tag2 Tag3` format.(See my update above). If I misunderstand the array you mean, please correct me with a simple example so that I can test it on my side:) – LoLance Jul 20 '20 at 07:30
  • Got it, thanks! I'll finish my tests today and mark this as answer. Thanks again – eestein Jul 20 '20 at 08:48
  • Lance, thank you, but there's a small change to my requirement. IndividualCI is the trigger. Here's the actual scenario (sorry, I forgot about that). I have a CI that checks if everything's ok before merging and then it triggers a CD that will actually generate the notes and read the tag from the PR. Will this CD have access to the PR? – eestein Jul 20 '20 at 12:58
  • Lance, it didn't work, I got permission denied, but the access token is selected. This is the error: ``` mkdir: cannot create directory ‘/run/user/1001’: Permission denied Invoke-RestMethod: /home/vsts/work/_temp/46f2130e-5c70-4fbc-aac3-b928ef5e37f3.ps1:3 Line | 3 | $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | {"count":1,"value":{"Message":"The request is invalid."}} ##[error]PowerShell exited with code '1'. ``` – eestein Jul 20 '20 at 13:21
  • https://developercommunity.visualstudio.com/content/problem/1082318/azure-cli-task-always-fails-with-permission-denied.html – eestein Jul 20 '20 at 13:22
  • Even if I change to ubuntu 18.04 I get the request is invalid error. Line | 3 | $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | {"count":1,"value":{"Message":"The request is invalid."}} ##[error]PowerShell exited with code '1'. – eestein Jul 20 '20 at 13:56
  • @eestein Try [Powershell task](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/powershell?view=azure-devops), it works well on my side when I use PS task in hosted windows-2017 agent. – LoLance Jul 21 '20 at 01:50
  • @eestein I haven't had time to test this using ubuntu system, but I'll try that this day and let you know if there's any update. – LoLance Jul 21 '20 at 01:51
  • @eestein Again it works in hosted Ubuntu18.04 machine. Please check: 1. Whether your devops account has permission to create PR and access PR. 2.Which token do you use? My script uses OAuth token instead of normal PAT, so you should enable `allow scripts to access the OAuth token` option for the agent job, and then use the same script like mine. (You don't need to change anything within my script above..) – LoLance Jul 21 '20 at 02:08
  • Does the `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` in your error message mean PAT token? If you're using PAT, you should use [this format](https://stackoverflow.com/a/60768179/10910450). My script above uses OAuth token instead of PAT... And for your first question in your comments, the answer is `yes`. The CD by default has no knowledge about the PR, but I think there should be workaround to achieve that. (However that's another question which is too complex to answer that in a comment.. Could you please post a new case with `azure devops` tag? Normally we'll view the cases if we have good idea). – LoLance Jul 21 '20 at 02:17
  • Also, we can move the discussion to chat if you do want that :) – LoLance Jul 21 '20 at 02:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218265/discussion-between-eestein-and-lance-li-msft). – eestein Jul 21 '20 at 09:06
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:

Dharman
  • 30,962
  • 25
  • 85
  • 135
Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139