aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/preview.yml
blob: 1d2fe4321b3e6cd01a038976099b8e275327607f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
name: Preview

on:
  workflow_dispatch:
    inputs:
      commit:
        description: Optional master commit SHA to publish
        required: false
        type: string
  schedule:
    - cron: "0 3 * * 3,5"

permissions:
  contents: read

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

concurrency:
  group: preview-publish
  cancel-in-progress: false

jobs:
  preflight:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      should_publish: ${{ steps.plan.outputs.should_publish }}
      commit: ${{ steps.plan.outputs.commit }}
      short_sha: ${{ steps.plan.outputs.short_sha }}
      build_id: ${{ steps.plan.outputs.build_id }}
      tag: ${{ steps.plan.outputs.tag }}
      built_at: ${{ steps.plan.outputs.built_at }}
      base_version: ${{ steps.plan.outputs.base_version }}
      protocol: ${{ steps.plan.outputs.protocol }}
    steps:
      - uses: actions/checkout@v6
        with:
          ref: master
          fetch-depth: 0
          persist-credentials: false

      - name: Select preview commit
        id: plan
        shell: bash
        run: |
          set -euo pipefail
          git fetch origin master --tags
          requested="${{ github.event.inputs.commit || '' }}"
          if [ -n "$requested" ]; then
            commit="$(git rev-parse "$requested^{commit}")"
            if ! git merge-base --is-ancestor "$commit" origin/master; then
              echo "error: requested commit $commit is not reachable from origin/master" >&2
              exit 1
            fi
          else
            commit="$(python3 scripts/preview.py select-commit --ref origin/master)"
          fi
          current_preview="$(python3 scripts/preview.py current-commit --manifest website/preview.json || true)"
          if [ "$current_preview" = "$commit" ]; then
            echo "Preview already points at $commit; skipping."
            echo "should_publish=false" >> "$GITHUB_OUTPUT"
            exit 0
          fi
          git checkout --detach "$commit"
          short_sha="$(git rev-parse --short=12 HEAD)"
          day="$(git show -s --format=%cs HEAD)"
          built_at="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
          build_id="$day-$short_sha"
          tag="preview-$build_id"
          base_version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)"
          protocol="$(python3 -c 'import re; print(re.search(r"pub const PROTOCOL_VERSION: u32 = (\d+);", open("src/protocol/wire.rs").read()).group(1))')"
          {
            echo "should_publish=true"
            echo "commit=$commit"
            echo "short_sha=$short_sha"
            echo "build_id=$build_id"
            echo "tag=$tag"
            echo "built_at=$built_at"
            echo "base_version=$base_version"
            echo "protocol=$protocol"
          } >> "$GITHUB_OUTPUT"

      - name: Install Rust
        if: steps.plan.outputs.should_publish == 'true'
        uses: dtolnay/rust-toolchain@stable

      - name: Install Rust tools
        if: steps.plan.outputs.should_publish == 'true'
        uses: taiki-e/install-action@b550161ef8a7bc4f2a671c0b03a18ac9ccedea1e # v2
        with:
          tool: just,cargo-nextest

      - name: Install Zig
        if: steps.plan.outputs.should_publish == 'true'
        uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2.2.1
        with:
          version: 0.15.2

      - name: Restore cargo cache
        if: steps.plan.outputs.should_publish == 'true'
        uses: Swatinem/rust-cache@v2
        with:
          cache-bin: false

      - name: Run checks
        if: steps.plan.outputs.should_publish == 'true'
        run: just check

  build:
    needs: preflight
    if: needs.preflight.outputs.should_publish == 'true'
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            name: herdr-linux-x86_64
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            name: herdr-linux-aarch64
          - target: x86_64-apple-darwin
            os: macos-latest
            name: herdr-macos-x86_64
          - target: aarch64-apple-darwin
            os: macos-latest
            name: herdr-macos-aarch64
    runs-on: ${{ matrix.os }}
    env:
      LIBGHOSTTY_VT_OPTIMIZE: ReleaseFast
      LIBGHOSTTY_VT_SIMD: 'true'
      HERDR_BUILD_CHANNEL: preview
      HERDR_BUILD_ID: ${{ needs.preflight.outputs.build_id }}
      HERDR_BUILD_COMMIT: ${{ needs.preflight.outputs.commit }}
    steps:
      - uses: actions/checkout@v6
        with:
          ref: ${{ needs.preflight.outputs.commit }}
          persist-credentials: false

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install Zig
        uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2.2.1
        with:
          version: 0.15.2

      - name: Prefer official Ubuntu mirrors over Azure
        if: runner.os == 'Linux'
        run: |
          if [ -f /etc/apt/apt-mirrors.txt ]; then
            sudo sed -i '/azure.archive.ubuntu.com/d' /etc/apt/apt-mirrors.txt
            cat /etc/apt/apt-mirrors.txt
          fi

      - name: Install Linux build tools
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y cmake ninja-build musl-tools gcc-aarch64-linux-gnu crossbuild-essential-arm64

      - name: Install macOS build tools
        if: runner.os == 'macOS'
        run: brew install cmake ninja

      - name: Set Linux aarch64 linker
        if: matrix.target == 'aarch64-unknown-linux-musl'
        run: echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV

      - name: Cache Rust artifacts
        uses: Swatinem/rust-cache@v2
        with:
          key: preview-${{ matrix.target }}

      - name: Remove Zig caches
        run: rm -rf .zig-cache vendor/libghostty-vt/.zig-cache vendor/libghostty-vt/zig-out

      - name: Build
        run: cargo build --release --locked --target ${{ matrix.target }}

      - name: Package artifact
        shell: bash
        run: |
          set -euo pipefail
          cp target/${{ matrix.target }}/release/herdr ${{ matrix.name }}
          if [ "${{ runner.os }}" = "Linux" ]; then
            file ${{ matrix.name }} > BUILD_INFO.txt
            ldd ${{ matrix.name }} > LDD_INFO.txt 2>&1 || true
            cat LDD_INFO.txt >> BUILD_INFO.txt
            grep -Eq "statically linked|not a dynamic executable" LDD_INFO.txt
            if nm -u ${{ matrix.name }} 2>/dev/null | grep -E '(__cxa|GLIBCXX|CXXABI|_ZSt)'; then
              echo "error: Linux artifact has unresolved C++ runtime symbols" >&2
              exit 1
            fi
          else
            file ${{ matrix.name }} > BUILD_INFO.txt
          fi
          python3 - <<'PY' | tee ${{ matrix.name }}.sha256
          import hashlib, pathlib
          path = pathlib.Path('${{ matrix.name }}')
          print(f"{hashlib.sha256(path.read_bytes()).hexdigest()}  {path.name}")
          PY
          {
            echo "commit=${{ needs.preflight.outputs.commit }}"
            echo "build_id=${{ needs.preflight.outputs.build_id }}"
            echo "target=${{ matrix.target }}"
            echo "channel=preview"
          } >> BUILD_INFO.txt

      - name: Upload artifact
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.name }}
          path: |
            ${{ matrix.name }}
            ${{ matrix.name }}.sha256
            BUILD_INFO.txt

  publish:
    needs: [preflight, build]
    if: needs.preflight.outputs.should_publish == 'true'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
        with:
          ref: master
          fetch-depth: 0
          ssh-key: ${{ secrets.PREVIEW_DEPLOY_KEY }}

      - name: Verify built commit is still current master
        run: |
          set -euo pipefail
          git fetch origin master --tags
          remote_head="$(git rev-parse origin/master)"
          if [ "$remote_head" != "${{ needs.preflight.outputs.commit }}" ]; then
            echo "error: origin/master moved from built commit; aborting preview publish" >&2
            exit 1
          fi

      - name: Download all artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts

      - name: Generate notes and checksums
        shell: bash
        run: |
          set -euo pipefail
          python3 scripts/preview.py notes \
            --commit '${{ needs.preflight.outputs.commit }}' \
            --build-id '${{ needs.preflight.outputs.build_id }}' \
            --base-version '${{ needs.preflight.outputs.base_version }}' \
            --output PREVIEW_NOTES.md
          python3 - <<'PY'
          import json, pathlib
          result = {}
          for path in pathlib.Path('artifacts').glob('herdr-*/*.sha256'):
              digest, name = path.read_text().split()[:2]
              target = name.removeprefix('herdr-')
              result[target] = digest
          pathlib.Path('preview-sha256.json').write_text(json.dumps(result, indent=2) + '\n')
          PY

      - name: Create preview prerelease
        uses: softprops/action-gh-release@v3
        with:
          tag_name: ${{ needs.preflight.outputs.tag }}
          name: Preview build ${{ needs.preflight.outputs.build_id }}
          body_path: PREVIEW_NOTES.md
          prerelease: true
          make_latest: false
          overwrite_files: true
          target_commitish: ${{ needs.preflight.outputs.commit }}
          files: |
            artifacts/herdr-linux-x86_64/herdr-linux-x86_64
            artifacts/herdr-linux-aarch64/herdr-linux-aarch64
            artifacts/herdr-macos-x86_64/herdr-macos-x86_64
            artifacts/herdr-macos-aarch64/herdr-macos-aarch64

      - name: Update preview manifest
        run: |
          python3 scripts/preview.py manifest \
            --output website/preview.json \
            --tag '${{ needs.preflight.outputs.tag }}' \
            --build-id '${{ needs.preflight.outputs.build_id }}' \
            --commit '${{ needs.preflight.outputs.commit }}' \
            --built-at '${{ needs.preflight.outputs.built_at }}' \
            --base-version '${{ needs.preflight.outputs.base_version }}' \
            --protocol '${{ needs.preflight.outputs.protocol }}' \
            --notes PREVIEW_NOTES.md \
            --sha-file preview-sha256.json \
            --retain 30

      - name: Commit preview manifest
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add website/preview.json
          git diff --cached --quiet || git commit -m "docs: update preview manifest"
          git push origin HEAD:master

      - name: Prune old preview prereleases
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail
          gh release list --repo "$GITHUB_REPOSITORY" --limit 100 --json tagName,isPrerelease,createdAt > preview-releases.json
          python3 - <<'PY' > old-preview-tags.txt
          import json
          with open("preview-releases.json", encoding="utf-8") as handle:
              data = json.load(handle)
          releases = [
              release for release in data
              if release.get("isPrerelease") and str(release.get("tagName", "")).startswith("preview-")
          ]
          releases.sort(key=lambda release: str(release.get("createdAt", "")), reverse=True)
          for release in releases[30:]:
              print(release["tagName"])
          PY
          while IFS= read -r tag; do
            [ -n "$tag" ] || continue
            gh release delete "$tag" --repo "$GITHUB_REPOSITORY" --yes --cleanup-tag
          done < old-preview-tags.txt