Skip to main content

Build a Docker image (multi-arch)

This workflows builds a Docker image for x86 and ARM, and deploys it to a server.

It uses BuildJet for the ARM build, since GitHub currently does not offer ARM runners and emulation is very slow.

.github/workflows/build.yml
name: 'Build and publish'

on:
workflow_dispatch:
# Allows to manually run the workflow from GitHub's UI
schedule:
# Rebuilds the image automatically every day,
# to ensure it stays up-to-date and secure
- cron: '0 8 * * *'
push:
branches:
- 'main'

env:
IMAGE_NAME: ghcr.io/${{ github.repository }}

jobs:
build:
strategy:
fail-fast: false
matrix:
arch: [x86, arm]
runs-on: ${{ matrix.arch == 'arm' && 'buildjet-2vcpu-ubuntu-2204-arm' || 'ubuntu-latest' }}
name: Build ${{ matrix.arch == 'arm' && 'ARM' || matrix.arch }} image
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Calculate short form of current git commit
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"

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

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push by digest
id: build
uses: docker/build-push-action@v4
with:
context: .
platforms: ${{ matrix.arch }}
outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
cache-from: type=registry,ref=ghcr.io/${{ env.IMAGE_NAME }}-buildcache:${{ matrix.arch }}
cache-to: type=registry,ref=ghcr.io/${{ env.IMAGE_NAME }}-buildcache:${{ matrix.arch }},mode=max

- 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@v3
with:
name: digests
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
runs-on: ubuntu-latest
name: 'Merge into a single manifest'
needs: build
permissions:
packages: write
steps:
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests

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

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=sha,prefix=git-
type=raw,value=latest,enable={{is_default_branch}}

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *)

- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}

deploy:
runs-on: ubuntu-latest
name: 'Deploy'
needs: merge
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Calculate short form of current git commit
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"

- name: Replace variables in stack file
env:
IMAGE_TAG: git-${{ steps.vars.outputs.sha_short }}
run: >
sed -e "s \${IMAGE_TAG} $IMAGE_TAG " \
-i .github/workflows/deploy/stack.yml
cat .github/workflows/deploy/stack.yml

- name: Trigger deployment
env:
API_KEY: ${{ secrets.StackSparkApiKey }}
STACKSPARK_HOST: ${{ secrets.StackSparkHost }}
STACK_NAME: stackspark
run: >
curl --fail -H "X-API-Key: $API_KEY" --data-binary "@.github/workflows/deploy/stack.yml" https://$STACKSPARK_HOST/deploy?stackName=$STACK_NAME