Rozmaryn
Rozmaryn

Reputation: 331

Condition OR with variables in Azure Devops Pipeline

I have three conditions as variables (isMaster, isRelease, isHotfix):

variables:
  isMaster: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/master')]
  isRelease: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')]
  isHotfix: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/hotfix/')]
  checkCondition: $[or(variables.isMaster, variables.isRelease)]

And the problem is when I take two 'false' for the OR condition (like checkCondition). I should get 'false' but for some reason I get 'true'.

  - task: CmdLine@2
       inputs:
         script: |
           echo %BUILD_SOURCEBRANCH%
           echo %ISMASTER%
           echo %ISRELEASE%
           echo %ISHOTFIX%
           echo %CHECKCONDITION%
       condition: or(variables.isMaster ,variables.isRelease)
...
Result:
...
refs/heads/someBranch
False
False
False
True
Finishing: CmdLine

Anyone have an idea why the condition gives the wrong result? Thank You!

Upvotes: 9

Views: 43805

Answers (3)

Bhellstream
Bhellstream

Reputation: 41

As previously stated the or needs it to be an expression but if you want it a bit more readable and only have the evaluation of the expression once in your variables section you could do it like this:

or(eq(variables['isMaster'], 'true'), eq(variables['isRelease'], 'true'))

Example with a and containing a or with an and statement in it:

- task: AzureCLI@2
  displayName: Download feed.xml
  inputs:
    azureSubscription: vanillaResourceConnection
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      az storage blob download --account-name $(storageAccount) --container-name '$web' --name feed.xml --file .ado\downloadedFeed.xml
   condition: and(succeeded(), or(eq(variables['isMaster'], 'true'), and(eq(variables['isTag'], 'true'), eq(variables['releaseCheck.isRelease'], 'true'))))

Upvotes: 4

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31003

When use OR function, you need to use or(expression, expression), then it will cast expression to Boolean for evaluation. If you use or(variables.isMaster ,variables.isRelease), there are two characters, no expression to cast to Boolean. You need to use or(startsWith(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')) instead.

Upvotes: 21

Krzysztof Madej
Krzysztof Madej

Reputation: 40533

It looks that this is possible as it is written here:

Conditions are evaluated to decide whether to start a stage, job, or step. This means that nothing computed at runtime inside that unit of work will be available. For example, if you have a job which sets a variable using a runtime expression using $[ ] syntax, you can't use that variable in your custom condition.

but it can't be used anywhere. So if you have steps on your root level it will not work, but it should if you put this in this way

variables:
  isMaster: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/master')]
  isRelease: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')]
  isHotfix: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/hotfix/')]
  checkCondition: $[or(variables.isMaster, variables.isRelease)]

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: A
  jobs:
  - job: A1
    steps:
      - script: echo Hello Stage A!

- stage: B
  condition:  or(variables.isMaster ,variables.isRelease)
  jobs:
  - job: B1
    steps:
    - task: CmdLine@2
      inputs:
        script: |
          echo %BUILD_SOURCEBRANCH%
          echo %ISMASTER%
          echo %ISRELEASE%
          echo %ISHOTFIX%
          echo %CHECKCONDITION%
      condition: or(variables.isMaster ,variables.isRelease)
    - task: CmdLine@2
      inputs:
        script: |
          echo %BUILD_SOURCEBRANCH%
          echo %ISMASTER%
          echo %ISRELEASE%
          echo %ISHOTFIX%
          echo %CHECKCONDITION%

Upvotes: 0

Related Questions