Lesson 4 of 5 Maintaining Pipelines

Designing a Retention Strategy for Artifacts and Dependencies

This lesson explains how to keep what matters and remove the rest.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the needs that drive retention.
  • Describe retention settings and leases in Azure Pipelines.
  • Set retention in GitHub Actions.
  • Apply retention to packages and images.

What drives retention

Retention balances three needs. Audit and compliance may require records of what was deployed, and when. Operations needs recent artifacts for rollback and debugging. Cost pushes the other way, because storage is billed, and old data has little value. A strategy treats each kind of data according to its purpose, instead of applying one long period to all of it.

Retention in Azure Pipelines

Retention is set under Project settings > Pipelines > Settings. The settings apply to the whole project, as Azure DevOps no longer supports retention rules per pipeline. They cover the days to keep artifacts, symbols and attachments, the days to keep runs, the days to keep pull request runs, and the number of recent runs to keep for each pipeline. In most projects the defaults are 30 days for runs and artifacts, 10 days for pull request runs, and three recent runs. Policies are processed once a day.

Retention leases

A retention lease keeps a specific run beyond those limits, which suits something like a production release. A run can be retained manually from its menu, and deployments to environments can be retained. A pipeline can also create a lease through the REST API, for a set duration.

- script: |
    curl -s -X POST \
      -H "Authorization: Bearer $SYSTEM_ACCESSTOKEN" \
      -H "Content-Type: application/json" \
      -d "[{\"daysValid\":365,\"definitionId\":$(System.DefinitionId),\"ownerId\":\"User:$(Build.RequestedForId)\",\"protectPipeline\":false,\"runId\":$(Build.BuildId)}]" \
      "$(System.CollectionUri)$(System.TeamProject)/_apis/build/retention/leases?api-version=7.1"
  displayName: Keep this production release for a year
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

This approach keeps long retention for the small number of runs that need it, while the project's general settings stay short. Raising retention for everything would be far more expensive.

The following diagram shows how long each kind of pipeline run is kept for a regulated company.

Retention for a regulated company. The project setting keeps all runs for 30 days; production deployments either create a retention lease or copy their artifacts to Azure Blob Storage with an immutability policy, keeping them for seven years. Raising the project setting would keep everything.
The scenario is illustrative.
True or false

Retention settings and leases

Exam skills 3.6.4

Check how Azure Pipelines retains runs and artifacts.

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

Retention in GitHub Actions

GitHub keeps workflow artifacts and logs for 90 days by default on most plans. The period can be set at repository, organization or enterprise level, within a maximum. A single artifact can have a shorter period with retention-days.

- uses: actions/upload-artifact@v4
  with:
    name: test-report
    path: reports/
    retention-days: 7

Artifacts count towards a plan's storage allowance, which is shared with GitHub Packages, so deleting them promptly reduces cost. Important outputs that must last, such as a release binary, should be attached to a GitHub release, or published to a package registry. Caches are evicted automatically when unused.

Scenario questions

Retention in GitHub

Exam skills 3.6.4

Choose how to keep or remove GitHub workflow outputs.

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

Retention for packages and dependencies

Azure Artifacts feeds have their own retention policies, which limit the number of versions kept for each package, and delete old versions automatically. Versions promoted to a view are protected, as are those downloaded recently, so a policy does not remove a version still in use. This protects the 2 GiB free allowance.

Azure Container Registry has a retention policy for untagged manifests, in the Premium tier, and az acr purge can run as a scheduled task to delete old images by age and name pattern. GitHub Packages keeps versions until they are deleted, and a workflow can remove old versions.

The following table shows a typical strategy.

ItemSuggested retentionMechanism
Pull request run logsDaysProject setting, or retention-days
Main branch artifactsWeeksProject setting
Production release artifactsAs long as support or audit requiresRetention lease, or GitHub release
Package versionsA set number per packageFeed retention policy
Container imagesCurrent and recent versionsaz acr purge, or a retention policy
Audit dataAs policy requiresExport to Azure Monitor Logs
Match the pairs

Retention for each kind of output

Exam skills 3.6.4

Match each item with the mechanism that controls its retention.

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

Keeping production builds

A regulated company must keep artifacts from each production deployment for seven years, but it wants all other runs removed after 30 days. Azure Pipelines project retention is 30 days. What should you do?

Keep the 30-day project setting, and create a retention lease as part of the production deployment, or copy artifacts to a long-term store such as Azure Blob Storage with an immutability policy. Raising the project setting to seven years would retain everything and raise cost. Per-pipeline retention rules are no longer supported, so they are not an option.

Check yourself

Can one pipeline have longer retention through its own rule?

Show answer

No. Per-pipeline rules are no longer supported, so use a retention lease for specific runs.

Exam Tips

Remember for the exam

  • Retention settings in Azure Pipelines apply at project level.
  • A retention lease keeps a specific run for longer.
  • GitHub artifacts default to 90 days; retention-days shortens one artifact.
  • Feed retention policies protect versions in views.
  • Use az acr purge for scheduled clean-up of images.