The site and its runner moved from the arm64 Pi (stppi) to stpvm (x86-64), so the clone URL and the comments about the build host and image architecture were wrong. Co-Authored-By: Claude Opus 5.5 <[email protected]>
159 lines
7.2 KiB
YAML
159 lines
7.2 KiB
YAML
# =============================================================================
|
|
# ci.yaml — test every push; publish an image for pushes to main that pass.
|
|
#
|
|
# Modelled on job-tracker's ci.yaml and run by the same runner: a self-hosted
|
|
# act_runner on the web_server host (stpvm) in HOST mode, with git and docker and no other
|
|
# toolchain. So Go runs in a `docker run` container, and the source is
|
|
# fetched with plain git rather than actions/checkout (which needs Node on
|
|
# the host).
|
|
#
|
|
# TWO JOBS
|
|
# test gofmt, go vet, staticcheck and go test (with the database
|
|
# tests against a throwaway Postgres container), as `make check`
|
|
# minus -race (see the Go step). Then the Vue app's
|
|
# typecheck and build, in a node container.
|
|
# publish main only, after test: builds deploy/Dockerfile, pushes it to
|
|
# this Gitea's container registry as :<sha> and :latest, then
|
|
# pulls it into the web_server stack (its `activity-tracker`
|
|
# service) and waits for it to be healthy.
|
|
#
|
|
# SECRETS (repo settings -> Actions -> Secrets)
|
|
# REGISTRY_USER a Gitea user that can write packages for adamf
|
|
# REGISTRY_TOKEN that user's access token with the write:package scope
|
|
# =============================================================================
|
|
name: Test and publish
|
|
|
|
on:
|
|
push:
|
|
|
|
env:
|
|
IMAGE: adam-french.co.uk/adamf/activity-tracker
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: self-hosted
|
|
env:
|
|
SHA: ${{ github.sha }}
|
|
REF: ${{ github.ref }}
|
|
REMOTE: ssh://git@localhost:2222/adamf/ccp.git
|
|
GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no"
|
|
CACHE: /home/adamf/.cache/activity-tracker-ci/repo.git
|
|
SRC: /home/adamf/.cache/activity-tracker-ci/src-${{ github.sha }}
|
|
steps:
|
|
- name: Export the pushed commit to a scratch directory
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
mkdir -p "$(dirname "$CACHE")"
|
|
[ -d "$CACHE" ] || git init --quiet --bare "$CACHE"
|
|
git --git-dir="$CACHE" fetch --quiet --no-tags "$REMOTE" "$REF"
|
|
rm -rf "$SRC"
|
|
mkdir -p "$SRC"
|
|
git --git-dir="$CACHE" archive --format=tar "$SHA" | tar -x -C "$SRC"
|
|
test -f "$SRC/go.mod"
|
|
|
|
# A throwaway Postgres on its own network, for the database tests
|
|
# (internal/testdb gives each test its own database on it).
|
|
- name: Start Postgres
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
docker network create "ci-$SHA"
|
|
docker run -d --name "ci-pg-$SHA" --network "ci-$SHA" --network-alias pg \
|
|
-e POSTGRES_PASSWORD=ci postgres:17
|
|
for i in $(seq 1 30); do
|
|
docker exec "ci-pg-$SHA" pg_isready -U postgres -h localhost && exit 0
|
|
sleep 1
|
|
done
|
|
docker logs "ci-pg-$SHA"
|
|
exit 1
|
|
|
|
# Read-only source, and named volumes for the module and build
|
|
# caches so repeat runs don't download everything again. No -race:
|
|
# it was left out while CI ran on the arm64 Pi, whose 39-bit
|
|
# address space the race detector doesn't support, and has not
|
|
# been turned on since the move to x86-64. `make check` runs it.
|
|
- name: Go - lint and test
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
docker run --rm \
|
|
-v "$SRC":/src:ro -w /src \
|
|
-v activity_tracker_ci_gomod:/go/pkg/mod \
|
|
-v activity_tracker_ci_gobuild:/root/.cache/go-build \
|
|
--network "ci-$SHA" \
|
|
-e TEST_DATABASE_URL='postgres://postgres:ci@pg:5432/postgres?sslmode=disable' \
|
|
golang:1.27 \
|
|
sh -c 'test -z "$(gofmt -l .)" && go vet ./... && go tool staticcheck ./... && go test -count=1 ./...'
|
|
|
|
# Copies web/ out of the read-only source so node_modules and the
|
|
# build output stay in the container.
|
|
- name: Web - typecheck and build
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
docker run --rm \
|
|
-v "$SRC":/src:ro \
|
|
-v activity_tracker_ci_npm:/root/.npm \
|
|
node:24-alpine \
|
|
sh -c 'cp -r /src/web /web && cd /web && npm ci && npm run typecheck && npx vite build --outDir /tmp/dist'
|
|
|
|
- name: Remove the scratch directory and Postgres
|
|
if: always()
|
|
shell: bash
|
|
run: |
|
|
rm -rf "$SRC"
|
|
docker rm -f "ci-pg-$SHA" || true
|
|
docker network rm "ci-$SHA" || true
|
|
|
|
publish:
|
|
needs: test
|
|
if: github.ref == 'refs/heads/main'
|
|
runs-on: self-hosted
|
|
env:
|
|
SHA: ${{ github.sha }}
|
|
CACHE: /home/adamf/.cache/activity-tracker-ci/repo.git
|
|
SRC: /home/adamf/.cache/activity-tracker-ci/publish-${{ github.sha }}
|
|
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
|
|
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
|
|
steps:
|
|
- name: Build and push the image
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
rm -rf "$SRC" && mkdir -p "$SRC"
|
|
git --git-dir="$CACHE" archive --format=tar "$SHA" | tar -x -C "$SRC"
|
|
printf '%s' "$REGISTRY_TOKEN" | docker login adam-french.co.uk -u "$REGISTRY_USER" --password-stdin
|
|
# Built natively on the host, so the image is amd64 — the only
|
|
# platform it runs on.
|
|
docker build -f "$SRC/deploy/Dockerfile" -t "$IMAGE:$SHA" -t "$IMAGE:latest" "$SRC"
|
|
docker push "$IMAGE:$SHA"
|
|
docker push "$IMAGE:latest"
|
|
|
|
- name: Remove the scratch directory
|
|
if: always()
|
|
shell: bash
|
|
run: rm -rf "$SRC"
|
|
|
|
# Put the new image live, exactly as job-tracker does: pull and
|
|
# recreate only this service, under the lock web_server's deploy
|
|
# and env-apply hold while they run compose. A pinned
|
|
# ACTIVITY_TRACKER_IMAGE_TAG in the site's .env makes this a no-op.
|
|
- name: Roll the site onto the new image
|
|
working-directory: /home/adamf/deploy/web_server
|
|
env:
|
|
DOCKER_API_VERSION: "1.41"
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
flock env-apply/run/lock sh -c 'docker compose pull activity-tracker && docker compose up -d --no-deps activity-tracker'
|
|
for i in $(seq 1 30); do
|
|
status=$(docker inspect -f '{{.State.Health.Status}}' activity-tracker)
|
|
echo "activity-tracker: $status"
|
|
[ "$status" = healthy ] && exit 0
|
|
[ "$status" = unhealthy ] && break
|
|
sleep 5
|
|
done
|
|
docker logs --tail 50 activity-tracker
|
|
exit 1
|