Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 152 additions & 19 deletions .github/workflows/deploy-dev2.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Ship every merge to dev2, where c0mpute.com runs since it left Railway (which
# deployed on merge by itself). The box pulls the merged commit, rebuilds the
# image(s) from this repo and restarts the compose stack under
# /home/anthony/www/c0mpute.com. Generated by cli-tools/dev2/dev2-site scaffold.
# Ship every merge to dev2, where c0mpute.com runs since it left Railway.
#
# dev2 never compiles anything. The `image` job builds .nixpacks/Dockerfile (the
# Next.js site plus the Rust `c0mpute` status aggregator) here on GitHub's runner
# and pushes ghcr.io/profullstack/c0mpute-web:<full commit sha>. The `deploy` job
# then runs /home/anthony/www/c0mpute.com/deploy-app.sh <sha> over ssh: with
# IMAGE_REPO set in its deploy.env the box only `docker pull`s that tag and does
# `compose up -d --no-build` (standby, health check, rollback unchanged). If the
# tag is missing the deploy stops with an error; it never falls back to a local
# build. dev2 was at ~2 GB free RAM and load 50-80 with cargo in every deploy.
#
# Pull auth: this job's short-lived GITHUB_TOKEN (packages: read) goes to the box
# on ssh stdin, is used once with a throwaway DOCKER_CONFIG and is never stored.
# If the package is made public (org admin: github.com/orgs/profullstack/packages
# -> c0mpute-web -> Change visibility) the box can also pull without it.
#
# The image holds no secrets: there are no build args, .env* is in .dockerignore,
# and app.env is mounted on dev2 at run time.
name: Deploy to dev2

on:
Expand All @@ -10,37 +24,153 @@ on:
workflow_dispatch:
inputs:
ref:
description: Git ref to deploy (defaults to the pushed commit)
description: Git ref to deploy (commit id or vX.Y.Z; defaults to the branch head)
required: false
type: string
deploy:
description: Deploy to dev2 after the image is pushed (false = build and push only)
required: false
type: boolean
default: true

# One deploying run at a time; a newer push replaces a pending one, so an older
# build can never deploy over a newer one. A build-only dispatch (deploy=false)
# stays out of that queue.
concurrency:
group: deploy-dev2
group: ${{ (github.event_name == 'push' || inputs.deploy) && 'deploy-dev2' || format('c0mpute-web-image-{0}', github.run_id) }}
cancel-in-progress: false

permissions:
contents: read

env:
IMAGE: ghcr.io/profullstack/c0mpute-web

jobs:
deploy:
image:
runs-on: ubuntu-latest
timeout-minutes: 45
timeout-minutes: 60
permissions:
contents: read
packages: write
outputs:
sha: ${{ steps.rev.outputs.sha }}
steps:
# ${{ }} values go through env, never straight into a run: body.
- name: Resolve target revision
id: rev
- name: Check the requested ref
env:
REF: ${{ inputs.ref || github.sha }}
# The ref ends up on the box's command line: ssh joins its arguments
# and the remote shell parses them again, so anything but a commit
# id or a release tag is refused here (`x; curl ... | sh` was a shell
# on dev2 for anyone who could dispatch this workflow).
# The sha ends up on the box's command line (ci-deploy only accepts hex),
# so anything but a commit id or a release tag is refused here.
shell: bash
run: |
# A bash match, not grep: it tests the whole value, newlines included.
if ! [[ "$REF" =~ ^([0-9a-f]{7,40}|v[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
echo "::error::ref must be a commit id (7-40 hex) or a release tag (vX.Y.Z)" >&2
exit 1
fi
echo "sha=$REF" >> "$GITHUB_OUTPUT"

- uses: actions/checkout@v5
with:
ref: ${{ inputs.ref || github.sha }}
persist-credentials: false

- name: Resolve the full commit id
id: rev
run: |
sha=$(git rev-parse HEAD)
[[ "$sha" =~ ^[0-9a-f]{40}$ ]] || { echo "::error::cannot resolve a commit id" >&2; exit 1; }
echo "sha=$sha" >> "$GITHUB_OUTPUT"
echo "building $IMAGE:$sha"

- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# A redeploy or rollback of a sha that was already built pushes nothing new.
- name: Already built?
id: have
env:
SHA: ${{ steps.rev.outputs.sha }}
run: |
if docker buildx imagetools inspect "$IMAGE:$SHA" >/dev/null 2>&1; then
echo "built=true" >> "$GITHUB_OUTPUT"; echo "$IMAGE:$SHA already in the registry"
else
echo "built=false" >> "$GITHUB_OUTPUT"
fi

# The Rust release build plus bun install/next build and three image stages
# need more than the runner's free disk.
- name: Free runner disk
if: steps.have.outputs.built != 'true'
run: |
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL /usr/local/share/boost
df -h /

- uses: docker/setup-buildx-action@v3
if: steps.have.outputs.built != 'true'

- name: Build and push
if: steps.have.outputs.built != 'true'
uses: docker/build-push-action@v6
with:
context: .
file: .nixpacks/Dockerfile
platforms: linux/amd64
push: true
provenance: false
tags: ${{ env.IMAGE }}:${{ steps.rev.outputs.sha }}
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ steps.rev.outputs.sha }}
org.opencontainers.image.url=https://c0mpute.com
org.opencontainers.image.title=c0mpute.com web
org.opencontainers.image.description=c0mpute.com: Next.js site plus the c0mpute status aggregator, as deployed on dev2
# Layer cache in the Actions cache (not artifacts, not GHCR storage):
# the Rust stage is reused whenever Cargo.*, node/ and plugins/ are unchanged.
cache-from: type=gha,scope=c0mpute-web
cache-to: type=gha,scope=c0mpute-web,mode=max

- name: Smoke test the image
if: steps.have.outputs.built != 'true'
env:
SHA: ${{ steps.rev.outputs.sha }}
run: |
docker pull -q "$IMAGE:$SHA"
docker run --rm --entrypoint /usr/local/bin/c0mpute "$IMAGE:$SHA" --version
test "$(docker inspect -f '{{ index .Config.Labels "org.opencontainers.image.revision" }}' "$IMAGE:$SHA")" = "$SHA"

# Keep the registry small: the newest 30 builds are plenty for rollbacks.
- name: Prune old images
if: steps.have.outputs.built != 'true'
continue-on-error: true
uses: actions/delete-package-versions@v5
with:
package-name: c0mpute-web
package-type: container
min-versions-to-keep: 30

- name: Package visibility
env:
SHA: ${{ steps.rev.outputs.sha }}
run: |
token=$(curl -fsS "https://ghcr.io/token?scope=repository:profullstack/c0mpute-web:pull" | jq -r .token)
code=$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $token" \
-H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \
"https://ghcr.io/v2/profullstack/c0mpute-web/manifests/$SHA")
if [ "$code" = 200 ]; then echo "c0mpute-web is public: anonymous pull works" >> "$GITHUB_STEP_SUMMARY"
else echo "c0mpute-web is private (HTTP $code): dev2 pulls with the deploy job's token" >> "$GITHUB_STEP_SUMMARY"; fi

deploy:
needs: image
if: github.event_name == 'push' || inputs.deploy
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read
packages: read
steps:
- name: Set up ssh
env:
SSH_KEY: ${{ secrets.DEV2_SSH_KEY }}
Expand All @@ -52,13 +182,16 @@ jobs:
printf '%s\n' "$KNOWN_HOSTS" > ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts

- name: Deploy
# The token rides stdin (never argv, never a file on the box); deploy-app.sh
# reads one line before it queues for a deploy slot.
- name: Deploy (pull the prebuilt image)
env:
DEV2_USER: ${{ secrets.DEV2_USER }}
DEV2_HOST: ${{ secrets.DEV2_HOST }}
SHA: ${{ steps.rev.outputs.sha }}
SHA: ${{ needs.image.outputs.sha }}
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
ssh -o BatchMode=yes "$DEV2_USER@$DEV2_HOST" \
printf '%s\n' "$GHCR_TOKEN" | ssh -o BatchMode=yes "$DEV2_USER@$DEV2_HOST" \
/home/anthony/www/c0mpute.com/deploy-app.sh "$SHA"

- name: Verify the site answers
Expand Down