Lesson 3 of 3 Pipeline Orchestration and Reuse

Implementing Checks and Approvals with YAML Environments

This lesson explains environments, the available checks and the order in which they run.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why checks live on resources.
  • Choose a check for a stated need.
  • State the check evaluation order.
  • Configure environments in GitHub.

What an environment provides

An environment in Azure Pipelines represents a target, such as test or production. It is created under Pipelines > Environments, or automatically when a deployment job first names it. An environment gives three things: a deployment history, which shows commits and work items for each deployment; a set of checks, which control access; and optional resources, such as Kubernetes namespaces and virtual machines.

The key design point is that checks belong to the resource, and not to the YAML. Anyone who can edit a pipeline file could remove a step or a stage, but they cannot remove a check, because it is set by the owner of the environment. The separation of duties is built in. Checks can also be applied to other protected resources: service connections, agent pools, variable groups, secure files and repositories.

True or false

What environments give you

Exam skills 3.3.9

Check what an Azure Pipelines environment provides.

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

The available checks

Each check serves a different purpose, and the exam frequently describes a need and asks which check meets it. The following explanations cover the full list.

Approvals

Approvals pause a stage until named users or groups agree. They can require all or a set number of approvers, can prevent the person who started the run from approving, and they have a timeout.

Branch control

Branch control allows only runs from named branches, such as refs/heads/main, and can require the branch to be protected. It stops a feature branch from deploying to production.

Business hours

Business hours allows deployments only within set days and times.

Evaluate artifact

Evaluate artifact tests a container image against a policy, for example to confirm that it came from an approved registry.

Exclusive lock

Exclusive lock allows only one run at a time to use a resource. The lockBehavior setting decides what happens to queued runs: runLatest, the default, cancels older pending runs and proceeds with the latest, whereas sequential runs them in order.

Invoke Azure Function and Invoke REST API

Invoke Azure Function and Invoke REST API call external logic, and they pass or fail according to the response. They can integrate with a change management system, or any custom rule.

Query Azure Monitor Alerts

Query Azure Monitor Alerts passes only if no matching alerts are active.

Required template

Required template allows only pipelines that extend a named template.

The ServiceNow Change Management check is also available through a Marketplace extension. The following table recaps which check answers which need.

NeedCheck
A person must agreeApprovals
Only main can deployBranch control
Only during a change windowBusiness hours
Only one deployment at a timeExclusive lock
Stop when the system is unhealthyQuery Azure Monitor Alerts
Ask an external systemInvoke REST API or Invoke Azure Function
Only compliant pipelinesRequired template
Only compliant imagesEvaluate artifact
Scenario questions

Choosing the right check

Exam skills 3.3.9

Pick the environment check that meets each requirement.

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

Check evaluation order

When a stage is about to start, Azure Pipelines gathers the checks for all protected resources that the stage uses, and evaluates them in five categories, in this order. Within a category, checks run in the order they were created.

  1. Static checks

    Branch control, Required template and Evaluate artifact.

  2. Pre-check approvals

    Approvals configured to run before dynamic checks.

  3. Dynamic checks

    Approvals, Invoke Azure Function, Invoke REST API, Business hours and Query Azure Monitor Alerts.

  4. Post-check approvals

    Approvals configured to run after dynamic checks.

  5. Exclusive lock

    Acquired last, so that a run does not hold the lock while it waits for approval.

Each check has a timeout. If it does not pass within that time, the stage is skipped, and the pipeline does not proceed to it. A check on a stage is evaluated before any of its jobs start, so a stage awaiting approval does not consume an agent or a parallel job.

The following diagram shows the checks on a production environment that are resolved before the stage's jobs start.

Three checks on the production environment: Branch control limited to main, Approvals from the release group with a minimum of two approvers, and Exclusive lock so deployments do not overlap. Checks are resolved before the stage's jobs start, so no agent is occupied while approvers decide.
The scenario is illustrative.
Put in order

How checks are evaluated

Exam skills 3.3.9

Arrange the categories in the order Azure Pipelines evaluates them.

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

Environments in YAML

A deployment job names the environment, which is what brings the checks into force. An ordinary job does not create deployment history.

stages:
  - stage: Production
    lockBehavior: sequential
    jobs:
      - deployment: Web
        environment: production
        strategy:
          runOnce:
            deploy:
              steps:
                - script: ./deploy.sh

The environment name can include a resource, such as environment: production.web-vm, to target a specific resource within it.

Environments in GitHub

GitHub environments are created in repository settings. They offer required reviewers, up to six people or teams, of whom one must approve, a wait timer of up to 30 days, and deployment branches and tags, which restrict where a deployment can come from. Environment secrets and variables become available only to jobs that reference that environment, and after protection rules pass. Custom deployment protection rules call a GitHub App, which can apply logic similar to the Azure checks.

A job references an environment with environment: production, or with a name and URL. Availability on private repositories depends on the plan.

True or false

GitHub environment protection

Exam skills 3.3.9

Check the protection rules that GitHub environments offer.

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

Combining checks

Production deployments in Azure Pipelines must come only from main, must not overlap, and must be approved by two members of a release group. During approval, no agent should be occupied. What should you configure?

On the production environment, add Branch control set to main, Approvals with the release group and a minimum of two approvers, and Exclusive lock. Checks are resolved before jobs start, so waiting for approval does not occupy an agent. Conditions in YAML could enforce the branch only until someone edits them, and a manual validation step inside a job would occupy a job while waiting, if it were in an agent job rather than an agentless one.

Check yourself

Why is Exclusive lock evaluated last?

Show answer

A run should not hold the lock while waiting for approvals or other checks, as that would block other runs.

Exam Tips

Remember for the exam

  • Checks live on the resource, so pipeline authors cannot remove them.
  • Order: static checks, pre-check approvals, dynamic checks, post-check approvals, Exclusive lock.
  • A stage waiting on checks does not use an agent.
  • lockBehavior is runLatest by default, or sequential.
  • GitHub environments offer required reviewers, wait timers and branch restrictions.