2

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

enter image description here


- ${{ 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

enter image description here


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

Zahid
  • 91
  • 1
  • 10

1 Answers1

4

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.

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • I think runtime parameters are used , when you just want to skip a job or a sequence of steps that are not working but you need to run the pipeline. Example a pipeline that have an Integraion a Migration Job and a Deploy Job , Sometimes you need to deliver as quick as possible the Deploy so manually you skipp the integration job with Runtime parameters . But what about pipelines that you want to run with ci/cd where you simply don't have that job Integration but in other pipelines similar you have it – Zahid Jul 20 '20 at 14:46
  • Thanks for the clarification. So, you mean that we can only specify the parameters when run the pipeline manually, but for CI/CD, no way to specify them, right? However, according to your description, even with using the variable we still have to declare the variable manually. It's still not available for CI/CD. Please correct me if any misunderstanding. – Andy Li-MSFT Jul 21 '20 at 02:03