Skip to content
Merged
109 changes: 97 additions & 12 deletions .github/workflows/docker-images-build-and-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ on:

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
IMAGE_NAME: parktrack-project/api-server-rs
IS_DEFAULT: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}

jobs:
build-and-push:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write
Expand All @@ -26,6 +24,15 @@ jobs:
dockerfile: ./Dockerfile
- name: migrate
dockerfile: ./Dockerfile.migrate
platform:
- name: linux/amd64
runner: ubuntu-24.04
digest_file_suffix: linux-amd64
- name: linux/arm64
runner: ubuntu-24.04-arm
digest_file_suffix: linux-arm64

runs-on: ${{ matrix.platform.runner }}

steps:
- name: Checkout
Expand Down Expand Up @@ -56,19 +63,97 @@ jobs:
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.image.name }}
tags: |
type=raw,value=development,enable=${{ github.ref != format('refs/heads/{0}', github.event.repository.default_branch) }}
type=raw,value=latest,enable={{is_default_branch}}
type=ref,event=branch
type=sha,format=short

- name: Build and push
id: build
uses: docker/build-push-action@v7
with:
context: .
file: ${{ matrix.image.dockerfile }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ matrix.image.name }}
cache-to: type=gha,scope=${{ matrix.image.name }},mode=max
push: ${{ github.event_name == 'push' }}
cache-from: type=gha,scope=${{ matrix.image.name }}-${{ matrix.platform.name }}
cache-to: type=gha,scope=${{ matrix.image.name }}-${{ matrix.platform.name }},mode=max
push: ${{ github.event_name == 'push' }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.image.name }},push-by-digest=true,name-canonical=true,push=${{ github.event_name == 'push' }}
platforms: ${{ matrix.platform.name }}

- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"

- name: Upload digest artifact
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.image.name }}-${{ matrix.platform.digest_file_suffix }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1


merge-manifests:
name: Merge manifests

permissions:
contents: read
packages: write

runs-on: ubuntu-latest

if: github.event_name == 'push'

needs: build-and-push

strategy:
matrix:
image:
- name: app
- name: migrate

steps:
- name: Download all digests
uses: actions/download-artifact@v4
with:
pattern: digests-${{ matrix.image.name }}-*
merge-multiple: true
path: ${{ runner.temp }}/digests

- name: Extract metadata
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.image.name }}
tags: |
type=raw,value=development,enable=${{ github.ref != format('refs/heads/{0}', github.event.repository.default_branch) }}
type=raw,value=latest,enable={{is_default_branch}}
type=ref,event=branch
type=sha,format=short

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4

- name: Log in to GHCR
if: env.REGISTRY == 'ghcr.io' && github.event_name == 'push'
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Optional generic registry login for non-GHCR
- name: Log in to external registry
if: env.REGISTRY != 'ghcr.io' && github.event_name == 'push'
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Merge manifests
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create \
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.image.name }}@sha256:%s ' *)

Loading