# ============================================================================= # ci.yaml — test every push; publish an image for pushes to main that pass. # # Modelled on job-tracker's ci.yaml. test and publish run on the build VM's # act_runner (stpbuild, label `build`); deploy runs on the stpweb host's # (stpvm, `self-hosted`), the only one with the stack. Both are 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). # # THREE 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 and pushes it # to this Gitea's container registry as : and :latest. It # reuses test's git cache, so both must run on the same runner. # deploy after publish, on stpvm: pulls it into the stpweb 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: build env: SHA: ${{ github.sha }} REF: ${{ github.ref }} # Names this run's Postgres and network (see SRC below). RUN: ${{ github.run_id }} # Over HTTP with the job's own token (read access to this repo while # the job runs), so the runner needs no SSH key or Gitea user. REMOTE: http://git.adam-french.co.uk:3000/adamf/ccp.git GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} CACHE: /home/adamf/.cache/activity-tracker-ci/repo.git # Per run, not just per commit: the build runner takes two jobs at # once, and the same commit pushed to two branches runs twice. SRC: /home/adamf/.cache/activity-tracker-ci/src-${{ github.sha }}-${{ github.run_id }} 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" # The job's token as a Basic auth header, passed via env rather # than -c so it never shows in the process list. auth=$(printf 'x-access-token:%s' "$GITEA_TOKEN" | base64 -w0) GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraHeader \ GIT_CONFIG_VALUE_0="Authorization: Basic $auth" \ 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-$RUN" docker run -d --name "ci-pg-$RUN" --network "ci-$RUN" --network-alias pg \ -e POSTGRES_PASSWORD=ci postgres:17 for i in $(seq 1 30); do docker exec "ci-pg-$RUN" pg_isready -U postgres -h localhost && exit 0 sleep 1 done docker logs "ci-pg-$RUN" 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-$RUN" \ -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-$RUN" || true docker network rm "ci-$RUN" || true publish: needs: test if: github.ref == 'refs/heads/main' runs-on: build env: SHA: ${{ github.sha }} CACHE: /home/adamf/.cache/activity-tracker-ci/repo.git SRC: /home/adamf/.cache/activity-tracker-ci/publish-${{ github.sha }}-${{ github.run_id }} 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" deploy: needs: publish runs-on: self-hosted steps: # Put the new image live, exactly as job-tracker does: pull and # recreate only this service, under .compose.lock, which stpweb's # deploy and maintenance 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/stpweb shell: bash run: | set -eu flock .compose.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