iz_old
Clone
https://git.tahinli.com/Tahinli/iz_old.git
0 watching0 forksRustGPL-3.02.2 MiB4c154b4Tahinli2 months ago301 commits
| 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 iz.service. |
| 20 | SERVICE: iz |
| 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: measured on a 12-core machine, the relink every deploy pays |
| 24 | # for goes from 2m27s to 25s, for a binary this server will never be |
| 25 | # CPU-bound on. See the profile in Cargo.toml. |
| 26 | PROFILE: dist |
| 27 | TOPCOAT: 0.9.0 |
| 28 | # The deploy lives in this subdirectory of the SSH user's home. The home |
| 29 | # itself is resolved on the server ($HOME), so switching the deploy user |
| 30 | # moves the path with it — a baked-in absolute path once kept pointing at |
| 31 | # the previous user's home and failed the deploy on permissions. |
| 32 | DEPLOY_SUBDIR: iz |
| 33 | # Baked into the binary by build.rs and asserted against after the |
| 34 | # deploy restart: a /healthz answered by a stale process from an old |
| 35 | # deploy now fails and rolls back instead of reporting green (observed |
| 36 | # on the izlek→iz rename). |
| 37 | IZ_BUILD_SHA: ${{ github.sha }} |
| 38 | |
| 39 | jobs: |
| 40 | deploy: |
| 41 | name: Deploy |
| 42 | # Pinned, not `latest`: ubuntu-latest floating to the next LTS would |
| 43 | # one day ship a glibc newer than the server's, and the gnu binary |
| 44 | # would stop running there. See the TARGET comment above. |
| 45 | runs-on: ubuntu-24.04 |
| 46 | steps: |
| 47 | - name: Checkout private repository |
| 48 | uses: actions/checkout@v4 |
| 49 | |
| 50 | - name: Set up Rust |
| 51 | run: rustup toolchain install stable --profile minimal |
| 52 | |
| 53 | - name: Cache cargo |
| 54 | uses: Swatinem/rust-cache@v2 |
| 55 | with: |
| 56 | cache-all-crates: true |
| 57 | # Bump when the target, the profile or any rustflags change: the |
| 58 | # key does not otherwise see those, and a run that restores the |
| 59 | # old key reports it up-to-date at save time — discarding the |
| 60 | # recompiled dependencies and silently recompiling them on every |
| 61 | # later run (observed when the musl→gnu move landed on this key). |
| 62 | prefix-key: v1-rust-gnu |
| 63 | |
| 64 | # Compiling the CLI from source costs minutes and it changes only when |
| 65 | # the pinned version does, so it is cached on that version alone. |
| 66 | - name: Cache the topcoat CLI |
| 67 | id: topcoat-cache |
| 68 | uses: actions/cache@v4 |
| 69 | with: |
| 70 | path: ~/.cargo/bin/topcoat |
| 71 | key: topcoat-cli-${{ env.TOPCOAT }}-${{ runner.os }} |
| 72 | |
| 73 | - name: Install the topcoat CLI |
| 74 | # Installed before the cargo config below exists, so the CLI itself |
| 75 | # is built for the host and not for the deploy target. |
| 76 | if: steps.topcoat-cache.outputs.cache-hit != 'true' |
| 77 | run: cargo install topcoat-cli@${{ env.TOPCOAT }} --locked |
| 78 | |
| 79 | - name: Point cargo at the deploy target |
| 80 | run: | |
| 81 | set -euo pipefail |
| 82 | # `topcoat asset bundle` runs its own `cargo build` and strips every |
| 83 | # CARGO_* variable from it, so the target cannot be passed by |
| 84 | # environment; it goes into the cargo config that inner build |
| 85 | # reads. This file is written in CI only; it is not committed. |
| 86 | # |
| 87 | # With the gnu target the linker is rustc's default — the bundled |
| 88 | # rust-lld — and it matters: a warm relink of this binary measured |
| 89 | # 142s under the BFD that an explicit `linker = "gcc"` would pick, |
| 90 | # against 23s under lld. That link is the largest single chunk of |
| 91 | # the deploy build, so the default is deliberately left alone. |
| 92 | mkdir -p .cargo |
| 93 | printf '[build]\ntarget = "%s"\n' "$TARGET" >> .cargo/config.toml |
| 94 | |
| 95 | - name: Build the binary and bundle its assets |
| 96 | run: | |
| 97 | set -euo pipefail |
| 98 | # One compile: this builds iz-web and writes out the asset bundle |
| 99 | # embedded in the binary it produced. |
| 100 | topcoat asset bundle --package iz-web --profile "$PROFILE" --out stage/assets |
| 101 | cp "target/$TARGET/$PROFILE/iz-web" stage/iz-web |
| 102 | cp deploy/iz.service stage/iz.service |
| 103 | strip stage/iz-web |
| 104 | ls -l stage stage/assets |
| 105 | |
| 106 | - name: Install SSH keys |
| 107 | run: | |
| 108 | mkdir -p ~/.ssh |
| 109 | echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa |
| 110 | ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts |
| 111 | |
| 112 | - name: Upload the release |
| 113 | run: | |
| 114 | set -euo pipefail |
| 115 | ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" |
| 116 | |
| 117 | # The server's login shell is not bash, and ssh would hand this |
| 118 | # script to it; `bash -s` takes it on stdin instead, so the login |
| 119 | # shell only ever parses one plain command. |
| 120 | # |
| 121 | # The deploy path is $DEPLOY_SUBDIR under the SSH user's home, |
| 122 | # resolved on the server as $HOME: there is no path secret to go |
| 123 | # stale, and switching the deploy user moves the deploy with it. |
| 124 | ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" <<'REMOTE' |
| 125 | set -euo pipefail |
| 126 | case "$1" in |
| 127 | ''|/*|*..*) |
| 128 | echo "Refusing a broad deploy subdirectory: '$1'" >&2 |
| 129 | exit 1 |
| 130 | ;; |
| 131 | esac |
| 132 | deploy_path=$HOME/$1 |
| 133 | # The deploy path is the service's working directory: config/, |
| 134 | # iz.db and iz.key live there and are never touched from here. |
| 135 | # Only 'current' is replaced. |
| 136 | mkdir -p -- "$deploy_path" |
| 137 | rm -rf -- "$deploy_path/current.new" |
| 138 | mkdir -p -- "$deploy_path/current.new" |
| 139 | REMOTE |
| 140 | # stdin carries the tarball, so the script cannot; the extraction |
| 141 | # still goes through bash -c because the login shell is fish and |
| 142 | # would not expand $HOME. |
| 143 | tar -C stage -czf - . | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1/current.new\"' -- '$DEPLOY_SUBDIR'" |
| 144 | |
| 145 | - name: Swap and restart |
| 146 | run: | |
| 147 | set -euo pipefail |
| 148 | ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" |
| 149 | |
| 150 | # bash -s for the same reason as above: the login shell is not |
| 151 | # bash and must never see this script. |
| 152 | ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE' '$GITHUB_SHA'" <<'REMOTE' |
| 153 | set -euo pipefail |
| 154 | deploy_path=$HOME/$1 |
| 155 | service=$2 |
| 156 | expected="ok $3" |
| 157 | export XDG_RUNTIME_DIR=/run/user/$(id -u) |
| 158 | # Read before anything moves: a config the health check cannot |
| 159 | # read is a failure to stop on, not one to roll back from. |
| 160 | listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$deploy_path/config/iz.toml") |
| 161 | if [ -z "$listen" ]; then |
| 162 | echo "no listen address in $deploy_path/config/iz.toml" >&2 |
| 163 | exit 1 |
| 164 | fi |
| 165 | chmod 0600 -- "$deploy_path/config/"*.toml 2>/dev/null || true |
| 166 | chmod 0600 -- "$deploy_path/config/"*.keys 2>/dev/null || true |
| 167 | find "$deploy_path/config" -maxdepth 1 -type f \( -name '*.bak*' -o -name '*.pre-*' \) -delete |
| 168 | chmod +x -- "$deploy_path/current.new/iz-web" |
| 169 | rm -rf -- "$deploy_path/current.previous" |
| 170 | if [ -e "$deploy_path/current" ]; then |
| 171 | mv -- "$deploy_path/current" "$deploy_path/current.previous" |
| 172 | fi |
| 173 | mv -- "$deploy_path/current.new" "$deploy_path/current" |
| 174 | # The unit ships with the release: a freshly provisioned box has |
| 175 | # no unit until the first deploy writes it, and a changed unit |
| 176 | # lands with the code that needs it. Enabling is idempotent. |
| 177 | mkdir -p -- "$HOME/.config/systemd/user" |
| 178 | install -m 644 -- "$deploy_path/current/iz.service" "$HOME/.config/systemd/user/$service.service" |
| 179 | systemctl --user daemon-reload |
| 180 | systemctl --user enable -- "$service" |
| 181 | systemctl --user restart "$service" |
| 182 | # The unit is Type=simple, so a restart returns before the new |
| 183 | # process has bound its port — and before an old one has let go |
| 184 | # of it. Wait for the health route rather than a clock, and |
| 185 | # check the answer names this commit: a stale process can still |
| 186 | # answer, and did once (the izlek→iz rename deployed green over |
| 187 | # a pre-rename server). |
| 188 | rollback() { |
| 189 | rm -rf -- "$deploy_path/current" |
| 190 | mv -- "$deploy_path/current.previous" "$deploy_path/current" |
| 191 | systemctl --user restart "$service" |
| 192 | } |
| 193 | for _ in $(seq 1 30); do |
| 194 | served=$(curl -fsS "http://$listen/healthz" 2>/dev/null) || { sleep 1; continue; } |
| 195 | if [ "$served" != "$expected" ]; then |
| 196 | echo "/healthz answered '$served', wanted '$expected'; rolling back" >&2 |
| 197 | rollback |
| 198 | exit 1 |
| 199 | fi |
| 200 | echo 'iz is up' |
| 201 | rm -rf -- "$deploy_path/current.previous" |
| 202 | exit 0 |
| 203 | done |
| 204 | echo 'iz did not answer /healthz; rolling back' >&2 |
| 205 | rollback |
| 206 | exit 1 |
| 207 | REMOTE |
| 208 | |
| 209 | # The boot-time fingerprint guard refuses a foreign bundle, so a |
| 210 | # mixed generation cannot get this far unnoticed — this is the |
| 211 | # independent after-the-fact check: the page's stylesheet URL must |
| 212 | # answer with bytes byte-identical to the bundle this deploy staged. |
| 213 | - name: Verify the served stylesheet is this deploy's |
| 214 | run: | |
| 215 | set -euo pipefail |
| 216 | ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" |
| 217 | # One remote script: mixing runner-side and server-side variables in |
| 218 | # nested ssh quoting once shipped a check that failed on a healthy |
| 219 | # deploy. Everything server-side happens inside the heredoc; only the |
| 220 | # stylesheet bytes come back on stdout. |
| 221 | ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" > served-stylesheet.css <<'REMOTE' |
| 222 | set -euo pipefail |
| 223 | deploy_path=$HOME/$1 |
| 224 | listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$deploy_path/config/iz.toml") |
| 225 | url=$(curl -fsS "http://${listen}/" | grep -oE '/_topcoat/assets/main-[a-f0-9]+\.css' | head -n1) |
| 226 | if [ -z "$url" ]; then |
| 227 | echo 'the served page references no stylesheet' >&2 |
| 228 | exit 1 |
| 229 | fi |
| 230 | echo "served stylesheet url: $url" >&2 |
| 231 | curl -fsS "http://${listen}${url}" |
| 232 | REMOTE |
| 233 | staged=$(sha256sum stage/assets/main-*.css | cut -d' ' -f1) |
| 234 | served=$(sha256sum served-stylesheet.css | cut -d' ' -f1) |
| 235 | echo "staged sha256:$staged, served sha256:$served" |
| 236 | test "$staged" = "$served" |
| 237 | |
| 238 | - name: Cleaning |
| 239 | run: rm -rf ~/.ssh |