Skip to content

feat(ci): don't run build on unchanged containers #71

feat(ci): don't run build on unchanged containers

feat(ci): don't run build on unchanged containers #71

Workflow file for this run

name: Docker build
on:
push:
branches:
- '*'
pull_request:
types: [opened]
workflow_dispatch:
env:
REGISTRY: ghcr.io
jobs:
list_containers:
name: List containers to build
runs-on: ubuntu-latest
if: >-
(github.event_name == 'push' && github.event.before != '0000000000000000000000000000000000000000') ||
github.event_name != 'push'
outputs:
matrix: ${{ steps.set-matrix.outputs.MATRIX }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Generate containers list with parameters
id: set-matrix
working-directory: containers
run: |
echo "Event: ${{ github.event_name }}"
echo "Action: ${{ github.event.action }}"
echo "Base ref: ${{ github.event.base_ref }}"
echo "Before: ${{ github.event.before }}"
echo "After: ${{ github.event.after }}"
echo "Pull request base ref: ${{ github.event.pull_request.base.ref }}"
echo "Pull request base sha: ${{ github.event.pull_request.base.sha }}"
echo "Pull request head ref: ${{ github.event.pull_request.head.ref }}"
echo "Pull request head sha: ${{ github.event.pull_request.head.sha }}"
{
echo "MATRIX<<9743a66f914cc249efca164485a19c5c"
for d in * ; do
if [ -f "$d/Dockerfile" ] ; then
echo "- container: \"$d\""
# extract additional properties from Dockerfiles
sed -En 's/^#\s+((\w|-)+):\s+(.+)$/ "\1": "\3"/p' "$d/Dockerfile"
fi
done | yq -p yaml -o json -I 0 .
echo 9743a66f914cc249efca164485a19c5c
} >> "$GITHUB_OUTPUT"
build:
name: "Build container: ${{ matrix.container }}"
runs-on: ubuntu-latest
needs: list_containers
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(needs.list_containers.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Generate properties
id: props
run: |
PLATFORMS_DEFAULT="linux/amd64,linux/arm64"
PLATFORMS="${{ matrix.platforms }}"
{
echo "platforms=${PLATFORMS:-"${PLATFORMS_DEFAULT}"}"
} >> "$GITHUB_OUTPUT"
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.container }}
labels: |
org.opencontainers.image.title=${{ matrix.container }}
tags: |
type=schedule
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: containers/${{ matrix.container }}
push: true
platforms: ${{ steps.props.outputs.platforms }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max