Files
computer/.github/workflows/docker.yml
T
2026-06-06 12:18:07 +01:00

232 lines
6.8 KiB
YAML

name: Docker (GHCR)
on:
push:
branches: [main, release]
workflow_dispatch:
concurrency:
group: docker-${{ github.ref }}
cancel-in-progress: true
env:
IMAGE: ghcr.io/${{ github.repository }}
permissions:
contents: read
packages: write
jobs:
detect-version-change:
if: github.ref == 'refs/heads/release'
runs-on: ubuntu-latest
outputs:
version_changed: ${{ steps.check.outputs.changed }}
version: ${{ steps.check.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check for version bump
id: check
run: |
CURRENT=$(python3 -c "
import re, pathlib
text = pathlib.Path('pyproject.toml').read_text()
m = re.search(r'^version\s*=\s*\"([^\"]+)\"', text, re.M)
print(m.group(1))
")
git show HEAD~1:pyproject.toml > /tmp/prev_pyproject.toml 2>/dev/null || true
if [ -f /tmp/prev_pyproject.toml ]; then
PREVIOUS=$(python3 -c "
import re, pathlib
text = pathlib.Path('/tmp/prev_pyproject.toml').read_text()
m = re.search(r'^version\s*=\s*\"([^\"]+)\"', text, re.M)
print(m.group(1) if m else '')
")
else
PREVIOUS=""
fi
echo "current=$CURRENT"
echo "previous=$PREVIOUS"
if [ "$CURRENT" != "$PREVIOUS" ] && [ -n "$CURRENT" ]; then
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "version=$CURRENT" >> "$GITHUB_OUTPUT"
echo "Version changed: $PREVIOUS -> $CURRENT"
else
echo "changed=false" >> "$GITHUB_OUTPUT"
echo "No version change detected"
fi
build-dev:
if: github.ref == 'refs/heads/main'
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push ${{ matrix.platform }} dev image by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true
cache-from: type=gha,scope=dev-${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=dev-${{ matrix.arch }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-dev-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge-dev:
if: github.ref == 'refs/heads/main'
needs: build-dev
runs-on: ubuntu-latest
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-dev-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create dev multi-arch manifest
working-directory: /tmp/digests
run: |
SHORT_SHA="${GITHUB_SHA::7}"
docker buildx imagetools create \
-t "${{ env.IMAGE }}:dev" \
-t "${{ env.IMAGE }}:dev-${SHORT_SHA}" \
$(printf '${{ env.IMAGE }}@sha256:%s ' *)
build-release:
needs: detect-version-change
if: github.ref == 'refs/heads/release' && needs.detect-version-change.outputs.version_changed == 'true'
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push ${{ matrix.platform }} release image by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true
cache-from: type=gha,scope=release-${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=release-${{ matrix.arch }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-release-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge-release:
needs: [detect-version-change, build-release]
if: github.ref == 'refs/heads/release' && needs.detect-version-change.outputs.version_changed == 'true'
runs-on: ubuntu-latest
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-release-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create release multi-arch manifest
working-directory: /tmp/digests
run: |
VERSION="${{ needs.detect-version-change.outputs.version }}"
MAJOR_MINOR="${VERSION%.*}"
docker buildx imagetools create \
-t "${{ env.IMAGE }}:latest" \
-t "${{ env.IMAGE }}:${VERSION}" \
-t "${{ env.IMAGE }}:${MAJOR_MINOR}" \
$(printf '${{ env.IMAGE }}@sha256:%s ' *)