Azure Pipelines "A template expression is not allowed in this context"
You used a compile-time template expression ${{ }} somewhere the schema only allows a literal or a runtime expression. Azure has three expression syntaxes and they are not interchangeable.
What this error means
The pipeline fails to compile with A template expression is not allowed in this context, pointing at a ${{ ... }} you placed in a field that is evaluated later, at runtime.
/azure-pipelines.yml (Line: 20, Col: 18): A template expression is not
allowed in this contextCommon causes
Compile-time syntax in a runtime-only field
Template expressions ${{ }} are expanded before the run starts. Fields that depend on runtime state (like a step condition referencing a prior job result) must use runtime $[ ] or macro $( ) syntax instead.
Mixing the three expression types
Azure has ${{ }} (template, compile time), $[ ] (runtime expression), and $( ) (macro). Using the wrong one for the context - e.g. ${{ }} to read an output variable set during the run - is rejected.
How to fix it
Use a runtime expression for runtime values
When the value is only known during the run (output vars, conditions on prior steps), switch to $[ ].
variables:
isMain: $[ eq(variables['Build.SourceBranch'], 'refs/heads/main') ]Reserve template expressions for parameters
- Use
${{ }}forparameters.*, conditional insertion, and loops - all resolved before the run. - Use
$( )to substitute a variable into a script or argument string. - Use
$[ ]for variable definitions and conditions that depend on runtime results.
How to prevent it
- Learn the three expression syntaxes and where each is legal.
- Keep parameter logic in
${{ }}and runtime logic in$[ ]. - Validate templates in the pipeline editor before merging.