Skip to content

Commit 616d734

Browse files
zzgbothizzgdev
andauthored
ci: split into two jobs and embed the render preview (#16)
* ci: fold the preview into the main workflow Two workflow files, two builds, two browsers, and two places to look when something breaks. The preview was not decoration either — the tests prove the geometry is computed right and cannot show whether the result looks like anything a person would want to read. That needs an image, so it belongs in the same pipeline as the assertions and fails the same way. One file now, one job: format → types → coverage → build → UI tests → render → post Branches to master skip the last three: a push to master has no pull request to comment into. The job keeps a single checkout, install and build instead of two, and the timeout goes from 10 to 15 minutes to leave room for the browser the preview now needs after the UI suite has already started one. Permissions are declared at job level, which means the earlier steps inherit them. That is a real trade-off — narrowest scope would be a separate job — but it costs a second checkout/install/build and this repository's steps are all its own code. render-preview.yml is deleted; its history stays queryable in the Actions tab. * ci: upload the preview unzipped, and drop the browser probe The artifact link in the comment is the only way to look at the render when inline embedding is unavailable, and until now clicking it downloaded a zip containing one PNG. GitHub added unzipped artifacts in February: `actions/upload-artifact` v7 with `archive: false` stores the file as-is, so a browser that can display the type opens it directly. That turns the link from "download, unzip, look" into "look". scripts/probe-browser.ts is deleted. It existed to prove a browser was available, that text could be measured, and that screenshots were deterministic — all three are now exercised on every run by the UI suite and the preview script. What it left behind was a fourth copy of the Chromium launch flags, and a stale copy at that: the missing `--user-data-dir` was fixed in the preview script while this one sat unchanged, which is exactly the failure mode that copying causes. * ci: the accidental /zip endpoint returns the PNG now `gh run download` fails on non-zipped artifacts with 'zip: not a valid zip file' — the CLI still assumes every artifact is an archive (cli/cli#13012). `gh api .../artifacts/<id>/zip` returns the raw body, which for an unzipped artifact is the PNG. Verified by downloading this run's artifact and checking the file type. * ci: embed the preview, don't link it The comment has been linking to a file the reader had to fetch. Now it shows the picture. The URL had to be one GitHub serves as an image, and that ruled out everything tried so far: - artifacts need auth, and their URLs redirect to HTML - release assets come back as `application/octet-stream` with `Content-Disposition: attachment`, so they are treated as a download - `gh --attach` needs the web upload endpoint, which an API token cannot call A file on a branch is served by raw.githubusercontent.com as `image/png`. That is the only form that renders, and it is what reg-actions and awesome-copilot both document. The image is committed to a dedicated `render-preview` branch — force pushed, one file, no history — from a scratch directory so the checked-out tree is never touched. `?v=<short sha>` busts the CDN cache, otherwise a re-render would show the previous image for a few minutes. This also removes the attach-and-fall-back dance entirely: there is no longer any path where the comment posts without an image. The artifact upload stays, as a 30-day copy that does not depend on the branch. * chore: trigger a render to measure branch growth * ci: split into two jobs, tests and browser work One job was wrong. The browser-only work forced `contents: write` onto every step, including the ones that only read code and run assertions. Two jobs now, each asking for what it uses: checks format -> types -> test:coverage -> build (contents: read) ui build -> test:ui -> render -> publish -> comment (contents: write) The second builds its own dist/ rather than uploading and downloading it between jobs — a round trip through an artifact is slower than the three seconds the build takes. Publishing also skips itself when the render is byte-identical to what the branch already holds. Rendering is deterministic, so documentation-only pushes now leave no dangling blob behind. * ci(preview): say what the reader should actually do with the image The comment explained what the pipeline was rather than what to look at. It now says the image comes from the latest code on the branch, names the specific failures worth scanning for, and links to the run that produced it. * ci(preview): name the image after its commit One shared `preview.png` meant every run overwrote the file that every earlier comment pointed at. Open an old pull request and its screenshot had silently become a picture of newer code. The filename carries the full sha now, so a comment and the commit it describes stay together. That also removes the `?v=` cache-buster — a new name is a new URL. The branch grows by one file per commit instead of staying at one, which is the cost of not rewriting history. Worth watching; if it becomes noise, the old approach of pruning to the last N is a small change. * ci(preview): force-push the preview branch again Keeping every preview meant keeping a commit per push, and a preview is useful while the pull request is open and not much after. The old force-push behaviour is right; the per-commit filename is what was actually needed. The branch now holds one commit with one file, named after the commit it renders. An old pull request whose commit has been garbage-collected shows a broken image — clearly gone, rather than silently swapped for a newer picture, which is what a shared filename did. Also drops the clone-and-compare step: force-pushing a fresh repo is both simpler and correct, since nothing is being appended to. * ci(preview): warn that the image is not kept forever The branch is force-pushed, so a preview survives until its commit is garbage-collected. Say so where the image is, rather than leaving somebody to discover it as a broken link months later. * ci: collapse the duplicated sha variable and note the double build COMMIT_SHA and FULL_SHA were both `github.event.pull_request.head.sha`. One variable now, used whole for the filename and sliced for the prose. The build in `checks` is intentional — the `ui` job rebuilds rather than depending on this one and downloading dist/ through an artifact. The note was only in the other job, where a reader looking at `checks` would not see it, so it reads like duplication worth "fixing". --------- Co-authored-by: hizzgdev(bot) <[email protected]>
1 parent 6ec3197 commit 616d734

3 files changed

Lines changed: 178 additions & 424 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 178 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ concurrency:
1515
cancel-in-progress: true
1616

1717
jobs:
18+
# ---------------------------------------------------------------------------
19+
# Everything that does not need a browser.
20+
# ---------------------------------------------------------------------------
1821
checks:
1922
name: Format, types, tests, build
2023
runs-on: ubuntu-latest
2124
timeout-minutes: 10
2225

26+
# Read-only on purpose: nothing here writes to the repository, so nothing
27+
# here should be able to.
28+
permissions:
29+
contents: read
30+
2331
steps:
2432
# v7 runs on node24; v4 ran on node20, which GitHub deprecated.
2533
- name: Checkout
@@ -42,26 +50,184 @@ jobs:
4250
- name: Type check
4351
run: npm run type-check
4452

45-
# Coverage runs here only. scripts.ci / ci2 and the pre-push hook stay on
46-
# test-lite / test on purpose: they are the manual and local paths, where
47-
# the full coverage report is slow and noisy. Thresholds live in
48-
# package.json (scripts.test:coverage).
53+
# Unit and data-layer suites only. The UI tests live in the job below,
54+
# because they drive a real browser against the built bundle.
4955
#
50-
# This runs the unit and data-layer suites only. The UI tests are a
51-
# separate command further down, because they need the built bundle.
56+
# Coverage runs here and nowhere else. scripts.ci / ci2 and the pre-push
57+
# hook stay on test-lite / test on purpose: they are the manual and local
58+
# paths, where the full coverage report is slow and noisy. Thresholds live
59+
# in package.json (scripts.test:coverage).
5260
- name: Tests with coverage
5361
run: npm run test:coverage
5462

63+
# The ui job builds again on its own rather than depending on this one and
64+
# downloading dist/ as an artifact. That round trip costs more than the
65+
# roughly three seconds a build takes — see the note there before adding
66+
# a `needs:` between the jobs.
67+
- name: Build
68+
run: npm run build
69+
70+
# ---------------------------------------------------------------------------
71+
# Everything that needs a browser, in the order it needs them:
72+
# build -> UI tests -> render.
73+
# ---------------------------------------------------------------------------
74+
ui:
75+
name: Build, UI tests, render preview
76+
runs-on: ubuntu-latest
77+
# Generous: the browser starts twice here (UI suite, then the preview
78+
# render), and a stalled start used to eat the whole budget silently.
79+
timeout-minutes: 15
80+
81+
# The preview publishes an image to a branch and comments on the PR. Both
82+
# are writes; the checks job above deliberately does not get them.
83+
permissions:
84+
contents: write
85+
pull-requests: write
86+
87+
steps:
88+
- name: Checkout
89+
uses: actions/checkout@v7
90+
91+
- name: Setup Node
92+
uses: actions/setup-node@v7
93+
with:
94+
node-version: '24'
95+
cache: npm
96+
97+
- name: Install dependencies
98+
run: npm ci
99+
100+
# Separate job, so it needs its own build. Passing dist/ between jobs
101+
# would mean an upload and a download - slower than the ~3 seconds this
102+
# takes.
55103
- name: Build
56104
run: npm run build
57105

58-
# Last, and separate, on purpose: these drive a real browser against
59-
# dist/jsmind.js rather than the TypeScript sources, so they need the build
60-
# above. Kept out of test / test:coverage so that suite stays fast and
61-
# needs no browser, and so the manual packages (`npm run ci`) keep their
62-
# existing order.
106+
# These drive a real browser against dist/jsmind.js rather than the
107+
# TypeScript sources, which is why they need the build above. Kept out of
108+
# test / test:coverage so that suite stays fast and needs no browser, and
109+
# so the manual packages (`npm run ci`) keep their order.
63110
- name: UI tests
64111
# A stalled browser start used to consume the whole job timeout; fail
65112
# this step on its own budget instead so the cause is obvious.
66113
timeout-minutes: 3
67-
run: npm run test:ui
114+
run: npm run test:ui
115+
116+
# -----------------------------------------------------------------------
117+
# Render preview
118+
#
119+
# Not a nicety. The tests prove the geometry is computed correctly and
120+
# cannot show whether the result looks like anything - that takes an
121+
# image someone can open. So it runs after the assertions, and a failure
122+
# here is a failure like any other.
123+
#
124+
# Pull requests only: a push to master has no comment to post into.
125+
# -----------------------------------------------------------------------
126+
127+
- name: Render the example map
128+
if: github.event_name == 'pull_request'
129+
run: |
130+
node --experimental-strip-types --no-warnings scripts/render-preview.ts \
131+
examples/v2-features.json preview.png
132+
133+
# The comment embeds this image with markdown, which needs a URL GitHub
134+
# serves as an image. Neither artifacts nor release assets qualify:
135+
# artifact URLs need auth and redirect to HTML, and release download URLs
136+
# arrive as `application/octet-stream` with `Content-Disposition:
137+
# attachment`, so GitHub treats them as a file to fetch rather than a
138+
# picture to draw. A file on a branch is served by
139+
# raw.githubusercontent.com as `image/png` - the one form that works, and
140+
# what reg-actions and awesome-copilot both document.
141+
#
142+
# Force-pushed so the branch always holds exactly one commit and one file;
143+
# done in a scratch directory so the checked-out tree is never disturbed.
144+
- name: Publish the image to the preview branch
145+
id: publish
146+
if: github.event_name == 'pull_request'
147+
env:
148+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
149+
REPO: ${{ github.repository }}
150+
SHA: ${{ github.event.pull_request.head.sha }}
151+
run: |
152+
set -euo pipefail
153+
154+
# The filename carries the commit, so a comment and the code it
155+
# describes stay together. A single shared `preview.png` would be
156+
# overwritten by every later run and quietly rewrite history in every
157+
# earlier comment.
158+
file="preview-${SHA}.png"
159+
160+
# Force-pushed: the branch holds exactly one commit and one file, so
161+
# it never accumulates. The cost is that an older preview eventually
162+
# stops resolving once the superseded commit is garbage-collected —
163+
# an old pull request shows a broken image rather than, worse, a
164+
# picture of somebody else's code. A preview is worth a glance at
165+
# review time and not much after that.
166+
#
167+
# Built in a scratch directory so the checked-out tree is untouched.
168+
rm -rf /tmp/preview-branch && mkdir -p /tmp/preview-branch
169+
cp preview.png "/tmp/preview-branch/$file"
170+
cd /tmp/preview-branch
171+
172+
git init -q
173+
git config user.name "github-actions[bot]"
174+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
175+
git add "$file"
176+
git commit -q -m "preview: ${SHA:0:7}"
177+
git push -q -f \
178+
"https://x-access-token:${GH_TOKEN}@github.com/${REPO}.git" \
179+
HEAD:render-preview
180+
echo "published ${SHA:0:7} as $file"
181+
182+
- name: Post it on the pull request
183+
if: github.event_name == 'pull_request'
184+
env:
185+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
186+
PR_NUMBER: ${{ github.event.pull_request.number }}
187+
# One variable, two uses: the full sha names the file, the first seven
188+
# characters go in the prose.
189+
FULL_SHA: ${{ github.event.pull_request.head.sha }}
190+
RUN_ID: ${{ github.run_id }}
191+
REPO: ${{ github.repository }}
192+
run: |
193+
set -euo pipefail
194+
short="${FULL_SHA:0:7}"
195+
196+
# Invisible marker so a re-run can find and replace this comment.
197+
# Without it every push would leave its own screenshot behind and the
198+
# pull request would collect a column of images, all but the last
199+
# describing code that no longer exists.
200+
MARKER='<!-- jsmind-render-preview -->'
201+
202+
# Unquoted heredoc delimiter so $short interpolates; backticks are
203+
# escaped so the shell does not run them as commands.
204+
cat > /tmp/body.md <<BODY
205+
$MARKER
206+
### Render preview
207+
208+
![jsMind render preview](https://raw.githubusercontent.com/$REPO/render-preview/preview-$FULL_SHA.png)
209+
210+
Rendered from the latest code on this branch, at \`$short\`.
211+
212+
**Worth a look for anything odd** - overlapping nodes, missing edges, text
213+
spilling outside its box, a lopsided layout. The tests check that the
214+
geometry is computed correctly; whether it actually *looks* right is the one
215+
thing they cannot tell you, and the one thing this image is for.
216+
217+
[Workflow run](https://github.com/$REPO/actions/runs/$RUN_ID)
218+
<sub>Generated by \`scripts/render-preview.ts\` from \`examples/v2-features.json\`.
219+
The filename is the commit, so this image always matches the code above it.
220+
**This file is cleaned up periodically — download it if you need to keep it.**</sub>
221+
BODY
222+
223+
# Clear any previous preview first. Deleting before posting means a
224+
# failure leaves the old image in place instead of no image at all.
225+
previous=$(gh api "repos/$REPO/issues/$PR_NUMBER/comments" \
226+
--jq ".[] | select(.body | contains(\"$MARKER\")) | .id" || true)
227+
for id in $previous; do
228+
echo "removing previous preview comment $id"
229+
gh api -X DELETE "repos/$REPO/issues/comments/$id" || true
230+
done
231+
232+
gh pr comment "$PR_NUMBER" --repo "$REPO" --body-file /tmp/body.md
233+
echo "posted"

‎.github/workflows/render-preview.yml‎

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)