Deploy workflow — test gate, dist build, atomic swap, health check, stylesheet verify
Browse files at this commit71f9a9c633937f9abc5bdfff93ac68ccc954feaeParent: 963c897- ci
- deploy workflow — test gate, dist build, atomic swap, health check, stylesheet verify
1 changed file +207 -0
- .github/workflows/main.yml +207 -0
.github/workflows/main.ymladded+207 -0
| @@ -0,0 +1,207 @@ | |||
| 1 | +on: | ||
| 2 | + push: | ||
| 3 | + branches: | ||
| 4 | + - main | ||
| 5 | + | ||
| 6 | +# The deploy swaps a release directory and restarts one service; two of them | ||
| 7 | +# racing would leave the binary and the running process from different builds. | ||
| 8 | +concurrency: | ||
| 9 | + group: deploy | ||
| 10 | + cancel-in-progress: false | ||
| 11 | + | ||
| 12 | +env: | ||
| 13 | + # A gnu binary links against the runner's glibc, so the runner is pinned | ||
| 14 | + # (see runs-on below) to the same 24.04 the server runs; the day either | ||
| 15 | + # side moves, the pin is what forces a conscious decision rather than a | ||
| 16 | + # broken deploy. In exchange: no cross-toolchain, and the linker is | ||
| 17 | + # rustc's default bundled lld. | ||
| 18 | + TARGET: x86_64-unknown-linux-gnu | ||
| 19 | + # The systemd user unit that owns the process. See im.service. | ||
| 20 | + SERVICE: im | ||
| 21 | + # `release` keeps fat LTO in one codegen unit for when the last few percent | ||
| 22 | + # are worth five minutes. `dist` is that profile with thin LTO and the cores | ||
| 23 | + # actually used. See the profile in Cargo.toml. | ||
| 24 | + PROFILE: dist | ||
| 25 | + TOPCOAT: 0.6.2 | ||
| 26 | + # The deploy lives in this subdirectory of the SSH user's home. The home | ||
| 27 | + # itself is resolved on the server ($HOME), so switching the deploy user | ||
| 28 | + # moves the path with it — a baked-in absolute path once kept pointing at | ||
| 29 | + # the previous user's home and failed the deploy on permissions. | ||
| 30 | + DEPLOY_SUBDIR: im | ||
| 31 | + | ||
| 32 | +jobs: | ||
| 33 | + deploy: | ||
| 34 | + name: Deploy | ||
| 35 | + # Pinned, not `latest`: ubuntu-latest floating to the next LTS would | ||
| 36 | + # one day ship a glibc newer than the server's, and the gnu binary | ||
| 37 | + # would stop running there. See the TARGET comment above. | ||
| 38 | + runs-on: ubuntu-24.04 | ||
| 39 | + steps: | ||
| 40 | + - name: Checkout | ||
| 41 | + uses: actions/checkout@v4 | ||
| 42 | + | ||
| 43 | + - name: Set up Rust | ||
| 44 | + run: rustup toolchain install stable --profile minimal | ||
| 45 | + | ||
| 46 | + - name: Cache cargo | ||
| 47 | + uses: Swatinem/rust-cache@v2 | ||
| 48 | + with: | ||
| 49 | + cache-all-crates: true | ||
| 50 | + # Bump when the target, the profile or any rustflags change: the | ||
| 51 | + # key does not otherwise see those, and a run that restores the | ||
| 52 | + # old key reports it up-to-date at save time — discarding the | ||
| 53 | + # recompiled dependencies and silently recompiling them on every | ||
| 54 | + # later run. | ||
| 55 | + prefix-key: v1-rust-gnu | ||
| 56 | + | ||
| 57 | + # A broken build must never reach the server: the whole workspace's | ||
| 58 | + # tests gate the deploy. | ||
| 59 | + - name: Test | ||
| 60 | + run: cargo test --workspace | ||
| 61 | + | ||
| 62 | + # Compiling the CLI from source costs minutes and it changes only when | ||
| 63 | + # the pinned version does, so it is cached on that version alone. | ||
| 64 | + - name: Cache the topcoat CLI | ||
| 65 | + id: topcoat-cache | ||
| 66 | + uses: actions/cache@v4 | ||
| 67 | + with: | ||
| 68 | + path: ~/.cargo/bin/topcoat | ||
| 69 | + key: topcoat-cli-${{ env.TOPCOAT }}-${{ runner.os }} | ||
| 70 | + | ||
| 71 | + - name: Install the topcoat CLI | ||
| 72 | + # Installed before the cargo config below exists, so the CLI itself | ||
| 73 | + # is built for the host and not for the deploy target. | ||
| 74 | + if: steps.topcoat-cache.outputs.cache-hit != 'true' | ||
| 75 | + run: cargo install topcoat-cli@${{ env.TOPCOAT }} --locked | ||
| 76 | + | ||
| 77 | + - name: Point cargo at the deploy target | ||
| 78 | + run: | | ||
| 79 | + set -euo pipefail | ||
| 80 | + # `topcoat asset bundle` runs its own `cargo build` and strips every | ||
| 81 | + # CARGO_* variable from it, so the target cannot be passed by | ||
| 82 | + # environment; it goes into the cargo config that inner build | ||
| 83 | + # reads. This file is written in CI only; it is not committed. | ||
| 84 | + mkdir -p .cargo | ||
| 85 | + printf '[build]\ntarget = "%s"\n' "$TARGET" >> .cargo/config.toml | ||
| 86 | + | ||
| 87 | + - name: Build the binary and bundle its assets | ||
| 88 | + run: | | ||
| 89 | + set -euo pipefail | ||
| 90 | + # One compile: this builds im-web and writes out the asset bundle | ||
| 91 | + # for the binary it produced. | ||
| 92 | + topcoat asset bundle --package im-web --profile "$PROFILE" --out stage/assets | ||
| 93 | + cp "target/$TARGET/$PROFILE/im-web" stage/im-web | ||
| 94 | + strip stage/im-web | ||
| 95 | + ls -l stage stage/assets | ||
| 96 | + | ||
| 97 | + - name: Install SSH keys | ||
| 98 | + run: | | ||
| 99 | + mkdir -p ~/.ssh | ||
| 100 | + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa | ||
| 101 | + ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts | ||
| 102 | + | ||
| 103 | + - name: Upload the release | ||
| 104 | + run: | | ||
| 105 | + set -euo pipefail | ||
| 106 | + ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" | ||
| 107 | + | ||
| 108 | + # The server's login shell is not bash, and ssh would hand this | ||
| 109 | + # script to it; `bash -s` takes it on stdin instead, so the login | ||
| 110 | + # shell only ever parses one plain command. | ||
| 111 | + # | ||
| 112 | + # The deploy path is $DEPLOY_SUBDIR under the SSH user's home, | ||
| 113 | + # resolved on the server as $HOME: there is no path secret to go | ||
| 114 | + # stale, and switching the deploy user moves the deploy with it. | ||
| 115 | + ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" <<'REMOTE' | ||
| 116 | + set -euo pipefail | ||
| 117 | + case "$1" in | ||
| 118 | + ''|/*|*..*) | ||
| 119 | + echo "Refusing a broad deploy subdirectory: '$1'" >&2 | ||
| 120 | + exit 1 | ||
| 121 | + ;; | ||
| 122 | + esac | ||
| 123 | + deploy_path=$HOME/$1 | ||
| 124 | + # The deploy path is the service's working directory: config/, | ||
| 125 | + # im.db and im.key live there and are never touched from here. | ||
| 126 | + # Only 'current' is replaced. | ||
| 127 | + mkdir -p -- "$deploy_path" | ||
| 128 | + rm -rf -- "$deploy_path/current.new" | ||
| 129 | + mkdir -p -- "$deploy_path/current.new" | ||
| 130 | + REMOTE | ||
| 131 | + # stdin carries the tarball, so the script cannot; the extraction | ||
| 132 | + # still goes through bash -c because the login shell is fish and | ||
| 133 | + # would not expand $HOME. | ||
| 134 | + tar -C stage -czf - . | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1/current.new\"' -- '$DEPLOY_SUBDIR'" | ||
| 135 | + | ||
| 136 | + - name: Swap and restart | ||
| 137 | + run: | | ||
| 138 | + set -euo pipefail | ||
| 139 | + ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" | ||
| 140 | + | ||
| 141 | + # bash -s for the same reason as above: the login shell is not | ||
| 142 | + # bash and must never see this script. | ||
| 143 | + ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE'" <<'REMOTE' | ||
| 144 | + set -euo pipefail | ||
| 145 | + deploy_path=$HOME/$1 | ||
| 146 | + service=$2 | ||
| 147 | + export XDG_RUNTIME_DIR=/run/user/$(id -u) | ||
| 148 | + # Read before anything moves: a config the health check cannot | ||
| 149 | + # read is a failure to stop on, not one to roll back from. | ||
| 150 | + listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$deploy_path/config/im.toml") | ||
| 151 | + if [ -z "$listen" ]; then | ||
| 152 | + echo "no listen address in $deploy_path/config/im.toml" >&2 | ||
| 153 | + exit 1 | ||
| 154 | + fi | ||
| 155 | + chmod +x -- "$deploy_path/current.new/im-web" | ||
| 156 | + rm -rf -- "$deploy_path/current.previous" | ||
| 157 | + if [ -e "$deploy_path/current" ]; then | ||
| 158 | + mv -- "$deploy_path/current" "$deploy_path/current.previous" | ||
| 159 | + fi | ||
| 160 | + mv -- "$deploy_path/current.new" "$deploy_path/current" | ||
| 161 | + systemctl --user restart "$service" | ||
| 162 | + # The unit is Type=simple, so a restart returns before the process | ||
| 163 | + # has bound its port. Wait for the health route rather than a clock. | ||
| 164 | + for _ in $(seq 1 30); do | ||
| 165 | + if curl -fsS "http://$listen/healthz" >/dev/null 2>&1; then | ||
| 166 | + echo 'im is up' | ||
| 167 | + rm -rf -- "$deploy_path/current.previous" | ||
| 168 | + exit 0 | ||
| 169 | + fi | ||
| 170 | + sleep 1 | ||
| 171 | + done | ||
| 172 | + echo 'im did not answer /healthz; rolling back' >&2 | ||
| 173 | + rm -rf -- "$deploy_path/current" | ||
| 174 | + mv -- "$deploy_path/current.previous" "$deploy_path/current" | ||
| 175 | + systemctl --user restart "$service" | ||
| 176 | + exit 1 | ||
| 177 | + REMOTE | ||
| 178 | + | ||
| 179 | + # The independent after-the-fact check: the page's stylesheet URL must | ||
| 180 | + # answer with bytes byte-identical to the bundle this deploy staged. | ||
| 181 | + - name: Verify the served stylesheet is this deploy's | ||
| 182 | + run: | | ||
| 183 | + set -euo pipefail | ||
| 184 | + ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" | ||
| 185 | + # One remote script: mixing runner-side and server-side variables in | ||
| 186 | + # nested ssh quoting once shipped a check that failed on a healthy | ||
| 187 | + # deploy. Everything server-side happens inside the heredoc; only the | ||
| 188 | + # stylesheet bytes come back on stdout. | ||
| 189 | + ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" > served-stylesheet.css <<'REMOTE' | ||
| 190 | + set -euo pipefail | ||
| 191 | + deploy_path=$HOME/$1 | ||
| 192 | + listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$deploy_path/config/im.toml") | ||
| 193 | + url=$(curl -fsS "http://${listen}/" | grep -oE '/_topcoat/assets/main-[a-f0-9]+\.css' | head -n1) | ||
| 194 | + if [ -z "$url" ]; then | ||
| 195 | + echo 'the served page references no stylesheet' >&2 | ||
| 196 | + exit 1 | ||
| 197 | + fi | ||
| 198 | + echo "served stylesheet url: $url" >&2 | ||
| 199 | + curl -fsS "http://${listen}${url}" | ||
| 200 | + REMOTE | ||
| 201 | + staged=$(sha256sum stage/assets/main-*.css | cut -d' ' -f1) | ||
| 202 | + served=$(sha256sum served-stylesheet.css | cut -d' ' -f1) | ||
| 203 | + echo "staged sha256:$staged, served sha256:$served" | ||
| 204 | + test "$staged" = "$served" | ||
| 205 | + | ||
| 206 | + - name: Cleaning | ||
| 207 | + run: rm -rf ~/.ssh | ||