Biswajit Maharana
Biswajit Maharana

Reputation: 609

Azure - creating Boolean variables and use it across multiple tasks

I want to define one Boolean variable for a stage and want to set it(True/False) inside another script in one of the task in the stage and want to use that variable as a condition in other subsequent tasks, How can I do it?

My azure pipeline is YAML based.

Upvotes: 2

Views: 13060

Answers (1)

LoLance
LoLance

Reputation: 28196

I want to define one Boolean variable for a stage and want to set it(True/False) inside another script in one of the task in the stage and want to use that variable as a condition in other subsequent tasks, How can I do it?

Here's one example:

stages:
- stage: Build
  variables: 
    Key : false
  jobs:
  - job: Build
    continueOnError: true
    steps:
    - script: echo Key:'$(Key)'

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          # Write your PowerShell commands here.

          Write-Host "##vso[task.setvariable variable=Key;]true"

    - task: CmdLine@2
      inputs:
        script: echo Key:'$(Key)'

    - task: CmdLine@2
      condition: and(succeeded(), eq(variables['Key'], 'true'))
      inputs:
        script: |
          echo Write your commands here

          echo Hello world

I have one stage variable Key, its value is false. Then in PS task I use task.setvariable to override the key's value. Note: This way the new value true is a job-scope value. If you have more than one jobs in one stage, this can't work well.

Update1:

- task: PowerShell@2
      inputs:
        filePath: 'Test.ps1'

My Test.ps1:

Write-Host "##vso[task.setvariable variable=Key;]true"

Upvotes: 6

Related Questions