如何读取由另一个管道触发的管道内的PR标签? [英] How can I read the PR tag inside a pipeline triggered by another pipeline?

查看:44
本文介绍了如何读取由另一个管道触发的管道内的PR标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里回答了我的第一个问题

解决方案

更新

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

步骤:

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

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

  $ url =" $($ env:SYSTEM_TASKDEFINITIONSURI)$ env:BUILD_PROJECTID/_apis/git/repositories/$($ env:BUILD_REPOSITORY_NAME)/pullRequests/$($ env:BUILD_PULLREQUEST_ID)/labels-version = 5.1-preview.1"$ response = Invoke-RestMethod -Uri $ url -Method Get -Headers @ {授权=承载者$ env:SYSTEM_ACCESSTOKEN";}写主机"## vso [task.setvariable variable = PullRequestTag; isOutput = true] $($ response.value.name)" 

d .将引用名称配置为PS并添加任务cmd以输出标签.

CMD脚本:

  echo $(PS.PullRequestTag) 

e .创建拉取请求并添加标签结果:

Update2

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

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

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 @ {授权=承载者$ env:SYSTEM_ACCESSTOKEN";}写主机"## vso [task.setvariable variable = PullRequestTag; isOutput = true] $($ response.value.name)" 

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

  $ url ="$($ env:SYSTEM_TEAMFOUNDATIONSERVERURI)$ env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/55?api-version = 5.1"写主机"URL:$ url"$ pipeline = Invoke-RestMethod -Uri $ url -Headers @ {授权=承载者$ env:SYSTEM_ACCESSTOKEN";}写主机管道= $($管道| ConvertTo-Json-深度100)".#将一个名为PullRequestID的现有变量更新为其新的值提取请求ID$ pipeline.variables.PullRequestID.value = $($ env:SYSTEM_PULLREQUEST_PULLREQUESTID)#### ******************更新修改的对象**************************$ json = @($ pipeline)|ConvertTo-Json-深度99$ updatedef = Invoke-RestMethod -Uri $ url -Method Put -Body $ json -ContentType" application/json"-标头@ {Authorization ="Bearer $ env:SYSTEM_ACCESSTOKEN"}写主机" ========================================================="写主机"Varialbe'PullRequestID'的值更新为"$ updatedef.variables.PullRequestID.value写主机" =========================================================" 

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:

解决方案

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

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 Result:

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)

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

这篇关于如何读取由另一个管道触发的管道内的PR标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆