0

I had my first question answered here Is it possible to read the PR tag on a pipeline task? but my scenario is a little bit different. I need to read the PR tag from a pipeline that was triggered by another pipeline.

PR triggers the CI which checks if everything's ok for merge. If it is, the CI triggers the CD which will in turn read the PR tag.

PR -> CI -> CD (access the tag here)

I have a PowerShell task named Get PR tag with the following script (courtesy of Lance):

$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)"

But I keep getting "The request is invalid.":

========================== Starting Command Output ===========================
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/74b14931-e33a-4389-b19f-3db7faa53e8d.ps1'
Invoke-RestMethod: /home/vsts/work/_temp/74b14931-e33a-4389-b19f-3db7faa53e8d.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'.
Finishing: Get PR tag

My agent job is set to use the OAuth token:

enter image description here

eestein
  • 4,914
  • 8
  • 54
  • 93

1 Answers1

1

Update

In release pipeline, the variable name is not the same as the variable name in build, we need update the url info in the script, we can also check the release pipeline variable in the Initialize job log.

Steps:

a. Configure branch policy and add the policy Build Validation-> add build pipeline A enter image description here b. Create release->select the build A as the Source type->Enable the feature Pull request trigger->open Pre-deployment conditions and enable the option Pull request deployment enter image description here

c. Open the release->enable the feature Allow scripts to access the OAuth token (Click Agent Job Name=>Additional options) add task powershell and enter the script below

$url = "$($env:SYSTEM_TASKDEFINITIONSURI)$env:BUILD_PROJECTID/_apis/git/repositories/$($env:BUILD_REPOSITORY_NAME)/pullRequests/$($env:BUILD_PULLREQUEST_ID)/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)"

d. Configure the Reference name as PS and add task cmd to output the tags.

CMD script:

echo $(PS.PullRequestTag)

e. Create pull request and add tags enter image description here Result:

enter image description here

Update2

The pull request triggers the CI build pipeline(power shell), after the build pipeline is completed, another build pipeline(power shell test) will be triggered.

b. Open the build pipeline power shell test and add a new variable PullRequestID and grant test Build Service (xxx) account the Edit build pipeline permission. (open the build pipeline(power shell test)--> ... --> Security --> Edit build pipeline set to Allow) enter image description here

c. enable the feature Allow scripts to access the OAuth token (Click Agent Job Name=>Additional options) add task powershell(Get the tag value) and enter the script below. click the powershell task->Output Variables->enter PS->add a task cmd and use the code echo $(PS.PullRequestTag) to output the tag value

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:BUILD_REPOSITORY_NAME)/pullRequests/$(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)"

d. Open build pipeline power shell, enable the feature Allow scripts to access the OAuth token (Click Agent Job Name=>Additional options) add task powershell and enter the script below to update the pipeline(power shell test) variable PullRequestID value.

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/55?api-version=5.1"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

# Update an existing variable named PullRequestID to its new value pull request ID
$pipeline.variables.PullRequestID.value= $($env:SYSTEM_PULLREQUEST_PULLREQUESTID)

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe 'PullRequestID ' is updated to" $updatedef.variables.PullRequestID.value
write-host "=========================================================="

enter image description here

Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • I have updated the answer, please check it. The pull request triggers the CI build pipeline, after the build pipeline is completed, the CD release pipeline will be triggered, is it right? If not, could you please share the detailed steps with me? I will check it and help you. – Vito Liu Jul 23 '20 at 09:16
  • The regular pipeline is build pipeline or release pipeline? – Vito Liu Jul 23 '20 at 10:12
  • Hi, I have updated the answer, please check it. In addition, the error message permission denied, you should see the error message (xxx service account) needs Edit release/build pipeline permissions for release/build pipeline {pipeline definition ID}. We need give the service account edit permission(open the release/build pipeline--> ... --> Security --> Edit release/build pipeline set to Allow). Please check it – Vito Liu Jul 24 '20 at 11:15
  • If have any achievements, please feel free to let me know. – Vito Liu Jul 24 '20 at 11:56
  • Hi, on your script you added 55 statically, what would be the variable for that one? `definitions/55` – eestein Jul 24 '20 at 12:24
  • That is build pipeline(power shell test) definition ID. Since we cannot see another build definition ID in the build environment variable, I use the static variables. Sorry, I forgot to add this description. – Vito Liu Jul 24 '20 at 12:27
  • So that would work only statically, right? I need to use my actual build definition id – eestein Jul 24 '20 at 12:33
  • I got this error now: `This request expects an object in the request body, but the supplied data could not be deserialized.` This line `Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"` printed the json output, but this one `$json = @($pipeline) | ConvertTo-Json -Depth 99` (apparently) did not work – eestein Jul 24 '20 at 12:47
  • I can see that the json has the PR: `"variables": { "PullRequestId": { "value": "13577" }` but it's throwing that error I mentioned on my previous message – eestein Jul 24 '20 at 12:58
  • It is simpler to specify static variables here. Please change the code $json = @($pipeline) | ConvertTo-Json -Depth 99 to $json = $($pipeline | ConvertTo-Json -Depth 100). Please also open another build pipeline and check the variable. – Vito Liu Jul 24 '20 at 13:07
  • I got the same error, I'll try this solution and see if it works https://stackoverflow.com/questions/50044942/issue-when-updating-build-definition-using-the-rest-api-of-vsts – eestein Jul 24 '20 at 13:17
  • Hi eestein, Has this problem been solved? Please let me know if the variable has update in the another build pipeline variable. If the issue still occurs, do you mind share a screenshot of the log in the question description? – Vito Liu Jul 24 '20 at 13:51
  • It worked! that problem was the encoding. thanks for your help! – eestein Jul 24 '20 at 13:54