0

I have defined a Pipeline variable using the UI interface for my pipeline: enter image description here

And I am consuming that variable in my pipeline azure-pipelines.yml:

name: MyRepo.Deployment.$(date:yy)$(DayOfYear)$(rev:.r)
...
jobs:
  - job:
    steps:
...
      - template: azure-pipelines.yml@Common-YAML
        parameters:
          ...
          enable_datasync_job: ${{ eq(variables.enable_datasync_job, 'true') }}

That variable then gets passed to a template that expects a boolean. The issue is that when I run the pipeline and the azure-pipelines.yml@Common-YAML template gets expanded, the value of enable_datasync_job is always False

What am I missing here? Is it because both my pipeline and the template share the same variable/parameter name?

Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126

1 Answers1

1

Assume that enable_datasync_job is defined in the UI as false

Option 1

To override the above variable that is defined in the UI, you can add this bash task to your pipeline

- bash: |
    echo $(ENABLE_DATASYNC_JOB) # outputs as false
    echo "##vso[task.setvariable variable=enable_datasync_job;]true" # override
- bash: |
    echo $(ENABLE_DATASYNC_JOB) # outputs as true

Since your template accepts the parameter of type boolean, you would have something like the below

# template.yml
parameters:
- name: enable_datasync_job
  type: boolean

steps:
- bash: |
    echo $(ENABLE_DATASYNC_JOB) 

And your main yml file

# azure-pipelines.yml
steps:
  - bash: |
      echo "##vso[task.setvariable variable=enable_datasync_job;]true" # override
  - template: template.yml
    parameters:
      enable_datasync_job: $(enable_datasync_job) # fail

But the above azure-pipelines.yml will fail because of typecasting of string into boolean. Check this SO post for more details

/azure-pipelines.yml (Line: 11, Col: 28): The 'enable_datasync_job' parameter value '$(enable_datasync_job)' is not a valid Boolean.

According to the above post, you can change the data type of parameter to string in your template.yml

parameters:
- name: enable_datasync_job
  type: string

Final result of option 1

# template.yml
parameters:
- name: enable_datasync_job
  type: string

steps:
- bash: |
    echo $(ENABLE_DATASYNC_JOB) #outputs true
# azure-pipelines.yml
steps:
  - bash: |
      echo "##vso[task.setvariable variable=enable_datasync_job;]true" # override
  - template: template.yml
    parameters:
      enable_datasync_job: $(enable_datasync_job)

Option 2

If you are looking to just pass the value true to your template without overriding the pipeline variable, you can reference the value that you passed like this

# template.yml
parameters:
- name: enable_datasync_job
  type: boolean

steps:
- bash: |
    echo ${{ parameters.enable_datasync_job }} # outputs true
    echo $(ENABLE_DATASYNC_JOB) # outputs false
# azure-pipelines.yml
steps:
  - template: template.yml
    parameters:
      enable_datasync_job: true

The reason why it was returning False for you is because you have used this expression while passing value to the parameter

${{ eq(variables.enable_datasync_job, 'true') }}

According to the documentation, the eq function

Evaluates True if parameters are equal

You tried to evaluate if the UI variable was equal to true instead of setting it

The expression that you used above will always return False because false(UI variable value) is not equal to true(set in the expression)

pnkjkmr469
  • 195
  • 1
  • 8
  • Thank you very much for the detailed explanation! It does help! I have a couple of questions. 1. Isn't that expression `${{ eq(variables.enable_datasync_job, 'true') }}` return a `boolean`? That was my assumption. I thought that way it would set the my templates's `enable_datasync_job` to true or false depending on what value I set in the pipeline variable's UI. 2. If I were to make the above work, what changes I need to make? – Georgi Koemdzhiev Jan 20 '23 at 09:15