Typical task condition statements look like this PowerShell task:
- task: PowerShell@2 displayName: Rename output spspkg files to add version information. condition: eq(variables['Build.SourceBranch'], 'refs/heads/main') ...
Below are example condition statements. Note that you cannot use macro variable format such as $(Build.SourceBranch)
. Case is ignored on the variable names.
condition: eq(variables.CG_TARGET_FORTIFY, 'true') condition: eq(variables['CG_TARGET_FORTIFY'], 'true') condition: eq(variables['CG_TARGET_fortify'], 'true') # Multiline conditions condition: > and ( succeeded(), or ( eq( variables['Build.SourceBranch'], 'refs/heads/main'), eq( variables['Build.SourceBranch'], 'refs/heads/dev') ) )
Other useful conditions are available from Microsoft.
# Run if the source branch is main and the parent or preceding stage, job, or step succeeded. condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main')) # Run if the source branch isn't main, and the parent or preceding stage, job, or step succeeded. and(succeeded(), ne(variables['Build.SourceBranch'], 'refs/heads/main')) # Run if a variable is set to true, even if the parent or preceding stage, job, or step failed or was canceled. eq(variables['System.debug'], true)
Template Parameter Conditions
(From Microsoft documents) Parameter expansion happens before conditions are considered. Therefore, when you declare a parameter in the same pipeline as a condition, you can embed the parameter inside the condition. The script in the following YAML runs because parameters.doThing
is true.
parameters: - name: doThing default: true type: boolean steps: - script: echo I did a thing condition: and(succeeded(), ${{ eq(parameters.doThing, true) }})
However, this is a questionable example on their part since if you pass a variable to this template, it will fail since variables ARE ALWAYS STRINGS. It will not do an implicit conversion from string to boolean so it is better to do your own manual conversion.
In the following example, the attempt to pass anything other than “true
” or “false
” will generate an error:
/azure-pipelines.yml (Line: 134, Col: 24): The 'force_fortify' parameter value '$(TargetFortify)' is not a valid Boolean.
# Calling YAML file... - template: Devops/Generate-FullVersionInfoV1.yml@CustomBuildResources parameters: ver_mmb: $(CG_VERSION) revision: $(CG_REVISION) force_fortify: $(TargetFortify) # Template File... - name: force_fortify type: boolean default: false