英文:
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'.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论