Lesson 4 of 4 Package Management

Versioning Pipeline Artifacts

This lesson explains how to identify build output exactly, and promote it unchanged.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish packages from pipeline artifacts.
  • Explain the build once, promote everywhere rule.
  • Set run numbers in Azure Pipelines.
  • Choose a safe identifier for container images.

Packages and pipeline artifacts

The two terms are easily confused. A package is published to a feed so that other projects can consume it as a dependency. A pipeline artifact is a file or set of files produced by a run and kept with that run, usually so that a later job or stage can deploy it. In Azure Pipelines, the publish shortcut or PublishPipelineArtifact@1 stores one, and download or DownloadPipelineArtifact@2 retrieves it. In GitHub Actions, the equivalents are actions/upload-artifact and actions/download-artifact.

Sort into groups

Package or pipeline artifact?

Exam skills 3.1.4

Tell packages and pipeline artifacts apart.

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

Build once, promote everywhere

The central rule is to build once and promote the same artifact. If a pipeline rebuilds the application for each environment, the code deployed to production was never the code tested, because compilers, dependencies and timing can differ. The artifact should be environment-neutral, with configuration supplied at deployment time.

This has a direct consequence for versioning. The identifier must be fixed at build time, must be unique, and must lead back to the source. An artifact version usually combines a human-friendly release number with a unique part.

IdentifierExamplePurpose
Release version2.5.0Meaningful to people
Build counter2.5.0.417Unique and ordered
Commit SHA2.5.0+7c9e2f1Exact link to the source
Date20261102.3Readable sequence
Match the pairs

Parts of an artifact identifier

Exam skills 3.1.4

Match each part of an artifact version with its purpose.

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

Numbering runs in Azure Pipelines

The run number comes from the top-level name property, and it appears as Build.BuildNumber. Tokens fill in values, and $(Rev:r) adds a counter that resets when the rest of the name changes. The counter() expression gives more control, and it keeps a value per prefix.

name: 2.5.$(Rev:r)

variables:
  major: 2
  minor: 5
  # A new counter sequence starts whenever major.minor changes
  patch: $[ counter(format('{0}.{1}', variables['major'], variables['minor']), 0) ]
  version: $(major).$(minor).$(patch)

steps:
  - script: dotnet publish --configuration Release -p:Version=$(version) --output $(Build.ArtifactStagingDirectory)
  - publish: $(Build.ArtifactStagingDirectory)
    artifact: webapp-$(version)

In GitHub Actions, github.run_number gives a counter that is unique to a workflow, and github.sha gives the commit. A step can write a calculated value to $GITHUB_OUTPUT for later jobs to read.

Fill in the number

Counters and run numbers

Exam skills 3.1.4

Work out the values the counter expression produces.

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

Versioning container images

A container image tag is a label that can be moved, so latest tells you nothing about which code is running, and rollback becomes unreliable. Pipelines should tag each image with something unique, usually a commit SHA or version number.

docker build -t contoso.azurecr.io/web:2.5.417 -t contoso.azurecr.io/web:7c9e2f1 .
docker push --all-tags contoso.azurecr.io/web

A tag can still be overwritten. A digest, such as web@sha256:..., is a hash of the content, so it identifies one image exactly and cannot change. Deploying by digest gives the strongest guarantee. Azure Container Registry can also lock a tag to stop it from being overwritten or deleted.

The following diagram shows one image promoted through the environments.

Build once, tag uniquely, promote the same image. The pipeline builds one image, tags it with the commit SHA, and deploys that image to every environment, preferably by digest. The tag answers what is running and allows a rollback to an exact version; per-environment rebuilds and moving tags such as latest do neither.
The commit SHA is illustrative.
True or false

Tags, digests and locks

Exam skills 3.1.4 · 3.4.7

Check what each image reference guarantees.

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

Identifying what is deployed

A team's pipeline rebuilds a container image for each environment and tags all of them latest. An incident occurs, and nobody can say which code is in production. Which changes should you make?

The pipeline should build the image once, tag it with a unique value such as the commit SHA, and deploy that same image to every environment, preferably by digest. The unique tag answers what is running, and it allows a rollback to an exact previous version. Rebuilding per environment also has to stop, since test results then say nothing about the production image. A separate latest-prod tag would keep the same weakness, because it is still a tag that moves.

Check yourself

Why deploy by digest rather than by tag?

Show answer

A tag can be moved, whereas a digest is a hash of the content, so it identifies exactly one image.

Exam Tips

Remember for the exam

  • A package goes to a feed; a pipeline artifact stays with a run.
  • Build once and promote the same artifact through environments.
  • $(Rev:r) and counter() add unique numbers in Azure Pipelines.
  • Avoid latest in deployments; use a SHA, version or digest.
  • A digest identifies image content exactly and cannot be moved.