Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

How to pass a Pipeline variable as boolean to a template?

I am trying to pass a Pipeline variable (enable_datasync_job) that I have defined using the UI: enter image description here

to a template that is used within my main 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: $(enable_datasync_job)

The Common-YAML template defines that variable as boolean:

parameters:
...
  - name: enable_datasync_job
    type: boolean
    default: false

When I try to run my main pipeline, it currently breaks as it completes that I am not passing a boolean value to my template

enter image description here

I know that all pipeline variables as of type string. How do I convert that string to a boolean so that my template accepts it?

Upvotes: 1

Views: 1041

Answers (1)

promicro
promicro

Reputation: 1646

If you define the parameter that is in the azure-pipelines.yml also as boolean you will get a checkbox (boolean) instead of inputbox (string): example run

You should be able to pass this along the Common-YAML.

Example code

The azure-pipelines.yml:

trigger:
- main

pool:
  vmImage: ubuntu-latest

parameters:
  - name: enable_datasync_job
    type: boolean
    default: false

extends:
  template: boolean.yml
  parameters:    
    enable_datasync_job: ${{ parameters.enable_datasync_job }}

The azure-pipelines.yml, without extends:

trigger:
- main

pool:
  vmImage: ubuntu-latest

parameters:
  - name: enable_datasync_job
    type: boolean
    default: false

steps:
 - template: boolean.yml
   parameters:
    enable_datasync_job: ${{ parameters.enable_datasync_job }}


The boolean.yml (Common-YAML):

parameters:
  - name: enable_datasync_job
    type: boolean
    default: false

steps:
- script: |
   echo the value is ${{ parameters.enable_datasync_job }}

The result

result

Upvotes: 3

Related Questions