📦 Pipeline Artifacts
Learn how to publish and consume build outputs with pipeline artifacts.
Introduction to Artifacts
In Azure Pipelines, artifacts are files that are produced as a result of a build or release. These can include compiled code, executables, libraries, configuration files, or any other output generated during your pipeline's execution. Artifacts are crucial for passing outputs from one stage of a pipeline to another, or for making them available for download and use outside the pipeline.
Artifacts ensure that the outputs of your build are versioned, traceable, and easily accessible, promoting a reproducible build and deployment process.
Publishing Artifacts
You can publish artifacts using the PublishBuildArtifacts
task in classic pipelines or the PublishBuildArtifacts@1
task in YAML pipelines.
YAML Example
azure-pipelines.yml
pipeline:
jobs:
- job: Build
pool:
vmImage: 'ubuntu-latest'
steps:
- script: echo "Hello, Artifacts!" > my_output.txt
displayName: 'Create a sample file'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(System.DefaultWorkingDirectory)/my_output.txt'
ArtifactName: 'my_sample_artifact'
publishLocation: 'Container'
displayName: 'Publish Sample Artifact'
In this example:
- A sample file
my_output.txt
is created. - The
PublishBuildArtifacts@1
task publishes this file as an artifact namedmy_sample_artifact
. PathtoPublish
specifies the path to the file or directory to be published.ArtifactName
defines the name of the artifact.publishLocation
can be 'Container' (default) or 'Pipeline', controlling where it's stored.
Consuming Artifacts
Artifacts published in one job can be automatically downloaded to subsequent jobs in the same pipeline. If you need to explicitly download them or download artifacts from a different pipeline, you can use the DownloadBuildArtifacts
task (classic) or DownloadBuildArtifacts@0
task (YAML).
YAML Example (Consuming in a subsequent job)
azure-pipelines.yml
pipeline:
jobs:
- job: Build
pool:
vmImage: 'ubuntu-latest'
steps:
- script: echo "Hello, Artifacts!" > my_output.txt
displayName: 'Create a sample file'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(System.DefaultWorkingDirectory)/my_output.txt'
ArtifactName: 'my_sample_artifact'
displayName: 'Publish Sample Artifact'
- job: Consume
pool:
vmImage: 'ubuntu-latest'
dependsOn: Build
steps:
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'my_sample_artifact'
downloadPath: '$(System.ArtifactsDirectory)'
displayName: 'Download my_sample_artifact'
- script: cat $(System.ArtifactsDirectory)/my_sample_artifact/my_output.txt
displayName: 'Verify artifact content'
In the Consume
job:
dependsOn: Build
ensures this job runs after theBuild
job.- The
DownloadBuildArtifacts@0
task downloads the specified artifact. buildType: 'current'
downloads from the current pipeline run.artifactName
specifies which artifact to download.downloadPath
indicates where the artifact should be placed. By default, it's placed in$(System.ArtifactsDirectory)
.- The final script verifies that the content was downloaded correctly.
Types of Artifacts
Azure Pipelines supports various artifact types:
- Pipeline Artifacts: The primary mechanism for sharing files within or across pipelines. These are managed via the
PublishBuildArtifacts
andDownloadBuildArtifacts
tasks. - Universal Packages: A more flexible way to store and retrieve any type of file or set of files. Useful for storing dependencies, tools, or large datasets. You can use the
AzureArtifacts@0
task to publish and consume Universal Packages. - Container Registry Artifacts: Docker images and other container artifacts pushed to registries like Azure Container Registry or Docker Hub.
- Maven, npm, NuGet, Python packages: Integrated support for common package managers, allowing you to publish and consume packages directly from your pipeline.
Best Practices
- Be Specific: Only publish what is necessary for downstream consumption. Avoid publishing entire build directories unless required.
- Clear Naming: Use descriptive names for your artifacts (e.g.,
MyApp.Release.zip
,AppConfig.Json
). - Versioning: If needed, incorporate versioning into your artifact names or use separate artifact feeds for different versions.
- Security: Be mindful of what you publish, especially if artifacts are made public or shared widely.
- Artifact Feeds: For managing dependencies and reusable components across multiple projects, consider using Azure Artifacts feeds.