如何在阶段模板级别条件运行表达式时,使用管道级变量模板?

huangapple go评论63阅读模式
英文:

How can I use a pipeline-level variable template for a conditionally run expression at a stage template level?

问题

开发人员可以在他们的源代码存储库中访问一个变量模板。流水线yaml引用这个模板并调用一个阶段模板,该模板具有多个“if”表达式,以有条件地运行一个作业,如果通过该变量模板传递了任意数量的变量。例如:

service-config.yaml

variables:
  buildDotNetCore: 'true'

App-Build-Pipeline.yaml

...
variables:
  template: service-config.yaml
...
stages:
  template: Master-CI-Template.yaml

Master-CI-Template.yaml

stages:

  • ${{ if eq(variables.buildDotNetCore, 'true') }}:
      - stage: Build
        jobs:
          - job: BuildDotNetCore
            steps:
              - task: Powershell@2
                inputs:
                targetType: 'inline'
                script: 'dir env:'
  • {{ else }}:
      - stage: Build
        jobs:
          - job: HitTheElse
            steps:
              - task: Powershell@2
                inputs:
                targetType: 'inline'
                script: 'dir env:'

这个表达式没有评估为true。由于有一个else子句,它仍然运行“dir env:”,但作业名称是HitTheElse,变量也存在。

英文:

Developers have access to a variable template in their source code repo. The pipeline yaml references this template and calls a stage template that has multiple "if" expressions to conditionally run a job if any number of variables was passed in via that variable template. As an example:

service-config.yaml

variables:
  buildDotNetCore: 'true'

App-Build-Pipeline.yaml

...
variables:
  template: service-config.yaml
...
stages:
  template: Master-CI-Template.yaml

Master-CI-Template.yaml

stages:
- ${{ if eq(variables.buildDotNetCore, 'true') }}:
  - stage: Build
    jobs:
    - job: BuildDotNetCore
      steps:
      - task: Powershell@2
        inputs:
          targetType: 'inline'
          script: 'dir env:'
- {{ else }}:
  - stage: Build
    jobs:
    - job: HitTheElse
      steps:
      - task: Powershell@2
        inputs:
          targetType: 'inline'
          script: 'dir env:'

The expression is not evaluating to true. Since I have an else clause it still runs the "dir env:" but the job name is HitTheElse and the variable is there.

如何在阶段模板级别条件运行表达式时,使用管道级变量模板?

Edit:

Giving some clarity on some points:

The screenshot I included might be causing some confusion. The issue is that the expression is not being properly expanded and interpreted:

stages:
- ${{ if eq(variables.deployDotNetCore, 'true') }}:
  - stage: Build
    jobs:
    - job: deployDotNetCoreTrue
      steps:
      - task: Powershell@2
        inputs:
          targetType: 'inline'
          script: "dir env:"
- ${{ else }}:
  - stage: Build
    jobs:
    - job: HitTheElse
      steps:
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: "dir env:; echo $(deployDotNetCore)"

The screenshot in the flow diagram where I show DEPLOYDOTNETCORE as “true”, to me indicates that the variable template is indeed being ingested and the variable created at the global level, but when trying to use that same variable, that is being defined at the pipeline level, it is not being expanded in the "if" expression:

- ${{ if eq(variables.deployDotNetCore, 'true') }}:

In the stages template. That comes back as eg(null, ‘true’) which results in it going to the - ${{ else }}: loop which puts in a job called “HitTheElse”. In the last picture of the diagram, same one with the variables output, you can see that the “HitTheElse” job was put in place.

答案1

得分: 1

我可以通过参数将整个 'variables' 对象传递给模板。然后,我将该变量引用为 'parameters.ParentVars.deployDotNetCore'。

英文:

I was able to pass the entire 'variables' object to the template via a parameter. I then reference the variable as 'parameters.ParentVars.deployDotNetCore'.

huangapple
  • 本文由 发表于 2023年2月10日 13:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定