10

Here's how I'm trying to accomplish inserting a template conditionally. Per requirement, I would like to either call fresh-deploy.yml or update.yml based on a pipeline variable supplied at run time. The user can edit a variable called 'freshInstall' to either true or false.

The main pipeline (entrypoint):

# azure-pipelines.yml
variables:
  shouldUpdate: 'false'

jobs:
  - job: TestJob
    pool:
      name: "Vyas' Local Machine"
    steps:
    - checkout: none
    - template: ./testIf.yml
      parameters:
        freshInstall: $(freshInstall)

The testif.yml:

# testIf.yml
parameters:
  - name: freshInstall
    type: string  # Can't be boolean as runtime supplied variable values ARE strings

steps:

  # set a preexisting variable valued 'false' to 'true'
  - powershell: |
      $shouldUpdate = 'true'
      Write-Host "##vso[task.SetVariable variable=shouldUpdate]$shouldUpdate"
    displayName: 'Set Should Update to $(shouldUpdate)'

  # Check if the parameter 'freshInstall' is passed in correctly
  - script: echo "Should freshInstall ${{ parameters['freshInstall'] }}"
    displayName: 'Is Fresh Install? ${{ parameters.freshInstall }}'

  # Should skip this
  - ${{ if eq(parameters.freshInstall, 'true') }}:
    - template: ./fresh-deploy.yml

  # Shoud include this
  - ${{ if eq(parameters.freshInstall, 'false') }}:
    - template: ./update.yml

  # Check variables vs parameters.  Include as per value set
  - ${{ if eq(variables.shouldUpdate, 'true') }}:
    - template: ./update.yml

  # Use all 3 syntaxes of variable access
  - script: echo "shouldUpdate is variables['shouldUpdate']"
    displayName: "Should Update? variables.shouldUpdate"

The mock file for fresh-deploy.yml:

# fresh-deploy.yml
steps:
  script: echo 'Kick off fresh deploy!'

The mock file for update.yml:

# update.yml
steps:
  script: echo 'Updating existing installation!'

Critical issue: The expectation is that the update.yml template is inserted and the script run when the variable 'freshInstall' is false.

Nice to know: I was also checking if I could somehow get this to work if it were a variable instead of a parameter. It'd be nice if I could be pointed to what I'm doing wrong with the variable displays.

Here's the result: enter image description here

riQQ
  • 9,878
  • 7
  • 49
  • 66
Vyas Bharghava
  • 6,372
  • 9
  • 39
  • 59
  • I did find this link that talks about why it doesn't work: Run time vs. compile time. However, it is not so clear cut https://developercommunity.visualstudio.com/content/problem/653819/yaml-pipeline-conditional-insertion-does-not-work.html – Vyas Bharghava Feb 24 '20 at 00:26
  • In the compile time, the 'Set Should Update to false' is not run yet, this means the variable `shouldUpdate`'s value is still 'false'. Change the `Should Update? ` task to - script: echo "shouldUpdate is $(shouldUpdate)" displayName: "Should Update? $(shouldUpdate)" and it will show "shouldUpdate is true" Finishing: Should Update? false, this might be easier to understand. – Yang Shen - MSFT Feb 24 '20 at 11:09
  • 1
    @YangShen-MSFT: then the following should work and it doesn't: - $[ if eq(${{ parameters.freshInstall }}, 'true') ]: - template: ./fresh-deploy.yml – Vyas Bharghava Mar 05 '20 at 19:18
  • No sir, it has to be the compile time, change it to `$[ ]` will not change the fact that this expression has to be at compile time. You shall meet the error: [Unexpected Value](https://i.stack.imgur.com/52ONe.png). – Yang Shen - MSFT Mar 10 '20 at 10:02
  • 1
    @YangShen-MSFT: It will be great if you could provide a workaround on how to accomplish conditional insertion using queue time variable values. Thanks! – Vyas Bharghava Apr 10 '20 at 21:37

2 Answers2

17

This solution now works for me:

- ${{ if eq(parameters.generateSwaggerFiles, true) }}:
    - template: generate-swagger-files.yaml
      parameters:
        appName: 'example'
        swaggerVersions:
          - v1
          - v2

Check documentation here.

Luca Perico
  • 1,335
  • 15
  • 29
0

I think your problem is now solved since runtime parameters are now allowed to be boolean. Check out Runtime Parameters. I had a similar use case and was able to achieve it using this.

raviabhiram
  • 671
  • 2
  • 8
  • 21