NeilNewman
NeilNewman

Reputation: 120

Is it possible to modify the requester in an Azure DevOps pipeline?

We have an Azure DevOps pipeline to run tests, that is triggered by the completion of another pipeline that builds and publishes artifacts when a pull request completes.

Ideally we would like the second pipeline to show the user that completed the original Pull Request, rather than the last person to modify the testing branch. Any failures of the second pipeline would also raise a Work Item and assign it to the user that completed the original Pull Request.

There is a mechanism for updating the build number, but I cannot find a way to update the RequestedFor information.

Update:

I found we can update the Build number using

Write-Host "##vso[build.updatebuildnumber]$BuildNumber"

in PowerShell, but cannot find any way the change the requesting user.

Upvotes: 1

Views: 465

Answers (1)

Suki Ji-MSFT
Suki Ji-MSFT

Reputation: 743

To make the second pipeline to show the user that completed the original Pull Request, please use Build.RequestedFor predefined variable to get the user who completed the original Pull Request. Then, use variable group with PowerShell task to transfer the value to the second pipeline. Here is an example below :

  1. Create a variable group and link the variable group to both of the two pipelines.

enter image description here

enter image description here

  1. Add a PowerShell task to the first Pipeline and run REST API to update the variable group. Pay attention to the PAT, variable name, variable group name, organization name, project name, groupID.

     - task: PowerShell@2
       inputs:
         targetType: 'inline'
         script: | 
           $connectionToken="<PAT>"
           $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
           $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
           $headers.Add("Authorization", "Basic $base64AuthInfo")
           $headers.Add("Content-Type", "application/json")
           $body = '{"variables": {"<variable name>": {"value": "$(Build.RequestedFor)"} },"type": "Vsts","name": "<variable group name>","description": "Updated variable group"}'
           $response = Invoke-RestMethod 'https://dev.azure.com/<organization name>/<project name>/_apis/distributedtask/variablegroups/<groupID>?api-version=5.0-preview.1' -Method 'PUT' -Headers $headers -Body $body
           $response | ConvertTo-Json
    
  2. Show the variable value in second pipeline that automatically triggered by first pipeline.

resources: 
  pipelines:
  - pipeline: <first pipeline name>
    source: <first pipeline name>
    trigger: true

pool:   
  vmImage: windows-latest

steps:
- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo $(<variable name>)   
  displayName: 'Requested For'

Upvotes: 1

Related Questions