iasksillyquestions
iasksillyquestions

Reputation: 5689

passing a variable into a templates parameter

I have a template which has a parameter 'enableVM1' of type boolean.

Simply, I want this parameter to be set by an expression. I want this expression to resolve at runtime since the data is retrieved by an earlier step.

  - stage: Build_Tenant_Refresh
    displayName: "Destroying Tenant VM"
    variables:
       vm1ActiveFlip: $[ not(eq(stageDependencies.Shutdown_Tenant.Setup.outputs['Identify_built_VM.vm1Active'],'True')) ] 
    jobs:
    - template: tenant-infrastructure-plan.yml
      parameters:
         enableVM1: <<ANY EXPRESSION WHICH I'D EXPECT TO RESOLVE TO A BOOL>>

When I press the run button on the pipeline I am immediately told that enableVM1s value is not a boolean. This suggests that the a parameters are evaluated at parse/compile time rather than run time. Is this true?

I was intending for the expression to be: $[vm1ActiveFlip] (referencing the variable defined at the stage).

I tried lots of variants for the expression including:

$[eq('vm1ActiveFlip','True')] $[eq('True','True')]

Is it possible to achieve what I need?

Upvotes: 1

Views: 529

Answers (1)

Hugh Lin
Hugh Lin

Reputation: 19481

I tested enableVM1: $[eq(variables['Build.SourceBranch'],'refs/heads/main')] and reproduced your issue:

enter image description here

To solve this , you need to use compile-time expressions (${{ <expression> }}). This is because if you use runtime expression, then when you click the run button, the expression has not been parsed as a boolean value and is judged as a string.

In a compile-time expression (${{ <expression> }}), you have access to parameters and statically defined variables. In a runtime expression ($[ <expression> ]), you have access to more variables but no parameters.

This is stated in this document,please refer to it.

Update:

As workaround, using job output variables and introducing a dependsOn to the template. For details , please refer to this document.

Upvotes: 1

Related Questions