0

我的第一个问题在这里得到了回答是否可以读取管道任务的 PR 标签?但我的情况有点不同。我需要从另一个管道触发的管道中读取 PR 标签。

PR 触发 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 令牌:

在此处输入图像描述

4

1 回答 1

1

更新

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

脚步:

一个。配置分支策略并添加策略 Build Validation-> add build pipeline A b。创建发布->选择构建A作为源类型->启用功能拉请求触发器->打开预部署条件并启用选项拉请求部署 在此处输入图像描述 在此处输入图像描述

。打开release->enable the feature Allow scripts to access the OAuth token (点击 Agent Job Name=>Additional options) 添加任务powershell,输入下面的脚本

$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 . 创建拉取请求并添加标签 在此处输入图像描述 结果:

在此处输入图像描述

更新2

拉取请求触发 CI 构建管道(power shell),构建管道完成后,将触发另一个构建管道(power shell 测试)。

。打开构建管道电源外壳测试并添加一个新变量 PullRequestID 并授予test Build Service (xxx)帐户编辑构建管道权限。(打开构建管道(power shell 测试)--> ... --> 安全性 --> 编辑构建管道设置为允许) 在此处输入图像描述

。启用功能允许脚本访问 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 . 打开构建管道power shell,启用功能允许脚本访问OAuth令牌(单击代理作业名称=>附加选项)添加任务powershell并输入下面的脚本以更新管道(power shell测试)变量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 "=========================================================="

在此处输入图像描述

于 2020-07-22T10:13:01.357 回答