--:--
notes/commonplace/github-actions-ghcr.mdx

NOTES / Commonplace ·

CI/CD with GitHub Actions and GHCR

A pipeline in three jobs, each depending on the previous one:

  1. Check on every push and pull request: install with a frozen lockfile, format check, lint, type check, tests, build, and bash -n on shell scripts.
  2. Image, only on main: build the Docker image and push it to ghcr.io/<owner>/<repo>, tagged with the commit SHA and latest. docker/build-push-action with platforms: linux/amd64,linux/arm64 builds for both Intel VMs and Arm servers.
  3. Deploy: SSH to the server and switch it to the new tag.

GHCR

  • The workflow logs in with the built-in GITHUB_TOKEN; the job needs permissions: packages: write.
  • An image built from a public repository is public too, so the server can docker pull without credentials. For a private one, make the package public or docker login ghcr.io on the server.
  • Tagging by commit SHA makes rollbacks trivial: deploy the previous tag.

Deploying over SSH safely

  • Generate a key used only by CI, add its public half to the server's authorized_keys, and store the private half as a repository or environment secret. Delete the local copy after.
  • Store the server's host key (ssh-keyscan <host>) as a secret too and write it to known_hosts, so CI verifies it is talking to your server.
  • Gate the job on a variable (if: vars.DEPLOY_ENABLED == 'true') so the pipeline stays green before the server exists.
  • The remote command should be a script in the repository (git pull && bash infra/deploy.sh <image>), not logic hidden in the workflow.

Multi-arch images without the wait

On GitHub's x86 runners, the arm64 half of a multi-platform build runs under QEMU emulation, which is many times slower: an uncached pnpm install plus bundling took over half an hour. If the build output is platform-independent (plain JavaScript, no native addons), do that work once on the runner's own CPU:

FROM --platform=$BUILDPLATFORM node:22-alpine AS build   # runs natively
# ... install and build ...
FROM node:22-alpine AS runtime                           # per target platform
COPY --from=build /out ./

Only the small runtime stage is built per platform. The same build went to about 40 seconds.

Things that look like CI problems but aren't

  • A skipped deploy job leaves the run green. Green does not mean deployed; check the job.
  • Runs in the same concurrency group queue: one stuck build delays every deploy behind it. Cancel it.
  • "Review deployments": the environment has required reviewers.
  • "Approve workflows to run": GitHub asks before running workflows from outside contributors or apps on a pull request.
  • Email on every finished run (success too) is a personal setting: Settings → Notifications → Actions.

Secrets vs variables

Secrets are encrypted and masked in logs (keys, hosts, tokens). Variables are plain configuration (feature flags such as DEPLOY_ENABLED). Both live under the repository's Settings, or per environment such as production. For getting app secrets to the server, see Managing secrets.