This pipeline's only job is to build, compress, and publish your Docker image artifact. You create this pipeline once in the Azure DevOps UI and link it to this file.
Key Features:
trigger: paths:: This is critical. The pipeline only runs if theDockerfileitself changes.docker save | gzip -c > ...: We pipe the output ofdocker savedirectly intogzip. This is a key optimization. It creates a compressed.tar.gzfile, which is much faster to upload and download than an uncompressed.tar.
# image-builder-pipeline.yml
# Use a dynamic build number
name: $(Date:yyyyMMdd)$(Rev:.r)
# Trigger ONLY when the Dockerfile changes
trigger:
paths:
include:
- 'ci/docker/Dockerfile'
pool:
vmImage: 'aws-ubuntu-latest'
parameters:
- name: phpImageName
displayName: '[PROJECT-NAME] Docker Image'
type: string
default: 'php-build-[PROJECT-NAME]'
stages:
- stage: BuildImage
displayName: 'Build and Publish Docker Image'
jobs:
- job: Build
displayName: 'Build Docker Image'
steps:
- checkout: self
- script: |
docker build --pull -t ${{ parameters.phpImageName }} -f ci/docker/Dockerfile .
displayName: 'Build PHP Docker image'
# === OPTIMIZATION ===
# Compress the image with gzip on-the-fly
- script: |
mkdir -p $(Build.ArtifactStagingDirectory)
docker save ${{ parameters.phpImageName }} | gzip -c > $(Build.ArtifactStagingDirectory)/${{ parameters.phpImageName }}.tar.gz
displayName: 'Export and gzip docker image'
# Publish the gzipped artifact
- task: PublishBuildArtifacts@1
displayName: 'Publish Docker Image Artifact'
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: '${{ parameters.phpImageName }}'