Files
oikb/.github/workflows/docker.yml
T
2026-10-21 12:54:24 +09:00

320 lines
10 KiB
YAML

name: Docker (GHCR)
on:
push:
branches: [main]
workflow_dispatch:
concurrency:
group: docker-${{ github.ref }}
cancel-in-progress: false
env:
IMAGE: ghcr.io/${{ github.repository }}
permissions:
contents: read
packages: write
jobs:
read-version:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
outputs:
version: ${{ steps.pkg.outputs.version }}
is_new_version: ${{ steps.version.outputs.is_new_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Read version
id: pkg
run: |
VERSION=$(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))
")
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Check release version
id: version
env:
VERSION: ${{ steps.pkg.outputs.version }}
run: |
if git rev-parse "refs/tags/v${VERSION}" >/dev/null 2>&1; then
TAG_SHA=$(git rev-list -n 1 "v${VERSION}")
if [ "$TAG_SHA" = "$GITHUB_SHA" ]; then
echo "is_new_version=true" >> "$GITHUB_OUTPUT"
else
echo "is_new_version=false" >> "$GITHUB_OUTPUT"
fi
else
echo "is_new_version=true" >> "$GITHUB_OUTPUT"
fi
build-dev:
if: github.ref == 'refs/heads/main'
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
flavor: default
dockerfile: Dockerfile
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
flavor: default
dockerfile: Dockerfile
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
flavor: alpine
dockerfile: Dockerfile.alpine
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
flavor: alpine
dockerfile: Dockerfile.alpine
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: .
file: ${{ matrix.dockerfile }}
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.flavor }}-${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=dev-${{ matrix.flavor }}-${{ 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.flavor }}-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge-dev:
if: ${{ !cancelled() && 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-default-*
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}"
tags=("${{ env.IMAGE }}:dev" "${{ env.IMAGE }}:dev-${SHORT_SHA}")
# A flavor publishes only when every one of its arches built.
[ "$(ls | wc -l)" -eq 2 ] || { echo "expected 2 digests, found: $(ls)"; exit 1; }
docker buildx imagetools create $(printf -- '-t %s ' "${tags[@]}") \
$(printf '${{ env.IMAGE }}@sha256:%s ' *)
merge-dev-alpine:
if: ${{ !cancelled() && 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-alpine-*
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 Alpine multi-arch manifest
working-directory: /tmp/digests
run: |
SHORT_SHA="${GITHUB_SHA::7}"
tags=("${{ env.IMAGE }}:dev-alpine" "${{ env.IMAGE }}:dev-${SHORT_SHA}-alpine")
# A flavor publishes only when every one of its arches built.
[ "$(ls | wc -l)" -eq 2 ] || { echo "expected 2 digests, found: $(ls)"; exit 1; }
docker buildx imagetools create $(printf -- '-t %s ' "${tags[@]}") \
$(printf '${{ env.IMAGE }}@sha256:%s ' *)
build-release:
needs: read-version
if: github.ref == 'refs/heads/main' && needs.read-version.outputs.is_new_version == 'true'
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
flavor: default
dockerfile: Dockerfile
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
flavor: default
dockerfile: Dockerfile
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
flavor: alpine
dockerfile: Dockerfile.alpine
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
flavor: alpine
dockerfile: Dockerfile.alpine
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: .
file: ${{ matrix.dockerfile }}
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.flavor }}-${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=release-${{ matrix.flavor }}-${{ 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.flavor }}-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge-release:
needs: [read-version, build-release]
if: ${{ !cancelled() && needs.read-version.result == 'success' && github.ref == 'refs/heads/main' && needs.read-version.outputs.is_new_version == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-release-default-*
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.read-version.outputs.version }}"
MAJOR_MINOR="${VERSION%.*}"
tags=("${{ env.IMAGE }}:latest" "${{ env.IMAGE }}:${VERSION}" "${{ env.IMAGE }}:${MAJOR_MINOR}")
# A flavor publishes only when every one of its arches built.
[ "$(ls | wc -l)" -eq 2 ] || { echo "expected 2 digests, found: $(ls)"; exit 1; }
docker buildx imagetools create $(printf -- '-t %s ' "${tags[@]}") \
$(printf '${{ env.IMAGE }}@sha256:%s ' *)
merge-release-alpine:
needs: [read-version, build-release]
if: ${{ !cancelled() && needs.read-version.result == 'success' && github.ref == 'refs/heads/main' && needs.read-version.outputs.is_new_version == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-release-alpine-*
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 Alpine multi-arch manifest
working-directory: /tmp/digests
run: |
VERSION="${{ needs.read-version.outputs.version }}"
MAJOR_MINOR="${VERSION%.*}"
tags=("${{ env.IMAGE }}:alpine" "${{ env.IMAGE }}:latest-alpine" "${{ env.IMAGE }}:${VERSION}-alpine" "${{ env.IMAGE }}:${MAJOR_MINOR}-alpine")
# A flavor publishes only when every one of its arches built.
[ "$(ls | wc -l)" -eq 2 ] || { echo "expected 2 digests, found: $(ls)"; exit 1; }
docker buildx imagetools create $(printf -- '-t %s ' "${tags[@]}") \
$(printf '${{ env.IMAGE }}@sha256:%s ' *)