forge
Explore
Log in

Don't you dare go Hollow

im_old

Clone
★ 0

0 watching0 forksRustGPL-3.0755.6 KiB08994ccTahinli2 months ago70 commits

main.yml237 lines, 10.7 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.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 # 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. im-migrate rides along for the
96 # one-time İzlek import (run by hand on the server).
97 topcoat asset bundle --package im-web --profile "$PROFILE" --out stage/assets
98 cargo build --package im-migrate --profile "$PROFILE"
99 cp "target/$TARGET/$PROFILE/im-web" stage/im-web
100 cp deploy/im.service stage/im.service
101 cp "target/$TARGET/$PROFILE/im-migrate" stage/im-migrate
102 strip stage/im-web stage/im-migrate
103 ls -l stage stage/assets
104
105 - name: Install SSH keys
106 run: |
107 mkdir -p ~/.ssh
108 echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
109 ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
110 - name: Upload the release
111 run: |
112 set -euo pipefail
113 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
114
115 # The server's login shell is not bash, and ssh would hand this
116 # script to it; `bash -s` takes it on stdin instead, so the login
117 # shell only ever parses one plain command.
118 #
119 # The deploy path is $DEPLOY_SUBDIR under the SSH user's home,
120 # resolved on the server as $HOME: there is no path secret to go
121 # stale, and switching the deploy user moves the deploy with it.
122 ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" <<'REMOTE'
123 set -euo pipefail
124 case "$1" in
125 ''|/*|*..*)
126 echo "Refusing a broad deploy subdirectory: '$1'" >&2
127 exit 1
128 ;;
129 esac
130 deploy_path=$HOME/$1
131 # The deploy path is the service's working directory: config/,
132 # im.db and im.key live there and are never touched from here.
133 # Only 'current' is replaced.
134 mkdir -p -- "$deploy_path"
135 rm -rf -- "$deploy_path/current.new"
136 mkdir -p -- "$deploy_path/current.new"
137 REMOTE
138 # stdin carries the tarball, so the script cannot; the extraction
139 # still goes through bash -c because the login shell is fish and
140 # would not expand $HOME.
141 tar -C stage -czf - . | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1/current.new\"' -- '$DEPLOY_SUBDIR'"
142
143 - name: Swap and restart
144 run: |
145 set -euo pipefail
146 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
147
148 # bash -s for the same reason as above: the login shell is not
149 # bash and must never see this script.
150 ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE' '$GITHUB_SHA'" <<'REMOTE'
151 set -euo pipefail
152 deploy_path=$HOME/$1
153 service=$2
154 expected="ok $3"
155 export XDG_RUNTIME_DIR=/run/user/$(id -u)
156 # Read before anything moves: a config the health check cannot
157 # read is a failure to stop on, not one to roll back from.
158 listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$deploy_path/config/im.toml")
159 if [ -z "$listen" ]; then
160 echo "no listen address in $deploy_path/config/im.toml" >&2
161 exit 1
162 fi
163 # The config directory carries secrets: the toml and any keys
164 # files are held to 0600, and the editor's stray secret-bearing
165 # backups go away — every deploy, the same way.
166 chmod 0600 -- "$deploy_path/config/"*.toml 2>/dev/null || true
167 chmod 0600 -- "$deploy_path/config/"*.keys 2>/dev/null || true
168 find "$deploy_path/config" -maxdepth 1 -type f \( -name '*.bak*' -o -name '*.pre-*' \) -delete
169 chmod +x -- "$deploy_path/current.new/im-web"
170 rm -rf -- "$deploy_path/current.previous"
171 if [ -e "$deploy_path/current" ]; then
172 mv -- "$deploy_path/current" "$deploy_path/current.previous"
173 fi
174 mv -- "$deploy_path/current.new" "$deploy_path/current"
175 # The unit ships with the release: a freshly provisioned box has
176 # no unit until the first deploy writes it, and a changed unit
177 # lands with the code that needs it. Enabling is idempotent.
178 mkdir -p -- "$HOME/.config/systemd/user"
179 install -m 644 -- "$deploy_path/current/im.service" "$HOME/.config/systemd/user/$service.service"
180 systemctl --user daemon-reload
181 systemctl --user enable -- "$service"
182 systemctl --user restart "$service"
183 # The unit is Type=simple, so a restart returns before the new
184 # process has bound its port — and before an old one has let go
185 # of it. Wait for the health route rather than a clock, and
186 # check the answer names this commit: a stale process can still
187 # answer, and did once on a sibling deploy.
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 'im is up'
201 rm -rf -- "$deploy_path/current.previous"
202 exit 0
203 done
204 echo 'im did not answer /healthz; rolling back' >&2
205 rollback
206 exit 1
207 REMOTE
208
209 # The independent after-the-fact check: the page's stylesheet URL must
210 # answer with bytes byte-identical to the bundle this deploy staged.
211 - name: Verify the served stylesheet is this deploy's
212 run: |
213 set -euo pipefail
214 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
215 # One remote script: mixing runner-side and server-side variables in
216 # nested ssh quoting once shipped a check that failed on a healthy
217 # deploy. Everything server-side happens inside the heredoc; only the
218 # stylesheet bytes come back on stdout.
219 ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" > served-stylesheet.css <<'REMOTE'
220 set -euo pipefail
221 deploy_path=$HOME/$1
222 listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$deploy_path/config/im.toml")
223 url=$(curl -fsS "http://${listen}/" | grep -oE '/_topcoat/assets/main-[a-f0-9]+\.css' | head -n1)
224 if [ -z "$url" ]; then
225 echo 'the served page references no stylesheet' >&2
226 exit 1
227 fi
228 echo "served stylesheet url: $url" >&2
229 curl -fsS "http://${listen}${url}"
230 REMOTE
231 staged=$(sha256sum stage/assets/main-*.css | cut -d' ' -f1)
232 served=$(sha256sum served-stylesheet.css | cut -d' ' -f1)
233 echo "staged sha256:$staged, served sha256:$served"
234 test "$staged" = "$served"
235
236 - name: Cleaning
237 run: rm -rf ~/.ssh