gpt4 book ai didi

azure - 如何根据 System.PullRequest.TargetBranch 变量将不同参数发送到 Azure 管道模板

转载 作者:行者123 更新时间:2023-12-03 05:32:04 26 4
gpt4 key购买 nike

我有一个管道模板,它应该根据拉取请求目标分支接收不同的输入。

模板:

parameters:
- name: BUILD_FOLDER
type: string

steps:
- script: |
echo "Build folder: ${{ parameters.BUILD_FOLDER }}"
displayName: 'Echo Build folder'

管道 YAML。

trigger: none

pr:
branches:
include:
- '*'

pool:
vmImage: ubuntu-latest

steps:
- template: templates/template.yml
parameters:
${{ if contains(variables['System.PullRequest.TargetBranch'], 'master') }}:
BUILD_FOLDER: base
${{ if not(contains(variables['System.PullRequest.TargetBranch'], 'master')) }}:
BUILD_FOLDER: $(System.PullRequest.TargetBranch)

我尝试这样做,但总是与 ${{ if not(contains(variables['System.PullRequest.TargetBranch'], 'master')) }}: 一起使用目标分支是拉取请求中的主分支。还有其他方法可以做到这一点吗?

存储库也在 GitHub 中。

提前致谢。

最佳答案

我可以重现同样的问题。这是因为预定义变量 System.PullRequest.TargetBranch 无法在编译时求值。它可以在运行时进行评估(包装在 $() 中),没有任何问题。包裹在 ${{}} 中的表达式将在构建编译时进行评估。请参阅 Runtime expression syntax

这些在 document 中标记为not available in template的变量无法在编译时进行解析。该文档引起了一些困惑,因为它没有明确说明那些变量不能在编译时解析。

因为模板是在编译时评估的。因此,包裹 ${{}} 的变量 System.PullRequest.TargetBranch 被评估为空字符串。

我使用下面的 yaml 进行了测试。 powershell 任务已执行:

- ${{ if eq(variables['System.PullRequest.TargetBranch'], '') }}:
- powershell: echo "i will out put empty"

解决方法是在额外的 powershell 任务中设置 variable value by script,如 Krzysztof Madej 所提到的。我稍微改变了你的 yaml 文件。见下文:

trigger: none
pr:
branches:
include:
- '*'
pool:
vmImage: ubuntu-latest
steps:
- powershell: |
$targetBranch = "$(System.PullRequest.TargetBranch)"
if($targetBranch -eq "master"){
Write-Host "##vso[task.setvariable variable=BUILD_FOLDER;]base"
}
if($targetBranch -ne "master"){
Write-Host "##vso[task.setvariable variable=BUILD_FOLDER;]$targetBranch"
}

- template: templates/template.yml
parameters:
BUILD_FOLDER: $(BUILD_FOLDER)

关于azure - 如何根据 System.PullRequest.TargetBranch 变量将不同参数发送到 Azure 管道模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64691764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com