Lesson 2 of 4 Pipeline Fundamentals

Developing YAML Pipelines

This lesson walks through complete pipelines, then explains variables, expressions and conditions.

Learning outcomes

After completing this lesson, you will be able to:

  • Read a multi-stage YAML pipeline.
  • Write the equivalent workflow in GitHub Actions.
  • Distinguish template, macro and runtime syntax.
  • Pass values between steps and jobs.

A multi-stage Azure Pipelines file

The following pipeline builds a Node.js application, publishes an artifact, and deploys to a test environment. Read it from the top, and note how the hierarchy of stages, jobs and steps appears as indentation.

trigger:
  branches:
    include:
      - main

pool:
  vmImage: ubuntu-latest

variables:
  nodeVersion: '22.x'

stages:
  - stage: Build
    jobs:
      - job: BuildApp
        steps:
          - task: UseNode@1
            inputs:
              version: $(nodeVersion)
          - script: |
              npm ci
              npm test
              npm run build
            displayName: Build and test
          - publish: dist
            artifact: web

  - stage: DeployTest
    dependsOn: Build
    jobs:
      - deployment: DeployWeb
        environment: test
        strategy:
          runOnce:
            deploy:
              steps:
                - download: current
                  artifact: web
                - task: AzureWebApp@1
                  inputs:
                    azureSubscription: sc-azure-test
                    appName: contoso-web-test
                    package: $(Pipeline.Workspace)/web

Several details deserve attention. pool at the top applies to all jobs, and it may be overridden lower down. publish and download are shortcuts for artifact tasks. A deployment job differs from a normal job: it records history against an environment, applies any checks, and has a strategy. In a deployment job, artifacts are downloaded automatically in the deploy hook, and the explicit step above makes the download clear. azureSubscription names a service connection.

True or false

Reading a multi-stage pipeline

Exam skills 3.3.5

Check what the parts of a multi-stage Azure Pipelines file do.

Log in to try this activity Interactive activities are free with an account.

The equivalent GitHub Actions workflow

The same process in GitHub Actions has a similar shape. Note the dependency through needs, and the explicit checkout step, which is implicit in Azure Pipelines.

name: build-and-deploy
on:
  push:
    branches: [main]

permissions:
  contents: read
  id-token: write

env:
  NODE_VERSION: '22'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
      - run: |
          npm ci
          npm test
          npm run build
      - uses: actions/upload-artifact@v4
        with:
          name: web
          path: dist

  deploy-test:
    needs: build
    runs-on: ubuntu-latest
    environment: test
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: web
          path: dist
      - uses: azure/login@v2
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      - uses: azure/webapps-deploy@v3
        with:
          app-name: contoso-web-test
          package: dist
Spot the bug

A workflow that cannot deploy

Exam skills 3.3.5

Find the lines that stop this GitHub Actions workflow from deploying.

Log in to try this activity Interactive activities are free with an account.

Variables and expressions

Azure Pipelines has three forms of variable syntax, and they are processed at different times. This is a favourite exam topic, as a pipeline can fail in a confusing way if the wrong form is used.

SyntaxNameWhen processedTypical use
${{ variables.name }}Template expressionCompile time, before the runParameters, and conditional insertion of YAML
$(name)MacroRun time, before a task runsTask inputs and scripts
$[variables.name]Runtime expressionRun timeConditions and variable values

A template expression can only see values known when the pipeline is compiled, such as parameters and statically defined variables, and it can change the structure of the pipeline by including or excluding YAML. A variable set by a script during the run is not available to it. A macro is replaced just before a task runs, and if it has no value, the text is left unchanged, so a script may see the literal $(name).

Conditions

A condition decides whether a stage, job or step runs. The default is succeeded(). Other functions include failed(), always(), canceled() and succeededOrFailed(). The following example runs a job only on main, and an inserted section appears only for a production parameter.

parameters:
  - name: environment
    type: string
    default: test
    values: [test, production]

jobs:
  - job: Notify
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
    steps:
      - script: echo Deploying to ${{ parameters.environment }}
      - ${{ if eq(parameters.environment, 'production') }}:
        - script: ./run-smoke-tests.sh

Passing values between steps

A step can set a variable for later steps with a logging command. To share it with another job, the step needs a name, the variable needs isOutput=true, and the consumer must depend on the job.

jobs:
  - job: Build
    steps:
      - bash: echo "##vso[task.setvariable variable=imageTag;isOutput=true]$(Build.SourceVersion)"
        name: setTag

  - job: Deploy
    dependsOn: Build
    variables:
      tag: $[ dependencies.Build.outputs['setTag.imageTag'] ]
    steps:
      - script: echo $(tag)

GitHub Actions has a single expression syntax, ${{ }}, with contexts such as github, env, vars, secrets and needs. A step passes a value by writing to the $GITHUB_OUTPUT file, and a job exposes it through outputs. The conditions are written with if:, using functions such as success(), failure(), always() and cancelled().

The following diagram shows when each kind of expression in a YAML pipeline is evaluated.

Compile time against runtime in a YAML pipeline. ${{ }} template expressions are evaluated when the pipeline is compiled, before any script runs, so a variable set by a script does not exist yet and the inserted job never appears. A runtime condition is evaluated during the run, after the script has set the value.
The scenario is illustrative.
Spot the bug

An output variable that never arrives

Exam skills 3.3.5

Find the lines that stop a value passing from one job to another.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reading a failing condition

A script step sets a variable named deployTarget during a run. A later part of the pipeline uses ${{ if eq(variables.deployTarget, 'prod') }}: to insert a job, but the job never appears. Why?

The ${{ }} expression is evaluated at compile time, before any script has run, so the variable does not exist yet. The job should instead use a runtime condition, such as eq(variables['deployTarget'], 'prod'), with the value passed as an output variable if it crosses jobs. Quoting the value differently would not change when the expression is evaluated.

Check yourself

A macro $(name) has no value. What does a script see?

Show answer

The literal text $(name). Macros with no value are left unchanged.

Exam Tips

Remember for the exam

  • ${{ }} runs at compile time; $(var) and $[ ] run at run time.
  • Variables set by scripts are not visible to template expressions.
  • A deployment job records history against an environment and applies checks.
  • Output variables need a named step, isOutput=true and a dependency.
  • In GitHub Actions, write to $GITHUB_OUTPUT and expose values with job outputs.