applyTo: .pipelines/**/*.{yml,yaml}
OneBranch Pipeline Condition Syntax
Overview
Azure Pipelines (OneBranch) uses specific syntax for referencing variables and parameters in condition expressions. Using the wrong syntax will cause conditions to fail silently or behave unexpectedly.
Variable Reference Patterns
In Condition Expressions
β Correct Pattern:
condition: eq(variables['VariableName'], 'value')
condition: or(eq(variables['VAR1'], 'true'), eq(variables['VAR2'], 'true'))
condition: and(succeeded(), eq(variables['Architecture'], 'fxdependent'))
β Incorrect Patterns:
# Don't use $(VAR) string expansion in conditions
condition: eq('$(VariableName)', 'value')
# Don't use direct variable references
condition: eq($VariableName, 'value')
In Script Content (pwsh, bash, etc.)
β Correct Pattern:
- pwsh: |
$value = '$(VariableName)'
Write-Host "Value: $(VariableName)"
In Input Fields
β Correct Pattern:
inputs:
serviceEndpoint: '$(ServiceEndpoint)'
sbConfigPath: '$(SBConfigPath)'
Parameter References
Template Parameters (Compile-Time)
β Correct Pattern:
parameters:
- name: OfficialBuild
type: boolean
default: false
steps:
- task: SomeTask@1
condition: eq('${{ parameters.OfficialBuild }}', 'true')
Note: Parameters use ${{ parameters.Name }} because they're evaluated at template compile-time.
Runtime Variables (Execution-Time)
β Correct Pattern:
steps:
- pwsh: |
Write-Host "##vso[task.setvariable variable=MyVar]somevalue"
displayName: Set Variable
- task: SomeTask@1
condition: eq(variables['MyVar'], 'somevalue')
Common Scenarios
Scenario 1: Check if Variable Equals Value
- task: DoSomething@1
condition: eq(variables['PREVIEW'], 'true')
Scenario 2: Multiple Variable Conditions (OR)
- task: DoSomething@1
condition: or(eq(variables['STABLE'], 'true'), eq(variables['LTS'], 'true'))
Scenario 3: Multiple Variable Conditions (AND)
- task: DoSomething@1
condition: and(succeeded(), eq(variables['Architecture'], 'fxdependent'))
Scenario 4: Complex Conditions
- task: DoSomething@1
condition: and(
succeededOrFailed(),
ne(variables['UseAzDevOpsFeed'], ''),
eq(variables['Build.SourceBranch'], 'refs/heads/master')
)
Scenario 5: Built-in Variables
- task: CodeQL3000Init@0
condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
- step: finalize
condition: eq(variables['Agent.JobStatus'], 'SucceededWithIssues')
Scenario 6: Parameter vs Variable
parameters:
- name: OfficialBuild
type: boolean
steps:
# Parameter condition (compile-time)
- task: SignFiles@1
condition: eq('${{ parameters.OfficialBuild }}', 'true')
# Variable condition (runtime)
- task: PublishArtifact@1
condition: eq(variables['PUBLISH_ENABLED'], 'true')
Why This Matters
String Expansion $(VAR) in Conditions:
- When you use
'$(VAR)'in a condition, Azure Pipelines attempts to expand it as a string - If the variable is undefined or empty, it becomes an empty string
'' - The condition
eq('', 'true')will always be false - This makes debugging difficult because there's no error message
Variables Array Syntax variables['VAR']:
- This is the proper way to reference runtime variables in conditions
- Azure Pipelines correctly evaluates the variable's value
- Undefined variables are handled properly by the condition evaluator
- This is the standard pattern used throughout Azure Pipelines
Reference Examples
Working examples can be found in:
.pipelines/templates/linux.yml- Build.SourceBranch conditions.pipelines/templates/windows-hosted-build.yml- Architecture conditions.pipelines/templates/compliance/apiscan.yml- CODEQL_ENABLED conditions.pipelines/templates/insert-nuget-config-azfeed.yml- Complex AND/OR conditions
Quick Reference Table
| Context | Syntax | Example |
|---|---|---|
| Condition expression | variables['Name'] |
condition: eq(variables['PREVIEW'], 'true') |
| Script content | $(Name) |
pwsh: Write-Host "$(PREVIEW)" |
| Task input | $(Name) |
inputs: path: '$(Build.SourcesDirectory)' |
| Template parameter | ${{ parameters.Name }} |
condition: eq('${{ parameters.Official }}', 'true') |
Troubleshooting
Condition Always False
If your condition is always evaluating to false:
- Check if you're using
'$(VAR)'instead ofvariables['VAR'] - Verify the variable is actually set (add a debug step to print the variable)
- Check the variable value is exactly what you expect (case-sensitive)
Variable Not Found
If you get errors about variables not being found:
- Ensure the variable is set before the condition is evaluated
- Check that the variable name is spelled correctly
- Verify the variable is in scope (job vs. stage vs. pipeline level)
Best Practices
- Always use
variables['Name']in conditions - This is the correct Azure Pipelines pattern - Use
$(Name)for string expansion in scripts and inputs - Use
${{ parameters.Name }}for template parameters (compile-time) - Add debug steps to verify variable values when troubleshooting conditions
- Follow existing patterns in the repository - grep for
condition:to see examples
Common Mistakes
β Mistake 1: String expansion in condition
condition: eq('$(PREVIEW)', 'true') # WRONG
β Fix:
condition: eq(variables['PREVIEW'], 'true') # CORRECT
β Mistake 2: Missing quotes around parameter
condition: eq(${{ parameters.Official }}, true) # WRONG
β Fix:
condition: eq('${{ parameters.Official }}', 'true') # CORRECT
β Mistake 3: Mixing syntax
condition: or(eq('$(STABLE)', 'true'), eq(variables['LTS'], 'true')) # INCONSISTENT
β Fix:
condition: or(eq(variables['STABLE'], 'true'), eq(variables['LTS'], 'true')) # CORRECT