Files
the-pet-loft/.gitea/workflows/deploy-staging.yml
ianshaloom 0bd0d90f45
All checks were successful
Deploy — Staging / Detect changed apps (push) Successful in 16s
Deploy — Staging / Lint, Typecheck & Test (push) Successful in 1m26s
Deploy — Staging / Build & push — ${{ matrix.app }} (push) Has been skipped
Deploy — Staging / Deploy to staging VPS (push) Has been skipped
feat(ci): skip build and deploy for unchanged apps
Add a changes job that diffs HEAD~1..HEAD and outputs which apps were
affected. Build and deploy jobs consume the output:

- build matrix is restricted to changed apps only — unchanged apps are
  never built or pushed
- deploy pulls only rebuilt images and restarts only those containers

Shared triggers (packages/, convex/, package-lock.json, turbo.json) mark
both apps as changed since they affect the full dependency tree.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-08 16:15:58 +03:00

266 lines
11 KiB
YAML

name: Deploy — Staging
on:
push:
branches:
- staging
# Gitea Actions has no environment-level secrets (unlike GitHub Actions).
# Staging and production secrets live at repo level, distinguished by prefix.
# Production workflow uses the same names with PROD_ prefix.
# (see: troubleshooting #8 — REGISTRY must include the owner segment)
#
# Required secrets (repo → Settings → Secrets and Variables → Actions):
# STAGING_REGISTRY — host:port/owner
# STAGING_REGISTRY_USER — Gitea username
# STAGING_REGISTRY_TOKEN — Gitea PAT (package:write)
# STAGING_SSH_HOST — host.containers.internal
# STAGING_SSH_USER — SSH user on the VPS
# STAGING_SSH_KEY — SSH private key (full PEM)
# STAGING_SSH_PORT — (optional) defaults to 22
# STAGING_NEXT_PUBLIC_CONVEX_URL
# STAGING_STOREFRONT_NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
# STAGING_ADMIN_NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
# STAGING_NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
# STAGING_NEXT_PUBLIC_CLOUDINARY_API_KEY
# STAGING_NEXT_PUBLIC_IMAGE_PROCESSING_API_URL
# STAGING_NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
# STAGING_STOREFRONT_CLERK_SECRET_KEY
# STAGING_ADMIN_CLERK_SECRET_KEY
# STAGING_CLOUDINARY_API_SECRET
jobs:
# ── 0. Detect changes ────────────────────────────────────────────────────────
# Determines which apps have changed so downstream jobs skip unchanged ones.
# Rules:
# apps/storefront/** → storefront
# apps/admin/** → admin
# packages/**, convex/** → both (shared code)
# package.json, package-lock.json, turbo.json → both (root config)
changes:
name: Detect changed apps
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.detect.outputs.matrix }}
storefront: ${{ steps.detect.outputs.storefront }}
admin: ${{ steps.detect.outputs.admin }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Determine affected apps
id: detect
run: |
# Fall back to diffing against an empty tree on the very first commit
BASE=$(git rev-parse HEAD~1 2>/dev/null || git hash-object -t tree /dev/null)
CHANGED=$(git diff --name-only "$BASE" HEAD)
STOREFRONT=false
ADMIN=false
# Shared paths affect both apps
if echo "$CHANGED" | grep -qE '^(package\.json|package-lock\.json|turbo\.json|packages/|convex/)'; then
STOREFRONT=true
ADMIN=true
fi
echo "$CHANGED" | grep -q '^apps/storefront/' && STOREFRONT=true
echo "$CHANGED" | grep -q '^apps/admin/' && ADMIN=true
# Build JSON matrix for the build job
APPS=()
[ "$STOREFRONT" = "true" ] && APPS+=("storefront")
[ "$ADMIN" = "true" ] && APPS+=("admin")
if [ ${#APPS[@]} -eq 0 ]; then
MATRIX="[]"
else
MATRIX=$(printf '"%s",' "${APPS[@]}" | sed 's/,$//')
MATRIX="[${MATRIX}]"
fi
echo "matrix=${MATRIX}" >> "$GITHUB_OUTPUT"
echo "storefront=${STOREFRONT}" >> "$GITHUB_OUTPUT"
echo "admin=${ADMIN}" >> "$GITHUB_OUTPUT"
# ── 1. CI ───────────────────────────────────────────────────────────────────
ci:
name: Lint, Typecheck & Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Typecheck
run: npm run type-check
- name: Test
run: npm run test:once
# ── 2. Build & push ─────────────────────────────────────────────────────────
# Matrix is restricted to apps that actually changed — unchanged apps are
# skipped entirely so their build minutes are not wasted.
build:
name: Build & push — ${{ matrix.app }}
needs: [ci, changes]
if: needs.changes.outputs.matrix != '[]'
runs-on: ubuntu-latest
strategy:
matrix:
app: ${{ fromJson(needs.changes.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Prune workspace for ${{ matrix.app }}
run: |
npx turbo prune ${{ matrix.app }} --docker
# convex/ is not an npm workspace so turbo prune excludes it — copy manually
cp -r convex out/full/convex
# turbo prune can't fully parse the npm 11 lockfile; replace with full lockfile
# so non-hoisted packages (e.g. apps/storefront/node_modules/@heroui/react)
# are present when npm ci runs inside the Docker build.
cp package-lock.json out/package-lock.json
- name: Authenticate with registry
# docker login fails for HTTP-only registries — pre-populate config.json instead
# (see: troubleshooting #7)
run: |
mkdir -p ~/.docker
AUTH=$(echo -n "${{ secrets.STAGING_REGISTRY_USER }}:${{ secrets.STAGING_REGISTRY_TOKEN }}" | base64 -w 0)
REGISTRY_HOST=$(echo "${{ secrets.STAGING_REGISTRY }}" | cut -d'/' -f1)
echo "{\"auths\":{\"${REGISTRY_HOST}\":{\"auth\":\"${AUTH}\"}}}" > ~/.docker/config.json
- name: Build & push ${{ matrix.app }}
# --push uses buildkit HTTPS internally, which fails for HTTP registries.
# --load + docker push goes through the Podman daemon (insecure=true). (#7, #12)
env:
STOREFRONT_CLERK_KEY: ${{ secrets.STAGING_STOREFRONT_NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY }}
ADMIN_CLERK_KEY: ${{ secrets.STAGING_ADMIN_NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY }}
NEXT_PUBLIC_CONVEX_URL: ${{ secrets.STAGING_NEXT_PUBLIC_CONVEX_URL }}
NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME: ${{ secrets.STAGING_NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME }}
NEXT_PUBLIC_CLOUDINARY_API_KEY: ${{ secrets.STAGING_NEXT_PUBLIC_CLOUDINARY_API_KEY }}
NEXT_PUBLIC_IMAGE_PROCESSING_API_URL: ${{ secrets.STAGING_NEXT_PUBLIC_IMAGE_PROCESSING_API_URL }}
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: ${{ secrets.STAGING_NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY }}
run: |
SHORT_SHA="${GITHUB_SHA::7}"
IMAGE="${{ secrets.STAGING_REGISTRY }}/${{ matrix.app }}"
if [ "${{ matrix.app }}" = "admin" ]; then
docker build \
-f apps/admin/Dockerfile \
--build-arg NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY="$ADMIN_CLERK_KEY" \
--build-arg NEXT_PUBLIC_CONVEX_URL="$NEXT_PUBLIC_CONVEX_URL" \
--build-arg NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME="$NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME" \
--build-arg NEXT_PUBLIC_CLOUDINARY_API_KEY="$NEXT_PUBLIC_CLOUDINARY_API_KEY" \
--build-arg NEXT_PUBLIC_IMAGE_PROCESSING_API_URL="$NEXT_PUBLIC_IMAGE_PROCESSING_API_URL" \
--load \
-t "${IMAGE}:staging" \
./out
else
docker build \
-f apps/storefront/Dockerfile \
--build-arg NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY="$STOREFRONT_CLERK_KEY" \
--build-arg NEXT_PUBLIC_CONVEX_URL="$NEXT_PUBLIC_CONVEX_URL" \
--build-arg NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="$NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY" \
--load \
-t "${IMAGE}:staging" \
./out
fi
docker tag "${IMAGE}:staging" "${IMAGE}:sha-${SHORT_SHA}"
docker push "${IMAGE}:staging"
docker push "${IMAGE}:sha-${SHORT_SHA}"
# ── 3. Deploy ───────────────────────────────────────────────────────────────
# Only pulls and restarts the containers whose images were just rebuilt.
deploy:
name: Deploy to staging VPS
needs: [build, changes]
if: needs.changes.outputs.matrix != '[]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Write SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.STAGING_SSH_KEY }}" > ~/.ssh/staging
chmod 600 ~/.ssh/staging
- name: Pull & restart containers on VPS
env:
REGISTRY: ${{ secrets.STAGING_REGISTRY }}
REGISTRY_USER: ${{ secrets.STAGING_REGISTRY_USER }}
REGISTRY_TOKEN: ${{ secrets.STAGING_REGISTRY_TOKEN }}
SSH_HOST: ${{ secrets.STAGING_SSH_HOST }}
SSH_USER: ${{ secrets.STAGING_SSH_USER }}
SSH_PORT: ${{ secrets.STAGING_SSH_PORT }}
CLERK_SECRET_KEY: ${{ secrets.STAGING_STOREFRONT_CLERK_SECRET_KEY }}
ADMIN_CLERK_SECRET_KEY: ${{ secrets.STAGING_ADMIN_CLERK_SECRET_KEY }}
CLOUDINARY_API_SECRET: ${{ secrets.STAGING_CLOUDINARY_API_SECRET }}
STOREFRONT_CHANGED: ${{ needs.changes.outputs.storefront }}
ADMIN_CHANGED: ${{ needs.changes.outputs.admin }}
run: |
REGISTRY_HOST=$(echo "$REGISTRY" | cut -d'/' -f1)
COMPOSE_B64=$(sed "s|\${REGISTRY}|${REGISTRY}|g" deploy/staging/compose.yml | base64 -w 0)
ssh -i ~/.ssh/staging \
-p "${SSH_PORT:-22}" \
-o StrictHostKeyChecking=accept-new \
"${SSH_USER}@${SSH_HOST}" bash -s << EOF
set -euo pipefail
echo "${REGISTRY_TOKEN}" \
| podman login "${REGISTRY_HOST}" \
-u "${REGISTRY_USER}" --password-stdin --tls-verify=false
# Pull only the images that were just rebuilt
[ "${STOREFRONT_CHANGED}" = "true" ] && podman pull --tls-verify=false "${REGISTRY}/storefront:staging"
[ "${ADMIN_CHANGED}" = "true" ] && podman pull --tls-verify=false "${REGISTRY}/admin:staging"
mkdir -p /opt/staging
echo "${COMPOSE_B64}" | base64 -d > /opt/staging/compose.yml
printf 'CLERK_SECRET_KEY=%s\nADMIN_CLERK_SECRET_KEY=%s\nCLOUDINARY_API_SECRET=%s\n' \
"${CLERK_SECRET_KEY}" "${ADMIN_CLERK_SECRET_KEY}" "${CLOUDINARY_API_SECRET}" \
> /opt/staging/.env
chmod 600 /opt/staging/.env
# Restart only the containers whose images changed
SERVICES=""
[ "${STOREFRONT_CHANGED}" = "true" ] && SERVICES="\${SERVICES} storefront"
[ "${ADMIN_CHANGED}" = "true" ] && SERVICES="\${SERVICES} admin"
cd /opt/staging
podman compose up -d --force-recreate --remove-orphans \${SERVICES}
podman image prune -f
EOF