更新
在发布管道中,变量名称与构建中的变量名称不同,我们需要更新脚本中的 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 "=========================================================="
