From 68d9c4fc013707f3e7c41edb805b38e3f61d6dfb Mon Sep 17 00:00:00 2001 From: ehonis Date: Tue, 13 Oct 2026 23:28:45 -0400 Subject: [PATCH] Deploy every merge to main automatically (server and iPhone app) Published from the private repository by oss-sync. - Deploy every merge to main automatically (server and iPhone app) 2 files changed, 222 insertions(+) --- .github/workflows/deploy.yml | 175 +++++++++++++++++++++++++++++++++++ docs/self-hosting.md | 47 ++++++++++ 2 files changed, 222 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..1d51d1d --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,175 @@ +name: Deploy + +# Continuous deployment for an install that wants it: when CI passes on main, a +# self-hosted runner on the machine that serves Krakatoa fast-forwards the live +# checkout, rebuilds, restarts the service and checks it answers, rolling back +# if any of that fails. If the iPhone app's code changed, it then ships that too: +# an over-the-air update, and a new App Store build when native code changed. +# +# Off unless the repository says where the install is (repository variables), so +# a fork or a copy of this repository never runs it. + +on: + workflow_run: + workflows: [CI] + types: [completed] + branches: [main] + workflow_dispatch: + +# One deploy at a time, in order; a deploy is never cancelled halfway. +concurrency: + group: deploy + cancel-in-progress: false + +jobs: + server: + if: >- + vars.DEPLOY_DIR != '' && (github.event_name == 'workflow_dispatch' || + (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push')) + runs-on: krakatoa-deploy + timeout-minutes: 20 + outputs: + sha: ${{ steps.pull.outputs.after }} + mobile: ${{ steps.pull.outputs.mobile }} + env: + DEPLOY_SERVICE: ${{ vars.DEPLOY_SERVICE || 'krakatoa' }} + DEPLOY_HEALTH_URL: ${{ vars.DEPLOY_HEALTH_URL || 'http://127.0.0.1:8101/api/health' }} + defaults: + run: + shell: bash + working-directory: ${{ vars.DEPLOY_DIR }} + steps: + # Someone's uncommitted work in the live folder is never deployed over. Untracked + # files are left alone; a pull that would overwrite one fails on its own. + - name: The live checkout is clean and on main + run: | + set -euo pipefail + if [ -n "$(git status --porcelain --untracked-files=no)" ]; then + echo "::error::The live checkout has uncommitted changes; nothing was deployed." + git status --short --untracked-files=no + exit 1 + fi + if [ "$(git branch --show-current)" != main ]; then + echo "::error::The live checkout is on $(git branch --show-current), not main; nothing was deployed." + exit 1 + fi + + - name: Pull + id: pull + run: | + set -euo pipefail + before=$(git rev-parse HEAD) + git fetch --quiet origin main + git merge --ff-only --quiet origin/main + after=$(git rev-parse HEAD) + echo "before=$before" >> "$GITHUB_OUTPUT" + echo "after=$after" >> "$GITHUB_OUTPUT" + # Nothing new is nothing to do, unless someone asked for a deploy by hand. + if [ "$before" = "$after" ] && [ "${{ github.event_name }}" != workflow_dispatch ]; then + echo "Already at $(git log --oneline -1); nothing to deploy." + exit 0 + fi + echo "go=true" >> "$GITHUB_OUTPUT" + changed=$(git diff --name-only "$before" "$after") + printf '%s\n' "$changed" | grep -qE '(^|/)package(-lock)?\.json$' && echo "deps=true" >> "$GITHUB_OUTPUT" || true + # What the iPhone app is built from: its own folder. + printf '%s\n' "$changed" | grep -qE '^mobile/' && echo "mobile=true" >> "$GITHUB_OUTPUT" || true + echo "Deploying $(git log --oneline -1) (was ${before:0:7})" + git log --oneline "$before..$after" + + - name: Install dependencies + if: steps.pull.outputs.deps == 'true' + run: | + npm ci --no-audit --no-fund + + - name: Build + if: steps.pull.outputs.go == 'true' + run: npm run build + + - name: Restart + if: steps.pull.outputs.go == 'true' + run: | + set -euo pipefail + if [ "$(uname)" = Darwin ]; then + launchctl kickstart -k "gui/$(id -u)/$DEPLOY_SERVICE" + else + systemctl --user restart "$DEPLOY_SERVICE" + fi + + - name: The service answers + if: steps.pull.outputs.go == 'true' + run: | + sleep 2 + for i in $(seq 1 60); do + if curl -fsS --max-time 2 "$DEPLOY_HEALTH_URL" > /dev/null; then echo "Up after ${i}s"; exit 0; fi + sleep 1 + done + echo "::error::$DEPLOY_HEALTH_URL did not answer within 60s" + exit 1 + + # A failed build or a service that will not start puts the last good version back. + - name: Roll back + if: failure() && steps.pull.outputs.before != '' + run: | + set -uo pipefail + echo "::warning::Rolling back to ${{ steps.pull.outputs.before }}" + git reset --hard --quiet "${{ steps.pull.outputs.before }}" + if [ "${{ steps.pull.outputs.deps }}" = true ]; then + npm ci --no-audit --no-fund + fi + npm run build + if [ "$(uname)" = Darwin ]; then + launchctl kickstart -k "gui/$(id -u)/$DEPLOY_SERVICE" + else + systemctl --user restart "$DEPLOY_SERVICE" + fi + + # The iPhone app. Installed builds take an over-the-air update only when their native + # code matches (runtimeVersion is a fingerprint of it). When no App Store build has + # this fingerprint yet, one is started and submitted to TestFlight on its own. + iphone: + needs: server + if: needs.server.outputs.mobile == 'true' && vars.EAS_PROJECT_ID != '' + runs-on: ubuntu-latest + timeout-minutes: 30 + env: + EXPO_TOKEN: ${{ secrets.EXPO_TOKEN }} + defaults: + run: + working-directory: mobile + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.server.outputs.sha }} + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + cache-dependency-path: mobile/package-lock.json + - run: npm ci --no-audit --no-fund + # The install's identity is not in the repository (mobile/app.config.js). + - name: The install's app identity + env: + BUNDLE_ID: ${{ vars.IOS_BUNDLE_ID }} + PROJECT_ID: ${{ vars.EAS_PROJECT_ID }} + run: | + node -e 'require("fs").writeFileSync("app.local.json", JSON.stringify({ bundleIdentifier: process.env.BUNDLE_ID, easProjectId: process.env.PROJECT_ID }))' + + - name: Is there a build with this native code? + id: native + run: | + set -euo pipefail + rv=$(npx expo-updates runtimeversion:resolve --platform ios | node -pe 'JSON.parse(require("fs").readFileSync(0, "utf8")).runtimeVersion') + builds=$(npx eas-cli build:list --platform ios --build-profile production --runtime-version "$rv" --limit 10 --json --non-interactive) + live=$(printf '%s' "$builds" | node -pe 'JSON.parse(require("fs").readFileSync(0, "utf8")).filter(b => !["ERRORED", "CANCELED"].includes(b.status)).length') + echo "runtime=$rv" >> "$GITHUB_OUTPUT" + if [ "$live" = 0 ]; then echo "build=true" >> "$GITHUB_OUTPUT"; echo "Native code changed: no build has runtime $rv yet."; else echo "A build has runtime $rv."; fi + + - name: Publish the update + run: npx eas-cli update --channel production --platform ios --non-interactive --message "$(git log -1 --format=%s)" + + - name: Start an App Store build + if: steps.native.outputs.build == 'true' + run: | + npx eas-cli build --platform ios --profile production --auto-submit --non-interactive --no-wait --message "$(git log -1 --format=%s)" + echo "::notice::Native code changed, so a new App Store build was started and will be submitted to TestFlight. The update above reaches only that build." diff --git a/docs/self-hosting.md b/docs/self-hosting.md index befb8a8..2902952 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -272,3 +272,50 @@ before). The server picks the file up without a restart. Every chain entry in `restaurants.json` records its source URL and date. The restaurant figures belong to the respective chains and are not covered by this repository's MIT license. + +## Deploying every merge (optional) + +`.github/workflows/deploy.yml` deploys `main` after CI passes on it. It runs on a +[self-hosted GitHub Actions runner](https://docs.github.com/actions/hosting-your-own-runners) +on the machine that serves the app, so nothing has to reach that machine from outside. +Each deploy: + +1. stops if the live checkout has uncommitted changes to tracked files or is not on `main`; +2. fast-forwards it to `origin/main` (nothing new, nothing done), runs `npm ci` only if a + `package.json` or lockfile changed, and builds; +3. restarts the service (`launchctl kickstart -k` on macOS, + `systemctl --user restart` on Linux), then waits up to a minute for the health URL; +4. if the build, the restart or the health check fails, resets to the commit it started + from, builds that and restarts again. + +When the iPhone app changed (`mobile/`), a second job ships it. It publishes an over-the-air +update to the `production` channel; installed builds take it only when their native code +matches (the runtime version is a fingerprint). When no App Store build has that +fingerprint yet, it also starts one with `--auto-submit`, so it lands in TestFlight. + +It does nothing until the repository is told where the install is: + +```bash +# On the serving machine, as the user the service runs as: a runner for this repository +# with the label the workflow asks for (Settings → Actions → Runners → New self-hosted +# runner gives the download and the token). +./config.sh --url https://github.com// --token --labels krakatoa-deploy --no-default-labels +./svc.sh install && ./svc.sh start +# Give it the PATH npm needs, in .env beside config.sh, e.g.: +# PATH=/opt/homebrew/opt/node/bin:/opt/homebrew/bin:/usr/bin:/bin + +# Repository variables (Settings → Secrets and variables → Actions): +gh variable set DEPLOY_DIR -b /path/to/the/live/checkout +gh variable set DEPLOY_SERVICE -b krakatoa # the launchd label or systemd unit +gh variable set DEPLOY_HEALTH_URL -b http://127.0.0.1:8101/api/health # the default + +# Optional, for the iPhone app: +gh variable set EAS_PROJECT_ID -b # what mobile/app.local.json holds +gh variable set IOS_BUNDLE_ID -b +gh secret set EXPO_TOKEN # an Expo access token +``` + +Run one by hand from the Actions tab (Deploy → Run workflow) or with +`gh workflow run deploy.yml`. Only people who can push to the repository can trigger it, so +use it on a private repository you control: a self-hosted runner runs whatever a workflow +on that repository asks of it.