Lesson 2 of 3 Pipeline Orchestration and Reuse

Creating Reusable Pipeline Elements

This lesson explains templates, task groups, variables and variable groups, and the GitHub equivalents.

Learning outcomes

After completing this lesson, you will be able to:

  • Write parameterized YAML templates.
  • Distinguish includes from extends.
  • Use variables and variable groups.
  • Choose between composite actions and reusable workflows.

YAML templates in Azure Pipelines

A template is a YAML file that can be inserted into a pipeline. Templates can define steps, jobs, stages or variables, and they accept parameters, which are typed and can have default values. They are expanded at compile time, so the final pipeline contains the inserted YAML.

# templates/build-steps.yml
parameters:
  - name: configuration
    type: string
    default: Release
  - name: runTests
    type: boolean
    default: true

steps:
  - script: dotnet build --configuration ${{ parameters.configuration }}
  - ${{ if eq(parameters.runTests, true) }}:
    - script: dotnet test --configuration ${{ parameters.configuration }}

A pipeline includes the template with the template keyword. A template may live in another repository, referenced as a repository resource, and it can be pinned to a tag, which lets a platform team share and version it.

resources:
  repositories:
    - repository: shared
      type: git
      name: Platform/pipeline-templates
      ref: refs/tags/v2.1.0

steps:
  - template: templates/build-steps.yml@shared
    parameters:
      configuration: Debug

Includes and extends

There are two ways of using a template. An include inserts content into a pipeline, as above, and the rest of the pipeline stays under its author's control. An extends template does the opposite: the template defines the outer structure, and the pipeline supplies only what the template allows, through parameters. Because the template controls everything else, it can guarantee that a security scan always runs.

# azure-pipelines.yml
extends:
  template: secure-pipeline.yml@shared
  parameters:
    buildSteps:
      - script: npm ci
      - script: npm run build

With the Required template check on an environment or service connection, a pipeline that does not extend an approved template cannot use that resource. This combination is the standard answer for enforcing organization-wide steps in Azure Pipelines.

The following diagram shows the template and the check that enforces it.

Making a credential scan mandatory. An extends template holds the scan and the deployment structure; a Required template check on the production environment or service connection lets a run through only if its pipeline extends that template. A pipeline that does not fails the check, whatever its YAML says.
Two teams are drawn.
Scenario questions

Includes and extends

Exam skills 3.3.8

Choose the template approach for each reuse need.

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

Task groups

A task group packages a sequence of tasks for reuse in classic build and release pipelines. Task groups are versioned, and they appear in the task list like any other task. They do not work in YAML pipelines, where a step template plays the same part. When migrating, each task group becomes a step template.

Variables and variable groups

Variables hold values that change between environments or runs. They can be defined at pipeline, stage or job level, and the narrowest scope wins. Variables may also be defined in a template, or set in the pipeline settings. A setting's settable at queue time option allows overriding at the start of a run, though runtime parameters are generally preferred, as they are typed.

Variable groups are stored in the Library, and they share values among pipelines. A group can be linked to Azure Key Vault, in which case the values come from the vault at run time. A group is a protected resource, so pipeline permissions, approvals and checks can apply to it.

variables:
  - group: web-common
  - name: appName
    value: contoso-web
  - template: vars/production.yml

When a variables block references a group, it must use the list form with name and value, and not the shorter key and value form. Secret variables are not passed to scripts automatically, and each secret must be mapped through env.

Spot the bug

Variables that do not arrive

Exam skills 3.3.8

Find the lines that stop this pipeline's variables from working.

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

Reuse in GitHub Actions

GitHub offers two main mechanisms, and they work at different levels.

Reusable workflows

A reusable workflow is a workflow that declares on: workflow_call, and another workflow calls it as a whole job, using uses at job level. Inputs and secrets are passed explicitly, or secrets can be inherited with secrets: inherit.

# .github/workflows/deploy.yml in contoso/platform
on:
  workflow_call:
    inputs:
      environment:
        type: string
        required: true
    secrets:
      token:
        required: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: ${{ inputs.environment }}
    steps:
      - run: echo Deploying to ${{ inputs.environment }}

# Calling workflow
jobs:
  production:
    uses: contoso/platform/.github/workflows/deploy.yml@v2
    with:
      environment: production
    secrets: inherit

Workflows can be nested up to ten levels deep, and one workflow file may call up to 50 unique reusable workflows, counting the nested ones. In a nested call, GITHUB_TOKEN permissions can only stay the same or become more restrictive.

Composite actions

A composite action packages several steps into one action, defined in an action.yml file with using: composite, and used as a single step inside a job. It runs in the caller's job, and on the caller's runner.

# .github/actions/setup-app/action.yml
name: Set up the application
inputs:
  node-version:
    default: '22'
runs:
  using: composite
  steps:
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ inputs.node-version }}
    - run: npm ci
      shell: bash

Note that each run step in a composite action must state its shell. The choice is simple. To reuse a few steps inside jobs, use a composite action. To reuse a whole job or several jobs, including their runner and environment, use a reusable workflow.

Composite action
Called fromA step
ContainsSteps
RunnerThe caller's
Uses secrets directlyNo; passed as inputs
EnvironmentsNo
Choose when You want to share a sequence of steps.
Reusable workflow
Called fromA job
ContainsJobs
RunnerIts own runs-on
Uses secrets directlyYes, when passed or inherited
EnvironmentsYes
Choose when You want to standardize a whole job, such as a deployment.

Shared values use configuration variables, at organization, repository or environment level, reached through the vars context. Secrets have the same three levels. Organization-level values can be limited to selected repositories. Workflow templates, formerly called starter workflows, give teams a starting point when they create a new workflow, but they are copied once, so they do not enforce later changes. An organization ruleset can require a workflow to pass before merging.

Sort into groups

Composite action or reusable workflow?

Exam skills 3.3.8

Decide which GitHub reuse mechanism each statement describes.

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

Enforcing a scan

A security team requires that every Azure Pipelines pipeline deploying to production runs a credential scan, and that teams cannot remove the step. Each team owns its own YAML file. What should you implement?

Create an extends template containing the scan and deployment structure, and add a Required template check to the production environment or service connection. Pipelines that do not extend the template fail the check. An include template could be removed by a pipeline author. A task group does not apply to YAML. A review of each pipeline would rely on people and would not prevent a later change.

Check yourself

Why must each run step in a composite action state a shell?

Show answer

A composite action does not inherit the job's default shell, so GitHub requires it to be set on each step.

Exam Tips

Remember for the exam

  • Includes insert YAML; extends lets a template control the structure.
  • Extends plus the Required template check enforces mandatory steps.
  • Task groups are for classic pipelines; step templates replace them.
  • Referencing a variable group needs the list form of variables.
  • A composite action replaces steps; a reusable workflow replaces jobs.