Add auto-deploys, optimise Dockerfile & add running SHA to RIGS

This commit is contained in:
Joe Banks
2026-10-03 13:48:12 +01:00
parent eadca71cdc
commit 3e6d7adb61
5 changed files with 126 additions and 15 deletions

View File

@@ -77,18 +77,25 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
docker:
name: Docker image
runs-on: ubuntu-latest
# Each architecture builds on a native runner: emulating arm64 under QEMU (compiling pycairo, npm, uv) was the slow part
docker-build:
name: Docker build (${{ matrix.platform }})
runs-on: ${{ matrix.runner }}
needs: [lint, test]
permissions:
contents: read
packages: write
strategy:
fail-fast: true
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
- platform: linux/arm64
runner: ubuntu-24.04-arm
steps:
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
# Only authenticate when we are actually going to push (never for PRs, which may come from forks)
@@ -100,6 +107,60 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- id: build
name: Build (and push by digest on master)
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
build-args: GIT_SHA=${{ github.sha }}
outputs: type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=${{ github.event_name == 'push' }}
cache-from: type=gha,scope=${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
- name: Export digest
if: github.event_name == 'push'
env:
DIGEST: ${{ steps.build.outputs.digest }}
run: |
mkdir -p "$RUNNER_TEMP/digests"
touch "$RUNNER_TEMP/digests/${DIGEST#sha256:}"
- name: Upload digest
if: github.event_name == 'push'
uses: actions/upload-artifact@v4
with:
name: digests-${{ strategy.job-index }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
docker:
name: Docker image
runs-on: ubuntu-latest
needs: [docker-build]
if: github.event_name == 'push'
outputs:
tag: ${{ steps.sha-tag.outputs.tag }}
permissions:
contents: read
packages: write
steps:
- uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- 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 }}
- id: meta
uses: docker/metadata-action@v5
with:
@@ -108,13 +169,40 @@ jobs:
type=raw,value=latest
type=sha
- name: Build (and push on master)
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name == 'push' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- id: sha-tag
name: Pick the commit tag
env:
TAGS: ${{ steps.meta.outputs.tags }}
run: |
ref="$(grep -m1 ':sha-' <<< "$TAGS")"
test -n "$ref"
echo "tag=${ref##*:}" >> "$GITHUB_OUTPUT"
- name: Create multi-arch manifest
working-directory: ${{ runner.temp }}/digests
env:
IMAGE: ghcr.io/${{ github.repository }}
run: |
docker buildx imagetools create \
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf "$IMAGE@sha256:%s " *)
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: [docker]
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
environment: production
concurrency: deploy-production
steps:
- name: Deploy the image built for this commit
env:
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
TAG: ${{ needs.docker.outputs.tag }}
run: |
install -m 700 -d ~/.ssh
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts
ssh deploy@rigs.nottinghamtec.co.uk "$TAG"