Skip to content

Docker publish

Docker publish #87

name: Docker build
on:
push:
branches:
- 'master'
pull_request:
types: [opened, reopened, synchronize]
workflow_dispatch:
env:
REGISTRY: ghcr.io
jobs:
list_containers:
name: List containers to build
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.MATRIX }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: Generate containers list with parameters
id: set-matrix
working-directory: containers
run: |
GIT_DIFF_ENABLED=0
if [ '${{ github.event_name }}' = 'push' ] ; then
GIT_DIFF_ENABLED=1
GIT_DIFF_BASE='${{ github.event.before }}'
GIT_DIFF_HEAD='${{ github.event.after }}'
elif [ '${{ github.event_name }}' = 'pull_request' ] ; then
GIT_DIFF_ENABLED=1
GIT_DIFF_BASE='${{ github.event.pull_request.base.sha }}'
GIT_DIFF_HEAD='${{ github.event.pull_request.head.sha }}'
fi
{
echo "MATRIX<<9743a66f914cc249efca164485a19c5c"
{
NO_CONTAINERS=1
for d in * ; do
# check if directory changed and skip if not
[ $GIT_DIFF_ENABLED = 1 ] && \
git diff -s --exit-code "${GIT_DIFF_BASE}..${GIT_DIFF_HEAD}" "$d" && \
continue
if [ -f "$d/Dockerfile" ] ; then
echo "- container: \"$d\""
# extract additional properties from Dockerfiles
sed -En 's/^#\s+((\w|-)+):\s+(.+)$/ "\1": "\3"/p' "$d/Dockerfile"
NO_CONTAINERS=0
fi
done
[ $NO_CONTAINERS = 1 ] && echo '[]'
} | 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
if: needs.list_containers.outputs.matrix != '[]'
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=pr
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