forge
Explore
Log in

Don't you dare go Hollow

im_old

Clone
★ 0

0 watching0 forksRustGPL-3.0755.6 KiB84318dcTahinli2 months ago77 commits

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