2

I have declared a variable group "myVariableGroup" and inside this I have a variable name "myVariable" with default value = true.

Pipeline looks like this

  • I declared the group variable name in variables
  • first step, I debug the result of myVariable, working fine, I get the value true
  • second step, I pass the variable to a template parameter
variables:
  - group: myVariableGroup

steps:
  - script: "echo myVariableFROM group vars = $(myVariable)"
    displayName: debug groupvars
  - template: "./.azure-devops/some-template.yml"
    parameters:
      myVariableParam: $(myVariable)

Template looks like this

  • parameter declared with default value false
  • debug the parameter value, result being true (it works)
  • I'm using an if statement to determine if the bash script should run or not, but this is not working
parameters:
  - name: myVariableParam
    type: boolean
    default: false

  - script: "echo parameters.myVariableParam = ${{ parameters.myVariableParam}}"
    displayName: debug parameters.myVariableParam

  - ${{ if eq(parameters['myVariableParam'], true) }}:
      - script: "echo parameters.myVariableParam= ${{ parameters.myVariableParam}}"

Questions:

  1. Is it possible to use group variables inside if statements or the IF is interpreted before running the pipeline and value is not defined?
  2. The only way to achieve this is via conditions? I have a corner case where I don't really want to use those
  3. Maybe I miss something pretty obvious and can anybody help me with this?
Alex
  • 130
  • 10
  • Try passing the variable using the template expansion expression instead of the runtime expression. e.g. `myVariableParam: $(myVariable)` => `myVariableParam: ${{ variables.myVariable }}` – jessehouwing Feb 16 '23 at 16:38
  • Thanks for your suggestion. Unfortunately I get the same result. – Alex Feb 16 '23 at 18:13

1 Answers1

0

Based on the @Alex's comments, updated the template to use condition.

I know this seems a bit hacky but if doesn't seem to be evaluated properly when using with boolean. See this answer for details

parameters:
  myVariableParam: 'false'

steps:
  - script: |
      echo "##vso[task.setvariable variable=myVariableNew;]$myVariableEnv"
    displayName: setting myVariableNew to parameter.myVariableParam
    env:
      myVariableEnv: ${{parameters['myVariableParam']}}
  
  - script: |
      echo "myVariableNew: $(myVariableNew)"
    displayName: check myVariableNew

  - script: |
      echo "I should run when myVariableNew is $(myVariableNew)"
    condition: eq(variables['myVariableNew'], 'true')

myVariable set to true

enter image description here

myVariable set to false

enter image description here

Sibtain
  • 1,436
  • 21
  • 39
  • Thanks for your answer, I've just implemented this modification and the result is still NOK. The task gets runed regardless of the value (true or false) And I can see in the echo that the result of parameter is false: Generating script. Script contents: echo parameters.myVariableParam = false ========================== Starting Command Output =========================== /usr/bin/bash --noprofile --norc /home/vsts/work/_temp/5cb51ca5-aea6-4814-a173-b901e099d96e.sh parameters.myVariableParam = false – Alex Feb 16 '23 at 18:15
  • @Alex sorry for the mix up, I have updated my answer with the new template and added the run screenshots – Sibtain Feb 16 '23 at 21:12
  • @Alex have you tried my updated answer? If it helped you, please upvote or accept if it answers your question – Sibtain Feb 22 '23 at 14:37