Split deploy into build+deploy jobs
Browse files at this commitef463c1b78a4d06e58d6d9f4abbdd49613abdd0cParent: b2780eb- chore
- split deploy into build+deploy jobs
- fix CI disk exhaustion
1 changed file +124 -9
- .github/workflows/main.yml +124 -9
.github/workflows/main.ymlmodified+124 -9
| @@ -1,7 +1,22 @@ | |||
| 1 | -# Deploys to the VPS on every push to main, im-style (see im's | 1 | +# VPS deploy, im-style, split into two jobs so failure recovery is cheap: |
| 2 | -# .github/workflows/main.yml): the whole suite gates the deploy, the release | 2 | +# |
| 3 | -# lands as one swapped directory, and the service must answer /health from | 3 | +# Build and test — Postgres sidecar, clippy, the whole suite, the release |
| 4 | -# this build's process — or the previous release is rolled back. | 4 | +# build; uploads the staged release as the run's artifact. |
| 5 | +# Deploy — SSH release-dir swap + systemd user-unit restart, | ||
| 6 | +# /health gated on THIS build's process, rollback on | ||
| 7 | +# failure, independent after-the-fact verify. | ||
| 8 | +# | ||
| 9 | +# The split is the recovery story, not ceremony: | ||
| 10 | +# * "Re-run failed jobs" on a run whose Deploy step failed re-runs ONLY the | ||
| 11 | +# ~1-minute deploy job — the built release is still in the run's artifact. | ||
| 12 | +# * workflow_dispatch (the manual door: Actions tab → Run workflow on main, | ||
| 13 | +# or `gh workflow run main.yml --ref main`) deploys WITHOUT the suite: it | ||
| 14 | +# takes the newest artifact a green Build-and-test produced — which can | ||
| 15 | +# lag HEAD while a push is mid-CI, and that is the point: redeploy the | ||
| 16 | +# known-good release, e.g. after a server-side fix (env file, proxy, | ||
| 17 | +# database). For env-only changes a plain | ||
| 18 | +# `ssh <vps> 'systemctl --user restart hezarfen'` needs no workflow at | ||
| 19 | +# all: the process reads its env at boot. | ||
| 5 | # | 20 | # |
| 6 | # One-time prerequisites, outside the workflow's reach: | 21 | # One-time prerequisites, outside the workflow's reach: |
| 7 | # * the repo secrets SSH_PRIVATE_KEY / SSH_HOST / SSH_USER (im's values | 22 | # * the repo secrets SSH_PRIVATE_KEY / SSH_HOST / SSH_USER (im's values |
| @@ -11,13 +26,19 @@ | |||
| 11 | # * a Postgres the backend can reach, and ~/hezarfen/hezarfen.env (0600) | 26 | # * a Postgres the backend can reach, and ~/hezarfen/hezarfen.env (0600) |
| 12 | # with at least DATABASE_URL — see deploy/hezarfen.service for the keys. | 27 | # with at least DATABASE_URL — see deploy/hezarfen.service for the keys. |
| 13 | 28 | ||
| 29 | +name: VPS deploy | ||
| 30 | + | ||
| 14 | on: | 31 | on: |
| 15 | push: | 32 | push: |
| 16 | branches: | 33 | branches: |
| 17 | - main | 34 | - main |
| 35 | + # The manual redeploy door — see the header comment for how and why. | ||
| 36 | + workflow_dispatch: | ||
| 18 | 37 | ||
| 19 | -# The deploy swaps a release directory and restarts one service; two of them | 38 | +# One deploy at a time: the deploy swaps a release directory and restarts one |
| 20 | -# racing would leave the binary and the running process from different builds. | 39 | +# service; two of them racing would leave the binary and the running process |
| 40 | +# from different builds. A dispatched redeploy queues behind any in-flight | ||
| 41 | +# push deploy rather than racing it. | ||
| 21 | concurrency: | 42 | concurrency: |
| 22 | group: deploy | 43 | group: deploy |
| 23 | cancel-in-progress: false | 44 | cancel-in-progress: false |
| @@ -51,16 +72,38 @@ env: | |||
| 51 | SQLX_OFFLINE: "true" | 72 | SQLX_OFFLINE: "true" |
| 52 | 73 | ||
| 53 | jobs: | 74 | jobs: |
| 54 | - deploy: | 75 | + build: |
| 55 | - name: Deploy | 76 | + name: Build and test |
| 77 | + # Pushes only: a manual redeploy reuses the newest green artifact | ||
| 78 | + # instead of paying the suite again. | ||
| 79 | + if: github.event_name == 'push' | ||
| 56 | # Pinned, not `latest`: ubuntu-latest floating to the next LTS would | 80 | # Pinned, not `latest`: ubuntu-latest floating to the next LTS would |
| 57 | # one day ship a glibc newer than the server's, and the gnu binary | 81 | # one day ship a glibc newer than the server's, and the gnu binary |
| 58 | # would stop running there. See the TARGET comment above. | 82 | # would stop running there. See the TARGET comment above. |
| 59 | runs-on: ubuntu-24.04 | 83 | runs-on: ubuntu-24.04 |
| 84 | + env: | ||
| 85 | + # The 41 test binaries each link the full tonic/otlp/axum/sqlx stack; | ||
| 86 | + # with debug info they blow past the runner's free disk and the | ||
| 87 | + # linker dies with SIGBUS (run 34736185339). The only cost of | ||
| 88 | + # dropping it is line-number backtraces. | ||
| 89 | + CARGO_PROFILE_TEST_DEBUG: "0" | ||
| 90 | + CARGO_PROFILE_DEV_DEBUG: "0" | ||
| 91 | + # A one-shot CI build never reuses incremental artifacts — they are | ||
| 92 | + # pure disk waste. | ||
| 93 | + CARGO_INCREMENTAL: "0" | ||
| 60 | steps: | 94 | steps: |
| 95 | + # The runner's preinstalled SDKs (~20GB of Android/.NET/ghc/CodeQL) | ||
| 96 | + # are dead weight here, and the suite needs the room (see the env | ||
| 97 | + # block above for the other half of the fix). | ||
| 98 | + - name: Free disk space | ||
| 99 | + run: | | ||
| 100 | + df -h / | ||
| 101 | + sudo rm -rf /usr/local/lib/android /usr/share/dotnet /opt/ghc /opt/hostedtoolcache/CodeQL | ||
| 102 | + sudo docker image prune -af >/dev/null || true | ||
| 103 | + df -h / | ||
| 104 | + | ||
| 61 | - name: Checkout | 105 | - name: Checkout |
| 62 | uses: actions/checkout@v4 | 106 | uses: actions/checkout@v4 |
| 63 | - | ||
| 64 | - name: Set up Rust | 107 | - name: Set up Rust |
| 65 | run: rustup toolchain install stable --profile minimal | 108 | run: rustup toolchain install stable --profile minimal |
| 66 | 109 | ||
| @@ -118,6 +161,78 @@ jobs: | |||
| 118 | strip stage/hezarfen_backend | 161 | strip stage/hezarfen_backend |
| 119 | ls -l stage | 162 | ls -l stage |
| 120 | 163 | ||
| 164 | + # The deploy job (and, later, a manual redeploy) consumes this. 30 | ||
| 165 | + # days is also the dispatch door's reach-back window: an older green | ||
| 166 | + # build's artifact is gone, and the redeploy fails loudly instead of | ||
| 167 | + # silently shipping something ancient. | ||
| 168 | + - name: Upload the release | ||
| 169 | + uses: actions/upload-artifact@v4 | ||
| 170 | + with: | ||
| 171 | + name: release | ||
| 172 | + path: stage | ||
| 173 | + retention-days: 30 | ||
| 174 | + | ||
| 175 | + deploy: | ||
| 176 | + name: Deploy | ||
| 177 | + needs: build | ||
| 178 | + # Push: only behind a green build — the suite gates the server. | ||
| 179 | + # Dispatch: build is skipped on purpose (see its `if`), and `always()` | ||
| 180 | + # is what stops that skip from cascading past this gate. | ||
| 181 | + if: >- | ||
| 182 | + always() && | ||
| 183 | + (needs.build.result == 'success' || | ||
| 184 | + (github.event_name == 'workflow_dispatch' && needs.build.result == 'skipped')) | ||
| 185 | + runs-on: ubuntu-24.04 | ||
| 186 | + steps: | ||
| 187 | + # A push deploy takes the release this very run built and gated. | ||
| 188 | + - name: Fetch this run's release | ||
| 189 | + if: github.event_name == 'push' | ||
| 190 | + uses: actions/download-artifact@v4 | ||
| 191 | + with: | ||
| 192 | + name: release | ||
| 193 | + path: stage | ||
| 194 | + | ||
| 195 | + # A manual redeploy takes the newest release whose BUILD job went | ||
| 196 | + # green — not the newest fully-green run: a run can be red because | ||
| 197 | + # Deploy failed at the health gate while its build is exactly the | ||
| 198 | + # release we want back on the server. `gh run list --status success` | ||
| 199 | + # would skip exactly those, hence the per-run job check. | ||
| 200 | + - name: Find the newest green build | ||
| 201 | + if: github.event_name == 'workflow_dispatch' | ||
| 202 | + env: | ||
| 203 | + GH_TOKEN: ${{ github.token }} | ||
| 204 | + run: | | ||
| 205 | + set -euo pipefail | ||
| 206 | + run_id="" | ||
| 207 | + for rid in $(gh run list --workflow main.yml --branch main --event push --limit 15 --json databaseId --jq '.[].databaseId'); do | ||
| 208 | + if [ "$(gh run view "$rid" --json jobs --jq '.jobs[] | select(.name=="Build and test") | .conclusion')" = "success" ]; then | ||
| 209 | + run_id=$rid | ||
| 210 | + break | ||
| 211 | + fi | ||
| 212 | + done | ||
| 213 | + if [ -z "$run_id" ]; then | ||
| 214 | + echo 'no run on main has a green Build-and-test job (or its artifact has expired)' >&2 | ||
| 215 | + exit 1 | ||
| 216 | + fi | ||
| 217 | + echo "redeploying the release from run $run_id" | ||
| 218 | + echo "run_id=$run_id" >> "$GITHUB_ENV" | ||
| 219 | + | ||
| 220 | + - name: Fetch the green release | ||
| 221 | + if: github.event_name == 'workflow_dispatch' | ||
| 222 | + uses: actions/download-artifact@v4 | ||
| 223 | + with: | ||
| 224 | + name: release | ||
| 225 | + path: stage | ||
| 226 | + run-id: ${{ env.run_id }} | ||
| 227 | + github-token: ${{ github.token }} | ||
| 228 | + | ||
| 229 | + - name: Check the release is complete | ||
| 230 | + run: | | ||
| 231 | + set -euo pipefail | ||
| 232 | + test -f stage/hezarfen_backend | ||
| 233 | + test -d stage/migrations/control | ||
| 234 | + test -f stage/hezarfen.service | ||
| 235 | + | ||
| 121 | - name: Install SSH keys | 236 | - name: Install SSH keys |
| 122 | run: | | 237 | run: | |
| 123 | mkdir -p ~/.ssh | 238 | mkdir -p ~/.ssh |