Reputation: 387
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:
variables:
buildDotNetCore: 'true'
...
variables:
template: service-config.yaml
...
stages:
template: 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.
Upvotes: 0
Views: 136
Reputation: 387
I was able to pass the entire 'variables' object to the template via a parameter. I then reference the variable as 'parameters.ParentVars.deployDotNetCore'.
Upvotes: 1