Skip to content
Latchkey

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 DevOps
/azure-pipelines.yml (Line: 20, Col: 18): A template expression is not
allowed in this context

Common 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 $[ ].

azure-pipelines.yml
variables:
  isMain: $[ eq(variables['Build.SourceBranch'], 'refs/heads/main') ]

Reserve template expressions for parameters

  1. Use ${{ }} for parameters.*, conditional insertion, and loops - all resolved before the run.
  2. Use $( ) to substitute a variable into a script or argument string.
  3. 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.

Frequently asked questions

What causes ""template expression not allowed""?
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.
How do I fix "template expression not allowed"?
When the value is only known during the run (output vars, conditions on prior steps), switch to $[ ].

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card