✨ build in GitHub Actions, ship static files to server
Browse files at this commitceb2f6fec3963f8276b150db82eec1a68c3a0238Parent: df1b8c0- feat
- ✨ build in GitHub Actions, ship static files to server
2 changed files +173 -6
- .github/workflows/deploy.yaml +168 -5
- Cargo.toml +5 -1
.github/workflows/deploy.yamlmodified+168 -5
| @@ -1,20 +1,183 @@ | |||
| 1 | +name: Deploy | ||
| 2 | + | ||
| 1 | on: | 3 | on: |
| 2 | push: | 4 | push: |
| 3 | branches: | 5 | branches: |
| 4 | - main | 6 | - main |
| 5 | 7 | ||
| 8 | +# The deploy swaps the served directory; two of them racing would leave the | ||
| 9 | +# server with pieces of different builds. | ||
| 10 | +concurrency: | ||
| 11 | + group: deploy | ||
| 12 | + cancel-in-progress: false | ||
| 13 | + | ||
| 14 | +env: | ||
| 15 | + # The dx CLI is compiled once per version and cached on that version; bump | ||
| 16 | + # here to move the site to a newer toolchain, nowhere on the server. | ||
| 17 | + DX: 0.7.10 | ||
| 18 | + # The site lives in this subdirectory of the SSH user's home. The home | ||
| 19 | + # itself is resolved on the server ($HOME), so switching the deploy user | ||
| 20 | + # moves the site with it and there is no path secret to go stale. The web | ||
| 21 | + # server's root must point at $HOME/personality. | ||
| 22 | + SITE_DIR: personality | ||
| 23 | + # Where the deployed site is checked after the swap. Set the repository | ||
| 24 | + # variable SITE_URL to override. | ||
| 25 | + SITE_URL: ${{ vars.SITE_URL || 'https://tahinli.com' }} | ||
| 26 | + | ||
| 6 | jobs: | 27 | jobs: |
| 7 | - pull: | 28 | + deploy: |
| 8 | name: Deploy | 29 | name: Deploy |
| 9 | - runs-on: ubuntu-latest | 30 | + # Pinned, not `latest`: the output is wasm and platform-independent, but a |
| 31 | + # runner image moving under the cache silently discards it. | ||
| 32 | + runs-on: ubuntu-24.04 | ||
| 10 | steps: | 33 | steps: |
| 11 | - - name: Install SSH Keys | 34 | + - name: Checkout repository |
| 35 | + uses: actions/checkout@v4 | ||
| 36 | + | ||
| 37 | + - name: Set up Rust | ||
| 38 | + run: rustup toolchain install stable --profile minimal --target wasm32-unknown-unknown | ||
| 39 | + | ||
| 40 | + - name: Cache cargo | ||
| 41 | + uses: Swatinem/rust-cache@v2 | ||
| 42 | + with: | ||
| 43 | + cache-all-crates: true | ||
| 44 | + # Bump when the target, the profile or any rustflags change: the key | ||
| 45 | + # does not otherwise see those, and a run restoring the old key | ||
| 46 | + # reports it up-to-date at save time, discarding the recompiled | ||
| 47 | + # dependencies. | ||
| 48 | + prefix-key: v1-rust-wasm | ||
| 49 | + | ||
| 50 | + # Compiling the CLI from source costs many minutes and it changes only | ||
| 51 | + # when the pinned version does, so it is cached on that version alone. | ||
| 52 | + - name: Cache the dx CLI | ||
| 53 | + id: dx-cache | ||
| 54 | + uses: actions/cache@v4 | ||
| 55 | + with: | ||
| 56 | + path: ~/.cargo/bin/dx | ||
| 57 | + key: dx-cli-${{ env.DX }}-${{ runner.os }} | ||
| 58 | + | ||
| 59 | + - name: Install cargo-binstall | ||
| 60 | + if: steps.dx-cache.outputs.cache-hit != 'true' | ||
| 61 | + uses: cargo-bins/cargo-binstall@main | ||
| 62 | + | ||
| 63 | + - name: Install the dx CLI | ||
| 64 | + # binstall fetches the prebuilt release binary and only compiles from | ||
| 65 | + # source when there is none for this version. | ||
| 66 | + if: steps.dx-cache.outputs.cache-hit != 'true' | ||
| 67 | + run: cargo binstall dioxus-cli@${{ env.DX }} --no-confirm --locked | ||
| 68 | + | ||
| 69 | + - name: Build the site | ||
| 70 | + run: | | ||
| 71 | + set -euo pipefail | ||
| 72 | + # The public dir survives in the restored cache and keeps every | ||
| 73 | + # earlier build's hashed assets; only this build's may ship. | ||
| 74 | + rm -rf "target/dx/$SITE_DIR" | ||
| 75 | + # wasm-opt needs the debuginfo stripped first (wasm-release profile | ||
| 76 | + # in Cargo.toml), otherwise it aborts and dx ships the fat wasm. | ||
| 77 | + dx build --release --platform web | ||
| 78 | + public="target/dx/$SITE_DIR/release/web/public" | ||
| 79 | + test -f "$public/index.html" | ||
| 80 | + rm -rf stage | ||
| 81 | + cp -r "$public" stage | ||
| 82 | + ls -lR stage | ||
| 83 | + | ||
| 84 | + - name: Install SSH keys | ||
| 12 | run: | | 85 | run: | |
| 13 | mkdir -p ~/.ssh | 86 | mkdir -p ~/.ssh |
| 14 | echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa | 87 | echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa |
| 15 | ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts | 88 | ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts |
| 16 | - - name: Connect and Pull | 89 | + |
| 17 | - run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "export PATH="${{ secrets.CARGO_PATH }}:$PATH" && cd ${{ secrets.WORK_DIR }} && git checkout ${{ secrets.MAIN_BRANCH }} && git pull --rebase && dx build --release && cp -r ${{ secrets.ARTIFACT_DIR }} ${{ secrets.DEPLOY_PATH }} && rm -rf ${{ secrets.DEPLOY_PATH }}/personality && mv ${{ secrets.DEPLOY_PATH }}/public ${{ secrets.DEPLOY_PATH }}/personality" | 90 | + - name: Upload the release |
| 91 | + run: | | ||
| 92 | + set -euo pipefail | ||
| 93 | + ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" | ||
| 94 | + | ||
| 95 | + # The server's login shell may not be bash, and ssh would hand this | ||
| 96 | + # script to it; `bash -s` takes it on stdin instead, so the login | ||
| 97 | + # shell only ever parses one plain command. | ||
| 98 | + ssh "$ssh_target" "bash -se -- '$SITE_DIR'" <<'REMOTE' | ||
| 99 | + set -euo pipefail | ||
| 100 | + case "$1" in | ||
| 101 | + ''|/*|*..*) | ||
| 102 | + echo "Refusing a broad site subdirectory: '$1'" >&2 | ||
| 103 | + exit 1 | ||
| 104 | + ;; | ||
| 105 | + esac | ||
| 106 | + site=$HOME/$1 | ||
| 107 | + rm -rf -- "$site.new" | ||
| 108 | + mkdir -p -- "$site.new" | ||
| 109 | + REMOTE | ||
| 110 | + # stdin carries the tarball, so the script cannot; the extraction | ||
| 111 | + # still goes through bash -c because a fish login shell would not | ||
| 112 | + # expand $HOME. | ||
| 113 | + tar -C stage -czf - . | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1.new\"' -- '$SITE_DIR'" | ||
| 114 | + | ||
| 115 | + - name: Swap | ||
| 116 | + id: swap | ||
| 117 | + run: | | ||
| 118 | + set -euo pipefail | ||
| 119 | + ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" | ||
| 120 | + ssh "$ssh_target" "bash -se -- '$SITE_DIR'" <<'REMOTE' | ||
| 121 | + set -euo pipefail | ||
| 122 | + site=$HOME/$1 | ||
| 123 | + rm -rf -- "$site.previous" | ||
| 124 | + if [ -e "$site" ]; then | ||
| 125 | + mv -- "$site" "$site.previous" | ||
| 126 | + fi | ||
| 127 | + mv -- "$site.new" "$site" | ||
| 128 | + REMOTE | ||
| 129 | + | ||
| 130 | + # The independent after-the-fact check: the live page must reference | ||
| 131 | + # the script this deploy staged, and that script must answer with | ||
| 132 | + # byte-identical content. Hashed asset names cannot be served stale by | ||
| 133 | + # a cache, so a mismatch means the swap did not land. | ||
| 134 | + - name: Verify the served site is this deploy's | ||
| 135 | + run: | | ||
| 136 | + set -euo pipefail | ||
| 137 | + js=$(grep -oE 'personality-dxh[a-f0-9]+\.js' stage/index.html | head -n1) | ||
| 138 | + test -n "$js" | ||
| 139 | + test -f "stage/assets/$js" | ||
| 140 | + staged=$(sha256sum "stage/assets/$js" | cut -d' ' -f1) | ||
| 141 | + for _ in $(seq 1 30); do | ||
| 142 | + if curl -fsS -H 'Cache-Control: no-cache' "$SITE_URL/" | grep -q "$js"; then | ||
| 143 | + served=$(curl -fsS "$SITE_URL/assets/$js" | sha256sum | cut -d' ' -f1) | ||
| 144 | + echo "staged sha256:$staged, served sha256:$served" | ||
| 145 | + test "$staged" = "$served" | ||
| 146 | + echo "$SITE_DIR is up" | ||
| 147 | + exit 0 | ||
| 148 | + fi | ||
| 149 | + sleep 1 | ||
| 150 | + done | ||
| 151 | + echo "$SITE_URL does not serve $js" >&2 | ||
| 152 | + exit 1 | ||
| 153 | + | ||
| 154 | + - name: Roll back | ||
| 155 | + if: failure() && steps.swap.conclusion == 'success' | ||
| 156 | + run: | | ||
| 157 | + set -euo pipefail | ||
| 158 | + ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" | ||
| 159 | + ssh "$ssh_target" "bash -se -- '$SITE_DIR'" <<'REMOTE' | ||
| 160 | + set -euo pipefail | ||
| 161 | + site=$HOME/$1 | ||
| 162 | + if [ ! -e "$site.previous" ]; then | ||
| 163 | + echo 'nothing to roll back to' >&2 | ||
| 164 | + exit 0 | ||
| 165 | + fi | ||
| 166 | + rm -rf -- "$site.rolledback" | ||
| 167 | + mv -- "$site" "$site.rolledback" | ||
| 168 | + mv -- "$site.previous" "$site" | ||
| 169 | + rm -rf -- "$site.rolledback" | ||
| 170 | + echo 'rolled back to the previous release' | ||
| 171 | + REMOTE | ||
| 172 | + | ||
| 173 | + - name: Drop the previous release | ||
| 174 | + run: | | ||
| 175 | + set -euo pipefail | ||
| 176 | + ssh "${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" "bash -se -- '$SITE_DIR'" <<'REMOTE' | ||
| 177 | + set -euo pipefail | ||
| 178 | + rm -rf -- "$HOME/$1.previous" | ||
| 179 | + REMOTE | ||
| 18 | 180 | ||
| 19 | - name: Cleaning | 181 | - name: Cleaning |
| 182 | + if: always() | ||
| 20 | run: rm -rf ~/.ssh | 183 | run: rm -rf ~/.ssh |
Cargo.tomlmodified+5 -1
| @@ -8,7 +8,7 @@ edition = "2021" | |||
| 8 | 8 | ||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | 10 | ||
| 11 | -dioxus = { version = "0.7.3", features = ["web", "router"] } | 11 | +dioxus = { version = "0.7.10", features = ["web", "router"] } |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | [profile] | 14 | [profile] |
| @@ -22,3 +22,7 @@ inherits = "dev" | |||
| 22 | 22 | ||
| 23 | [profile.android-dev] | 23 | [profile.android-dev] |
| 24 | inherits = "dev" | 24 | inherits = "dev" |
| 25 | + | ||
| 26 | +[profile.wasm-release] | ||
| 27 | +inherits = "release" | ||
| 28 | +strip = "debuginfo" | ||