yaml - 在运行时设置 YAML 模板变量在条件或表达式中不可用

标签 yaml azure-pipelines devops

无论我采取什么方法,我似乎都无法使用在运行时在表达式或条件中设置的 yaml 变量的值。

概要:调用管道调用模板管道。模板管道设置要在条件中使用的变量。即

- ${{ if and(eq(parameters.checkinUpdatedSubmoduleCommits, true), eq(variables['isDevBranch'], true)) }}:
#<Do some work>

调用Pipeline.yaml

parameters:
  - name: buildConfiguration
    displayName: Build Configuration
    type: string
    default: Release
    values:
      - Release
      - Debug
  - name: checkinUpdatedSubmoduleCommits
    displayName: Do you want to checkin updated submodule commits on the Dev branch?
    type: boolean
    default: false  

resources:
  repositories:
    - repository: Repo-MyProj
      type: git
      name: Org/MyProj  
      ref: DevBranch
      trigger:
      - DevBranch

variables:
  reporef: $[ resources.repositories['Repo-MyProj'].ref]

pool:
  name: MyBuildAgent

trigger: none

jobs:
  - template: 'template.yaml'
    parameters:
      buildConfiguration: '${{ parameters.buildConfiguration }}'
      checkinUpdatedSubmoduleCommits: '${{ parameters.checkinUpdatedSubmoduleCommits }}'

模板.yaml

parameters:
  - name: buildConfiguration
    type: string
  - name: checkinUpdatedSubmoduleCommits
    type: boolean
jobs:
- job: A
  steps:
  - powershell: |      
      $isRepoRefEq = "$(reporef)" -eq "refs/heads/DevBranch"
      Write-Host "isRepoRefEq = $isRepoRefEq"
      $val = $false
      if( "$(reporef)" -eq "refs/heads/DevBranch") {
        $val = $true          
      }
      Write-Host val = $val
      Write-Host "##vso[task.setvariable variable=isDevBranchOut;isOutput=true]$val"         
    name: 'setVar'

- job: MainJob
  dependsOn: A
  variables:
    group: TMPL_VARS
    statVar: 'testValue'
    isDevBranch: $[ dependencies.A.outputs['setVar.isDevBranchOut'] ]
  continueOnError: false
  workspace:
    clean: outputs
  steps:
  - script: |
      echo templateVarRepoRef = $(reporef)
      echo isEqTmplRepoRef = ${{ eq(variables['reporef'], 'refs/heads/DevBranch') }}      
      echo pwsh isDevBranch = $(isC5DevBranch)
      echo value1 isDevBranch = ${{ variables['isDevBranch'] }}
      echo value2 isDevBranch = ${{ variables.isDevBranch }}
      echo istrue isDevBranch = ${{ eq(variables['isDevBranch'], true) }}
      echo value1 statVar = ${{ variables['statVar'] }}
      echo value2 statVar = ${{ variables.statVar }}
      echo eq statVar = ${{ eq(variables['statVar'], 'testValue') }}
    displayName: Display Parameters' Values

作业 A 的输出:setVar 任务

isRepoRefEq = True
val = True
Finishing: setVar

显示参数值的输出

templateVarRepoRef = refs/heads/DevBranch #Correct
isEqTmplRepoRef = False      #Wrong
pwsh isDevBranch = True #Correct
value1 isDevBranch = $[ dependencies.A.outputs['setVar.isDevBranchOut'] ] #Wrong
value2 isDevBranch = $[ dependencies.A.outputs['setVar.isDevBranchOut'] ] #Wrong
istrue isDevBranch = False #Wrong
value1 statVar = testValue #Correct
value2 statVar = testValue #Correct
eq statVar = True #Correct

由于模板语法 {{ }} 无法在运行时解释更改的值,因此似乎只能在条件/表达式中使用静态设置的变量和参数。宏语法 $(MyVar) 可以工作,但不能在表达式中使用。 我该如何解决这个问题?

最佳答案

谢谢,丹尼尔的评论是正确的。

Template expressions are resolved at template compile time, long before the runtime variables exist.

因此,使用提供的内置 yaml 功能似乎没有简单的方法来解决这个问题。正如 Tejas 评论提供的链接,该评论使用 hack 来替换变量值。我不知何故认为这仍然无法解决模板条件在编译时解决的问题。

我担心我们的团队错误地使用了 AZ 模板条件和表达式。我必须提高技能并引导我们走向更好的方向。

关于yaml - 在运行时设置 YAML 模板变量在条件或表达式中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72649123/

相关文章:

Azure 发布管道 - Azure Key Vault 任务 VS 变量组

azure - 如何在发布构建期间用 configSource 替换连接字符串文件?

azure - 使用 azure pipeline 标记 docker 容器

shell - 如何从远程计算机上的jenkins中的SSH将变量导出到本地计算机上的shell?

node.js - Azure DevOps Pipeline 中的 Docker 镜像中的单元测试

ruby - Vagrant Shell 配置脚本运行两次

Git merge 与 Yaml 文件冲突

kubernetes - Gitlab CI管道失败:标记问题

amazon-web-services - 带有 !Equal 和 !Ref 的 CloudFormation 文件的 YAML 错误

php - 解析 YML 时将变量从 PHP 传递到 YML 文件