0
votes

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!

2
Can you try condition: or(variables.isRelease ,variables.isMaster)? It can make difference developercommunity.visualstudio.com/content/problem/1236160/… - Krzysztof Madej
@KrzysztofMadej that would be hilarious. I have had similar issues in the past. You can try wrap your or condition in: and(succeeded(), <your or condition>). - Mansoor
Unfortunately it is still incorrect (I'm getting 'true'). - Rozmaryn
But if I put full conditions in OR $[or(startsWith(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))], I am getting the correct 'false' :/ - Rozmaryn

2 Answers

6
votes

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.

0
votes

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%