Is there a way to use varaiables defined in the pipeline(dashboard) to use conditional insertion on a YAML template for steps:
I mean i have this steps:
- ${{ if eq(variables.['somevar'], '')}}:
- task: Bash@3
displayName: Var does not being declared
inputs:
targetType: 'inline'
script: |
echo "This is a step where the var 'somevar' does not being declared'
- task: Bash@3
displayName: Another Step
inputs:
targetType: 'inline'
script: |
echo "This is another step where the var 'somevar' does not being declared'
This should run when the variable is not being declared
- ${{ if ne(variables.['somevar'], '')}}:
- task: Bash@3
displayName: Var is being declared
inputs:
targetType: 'inline'
script: |
echo "This is a step where the var 'somevar' is being declared with some value'
- task: Bash@3
displayName: Another Step
inputs:
targetType: 'inline'
script: |
echo "This is another step where the var 'somevar' is being declared with some value'
This should run when the variable is declared
I know that it exist the Runtime parameters , but i don't want to use them every time that i run the pipeline (manually).I want that some pipelines run some steps, when i have declared the variable, and some other pipelines don't run some steps when the variable is not being declared.
I also know that it exist the condition inside every step like
condition: eq(variables.somevar, 'value')
But i want to use conditional insertion to run in some cases, many steps like in the examples above. Not in just one step
I want that some pipelines run some steps, when i have declared the variable, and some other pipelines don't run some steps when the variable is not being declared.
Why not use the Runtime parameters? With the Runtime parameters used we can simply achieve your requirement.
Whatever, to achieve that by using the variables we can try to Specify condition for the Yaml templates:
Create Yaml templates :
# File: somevar-nondeclared.yml
steps:
- task: Bash@3
displayName: Var does not being declared
inputs:
targetType: 'inline'
script: |
echo "This is a step where the var 'somevar' does not being declared"
- task: Bash@3
displayName: Another Step
inputs:
targetType: 'inline'
script: |
echo "This is another step where the var 'somevar' does not being declared"
And
# File: somevar-declared.yml
steps:
- task: Bash@3
displayName: Var is being declared
inputs:
targetType: 'inline'
script: |
echo "This is a step where the var 'somevar' is being declared with some value"
- task: Bash@3
displayName: Another Step
inputs:
targetType: 'inline'
script: |
echo "This is another step where the var 'somevar' is being declared with some value"
And the azure-pipelines.yml
for your reference :
# File: azure-pipelines.yml
trigger: none
jobs:
- job: Linux
pool:
vmImage: 'ubuntu-latest'
steps:
- template: somevar-nondeclared.yml
condition: eq(variables['somevar'], '')
- job: Windows
pool:
vmImage: 'windows-latest'
steps:
- template: somevar-declared.yml
condition: ne(variables['somevar'], '')
Thus, It will run windows job if you declared the variable when run pipeline. Otherwise it will run the Linux job in this example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With