如何读取由另一个管道触发的管道内的PR标签?

时间:2020-07-21 09:26:08

标签: azure-devops azure-pipelines

我在这里Is it possible to read the PR tag on a pipeline task?回答了我的第一个问题,但是我的情况有些不同。我需要从另一个管道触发的管道中读取PR标签。

PR触发CI,CI会检查是否可以合并。如果是,则CI触发CD,CD将依次读取PR标签。

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

我有一个名为Get PR tag的PowerShell任务,具有以下脚本(由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)"

但是我不断收到“请求无效”。

========================== 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

我的代理作业设置为使用OAuth令牌:

enter image description here

1 个答案:

答案 0 :(得分:1)

更新

在发布管道中,变量名称与内部版本中的变量名称不同,我们需要在脚本中更新url信息,我们还可以在初始化作业日志中检查发布管道变量。

步骤:

a 。配置分支策略并添加策略Build Validation->添加构建管道A enter image description here b 。创建发行版->选择内部版本A作为源类型->启用功能请求请求触发器->打开预部署条件并启用选项请求请求部署 enter image description here

c 。打开发行版->启用功能允许脚本访问OAuth令牌(单击代理作业名称=>其他选项)添加任务功能,并在下面输入脚本

$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 。将引用名称配置为PS并添加任务cmd以输出标签。

CMD脚本:

echo $(PS.PullRequestTag)

e 。创建请求请求并添加标签 enter image description here 结果:

enter image description here

Update2

拉取请求触发CI构建管道(电源外壳),在构建管道完成后,将触发另一个构建管道(电源外壳测试)。

b 。打开构建管道电源外壳测试,并添加新变量PullRequestID并向test Build Service (xxx)帐户授予“编辑构建管道”权限。 (打开构建管道(电源外壳测试)-> ...->安全->编辑构建管道设置为允许) enter image description here

c 。启用功能允许脚本访问OAuth令牌(单击代理作业名称=>其他选项)添加任务powershell(获取标签值)并在下面输入脚本。单击powershell任务->输出变量->输入PS->添加任务cmd并使用代码echo $(PS.PullRequestTag)输出标签值

$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 。打开构建管道电源外壳,启用功能允许脚本访问OAuth令牌(单击代理作业名称=>其他选项),添加任务电源外壳并在下面输入脚本以更新管道(电源外壳测试)变量PullRequestID值。

$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