Skip to content

Build Containers

Build Containers #157

name: Build Containers
on:
pull_request:
paths:
- devops/docker/**/Dockerfile
push:
branches:
- main
paths:
- devops/docker/**/Dockerfile
workflow_dispatch:
inputs:
distro_list:
description: 'Comma separated list of distros to build. eg. ubuntu-20.04-amd64,ubuntu-20.04-arm64'
type: string
rebuild_all:
description: 'Rebuild all containers'
default: false
type: boolean
env:
REGISTRY: ghcr.io
BUILD_TYPE: Release
MOUNT: /azure-osconfig
jobs:
modified-containers:
name: Modified containers
runs-on: ubuntu-latest
outputs:
containers: ${{ steps.matrix.outputs.containers }}
steps:
- uses: actions/checkout@v4
- name: Get changed containers
id: changed-containers
uses: tj-actions/changed-files@v41.0.0
with:
files: ./devops/docker/**/Dockerfile
- name: Create build matrix
id: matrix
run: |
# If this workflow is present in other_changed_files, then rebuild all containers
if [[ -n "${str// }" ]]; then
echo "Building specified containers..."
containers=$(echo "${{ inputs.distro_list }}" | jq -R 'split(",") | map("devops/docker/" + . + "/Dockerfile")')
elif [[ "${{ steps.changed-containers.outputs.other_changed_files }}" == *".github/workflows/build-containers.yml"* ]] || [[ "${{ inputs.rebuild_all }}" == "true" ]]; then
echo "Workflow file changed, adding all containers to build matrix..."
containers=$(ls -d devops/docker/**/Dockerfile | jq -R . | jq -s .)
else
containers=$(echo -n ${{ steps.changed-containers.outputs.all_changed_files }} | jq -R -s -c 'split(" ")')
fi
echo Containers to build: $containers
echo containers=$containers >> $GITHUB_OUTPUT
docker-build:
name: Docker
needs: modified-containers
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
container: ${{ fromJson(needs.modified-containers.outputs.containers) }}
steps:
- uses: actions/checkout@v4
- name: Setup image
id: image
run: |
dockerfile=${{ matrix.container }}
image=${dockerfile#devops/docker/}
image=${image%/*}
distro=${image%-*}
repo=$(echo ${{ github.repository }} | awk '{print tolower($0)}')
echo name=$(echo -n $repo/$image) >> $GITHUB_OUTPUT
echo path=$(dirname ${dockerfile}) >> $GITHUB_OUTPUT
echo distro=$(echo -n $distro) >> $GITHUB_OUTPUT
- name: Setup QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Docker login (ghcr.io)
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }}
tags: |
type=raw,value=latest
type=raw,value=${{ github.run_number }}
type=sha
- name: Build and push
uses: docker/build-push-action@v3
with:
context: ${{ steps.image.outputs.path }}
# Only push the image when a pull request is merged to main
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}