@@ -14,163 +14,84 @@ concurrency:
1414 cancel-in-progress : true
1515
1616jobs :
17- build-preview :
18- runs-on : ubuntu-latest
19- outputs :
20- artifact_name : ${{ steps.meta.outputs.artifact_name }}
21- screenshot_name : ${{ steps.meta.outputs.screenshot_name }}
22- post_path : ${{ steps.changed_post.outputs.post_path }}
23- post_url : ${{ steps.changed_post.outputs.post_url }}
24- steps :
25- - name : Set artifact name
26- id : meta
27- run : |
28- echo "artifact_name=site-preview-pr-${{ github.event.number }}" >> "$GITHUB_OUTPUT"
29- echo "screenshot_name=site-screenshots-pr-${{ github.event.number }}" >> "$GITHUB_OUTPUT"
30-
31- - name : Checkout
32- uses : actions/checkout@v4
33- with :
34- fetch-depth : 0
35-
36- - name : Set up Ruby
37- uses : ruby/setup-ruby@v1
38- with :
39- bundler-cache : true
40-
41- - name : Build site
42- run : bundle exec jekyll build
43-
44- - name : Detect changed post
45- id : changed_post
46- env :
47- BASE_SHA : ${{ github.event.pull_request.base.sha }}
48- HEAD_SHA : ${{ github.sha }}
49- run : |
50- mapfile -t posts < <(git diff --name-only "$BASE_SHA" "$HEAD_SHA" -- '_posts/*.md')
51- if [[ ${#posts[@]} -eq 0 ]]; then
52- echo "post_path=" >> "$GITHUB_OUTPUT"
53- echo "post_url=/" >> "$GITHUB_OUTPUT"
54- exit 0
55- fi
56-
57- post="${posts[0]}"
58- slug="$(basename "$post" .md)"
59- slug="${slug#????-??-??-}"
60- echo "post_path=$post" >> "$GITHUB_OUTPUT"
61- echo "post_url=/$slug/" >> "$GITHUB_OUTPUT"
62-
63- - name : Upload site artifact
64- uses : actions/upload-artifact@v4
65- with :
66- name : ${{ steps.meta.outputs.artifact_name }}
67- path : _site
68- retention-days : 7
69-
70- - name : Install Playwright Chromium
71- run : npx -y playwright@1.53.0 install --with-deps chromium
72-
73- - name : Capture preview screenshot
74- env :
75- POST_URL : ${{ steps.changed_post.outputs.post_url }}
76- run : |
77- python3 -m http.server --directory _site 4173 > /tmp/pr-preview-server.log 2>&1 &
78- server_pid=$!
79- trap 'kill "$server_pid"' EXIT
80-
81- for i in {1..20}; do
82- if curl -fsS "http://127.0.0.1:4173${POST_URL}" >/dev/null; then
83- break
84- fi
85- sleep 0.5
86- done
87-
88- npx -y playwright@1.53.0 screenshot --browser=chromium --full-page "http://127.0.0.1:4173${POST_URL}" preview-long.png
89-
90- - name : Upload screenshot artifact
91- uses : actions/upload-artifact@v4
92- with :
93- name : ${{ steps.meta.outputs.screenshot_name }}
94- path : preview-long.png
95- retention-days : 7
96-
9717 deploy-cloudflare-preview :
9818 runs-on : ubuntu-latest
99- needs : build-preview
100- if : ${{ !github.event.pull_request.head.repo.fork && vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && secrets.CLOUDFLARE_API_TOKEN != '' }}
19+ if : ${{ !github.event.pull_request.head.repo.fork && vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' }}
10120 permissions :
10221 contents : read
10322 deployments : write
10423 outputs :
105- preview_url : ${{ steps.cf.outputs.alias }}
24+ preview_url : ${{ steps.cf.outputs.pages-deployment-alias-url }}
25+ deployment_url : ${{ steps.cf.outputs.deployment-url }}
26+ has_token : ${{ steps.token_check.outputs.has_token }}
10627 steps :
28+ - name : Check Cloudflare token
29+ id : token_check
30+ env :
31+ CLOUDFLARE_API_TOKEN : ${{ secrets.CLOUDFLARE_API_TOKEN }}
32+ run : |
33+ if [ -n "$CLOUDFLARE_API_TOKEN" ]; then
34+ echo "has_token=true" >> "$GITHUB_OUTPUT"
35+ else
36+ echo "has_token=false" >> "$GITHUB_OUTPUT"
37+ fi
38+
10739 - name : Checkout
108- uses : actions/checkout@v4
40+ if : ${{ steps.token_check.outputs.has_token == 'true' }}
41+ uses : actions/checkout@v6
10942
11043 - name : Set up Ruby
44+ if : ${{ steps.token_check.outputs.has_token == 'true' }}
11145 uses : ruby/setup-ruby@v1
11246 with :
47+ ruby-version : " 3.1"
11348 bundler-cache : true
11449
11550 - name : Build site
51+ if : ${{ steps.token_check.outputs.has_token == 'true' }}
11652 run : bundle exec jekyll build
11753
11854 - name : Publish to Cloudflare Pages
11955 id : cf
120- uses : cloudflare/pages-action@v1
56+ if : ${{ steps.token_check.outputs.has_token == 'true' }}
57+ uses : cloudflare/wrangler-action@v3
12158 with :
12259 apiToken : ${{ secrets.CLOUDFLARE_API_TOKEN }}
12360 accountId : ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
124- projectName : ${{ vars.CLOUDFLARE_PAGES_PROJECT }}
125- directory : _site
61+ command : pages deploy _site --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=pr-${{ github.event.number }}
12662 gitHubToken : ${{ secrets.GITHUB_TOKEN }}
127- branch : pr-${{ github.event.number }}
128- wranglerVersion : ' 3'
63+ wranglerVersion : " 3"
12964
13065 comment-preview :
13166 runs-on : ubuntu-latest
132- needs : [build-preview, deploy-cloudflare-preview]
133- if : ${{ always() && !github.event.pull_request.head.repo.fork && needs.build- preview.result == 'success' && ( needs.deploy-cloudflare-preview.result == 'success ' || needs.deploy-cloudflare-preview.result == 'skipped ') }}
67+ needs : deploy-cloudflare-preview
68+ if : ${{ always() && !github.event.pull_request.head.repo.fork && ( needs.deploy-cloudflare- preview.result == 'success' || needs.deploy-cloudflare-preview.result == 'skipped ' || needs.deploy-cloudflare-preview.result == 'failure ') }}
13469 steps :
13570 - name : Add or update PR preview comment
13671 uses : actions/github-script@v7
13772 env :
13873 RUN_URL : https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
139- ARTIFACT_NAME : ${{ needs.build-preview.outputs.artifact_name }}
140- SCREENSHOT_NAME : ${{ needs.build-preview.outputs.screenshot_name }}
141- POST_PATH : ${{ needs.build-preview.outputs.post_path }}
142- POST_URL : ${{ needs.build-preview.outputs.post_url }}
14374 CLOUDFLARE_URL : ${{ needs.deploy-cloudflare-preview.outputs.preview_url }}
144- CLOUDFLARE_ENABLED : ${{ vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && secrets.CLOUDFLARE_API_TOKEN != '' }}
75+ CLOUDFLARE_DEPLOYMENT_URL : ${{ needs.deploy-cloudflare-preview.outputs.deployment_url }}
76+ CLOUDFLARE_ENABLED : ${{ vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && needs.deploy-cloudflare-preview.outputs.has_token == 'true' }}
14577 CLOUDFLARE_RESULT : ${{ needs.deploy-cloudflare-preview.result }}
14678 with :
14779 script : |
14880 const marker = "<!-- pr-preview-comment -->";
149- const postLine = process.env.POST_PATH
150- ? `- Changed post detected: \`${process.env.POST_PATH}\`\n- Suggested file to open after extracting: \`_site${process.env.POST_URL}index.html\``
151- : "- No post markdown file changed in this PR; preview starts at `_site/index.html`.";
81+ const previewUrl = process.env.CLOUDFLARE_URL || process.env.CLOUDFLARE_DEPLOYMENT_URL;
15282
15383 const cloudflareLine = process.env.CLOUDFLARE_ENABLED === "true"
15484 ? (process.env.CLOUDFLARE_RESULT === "success"
155- ? (process.env.CLOUDFLARE_URL
156- ? `- Hosted preview (Cloudflare Pages): [Open preview](${process.env.CLOUDFLARE_URL })`
85+ ? (previewUrl
86+ ? `- Hosted preview (Cloudflare Pages): [Open preview](${previewUrl })`
15787 : "- Hosted preview (Cloudflare Pages): deployed, but URL output was empty. Check deployment details.")
158- : " - Hosted preview (Cloudflare Pages): deployment did not succeed; check workflow logs." )
88+ : ` - Hosted preview (Cloudflare Pages): deployment did not succeed; check [ workflow logs](${process.env.RUN_URL}).` )
15989 : "- Hosted preview (Cloudflare Pages): not configured (set repository vars/secrets; see README).";
16090
16191 const body = `${marker}
16292 ## PR Preview Ready
16393
164- A static preview was built for this PR.
165-
166- - Download artifact: **${process.env.ARTIFACT_NAME}** from [this workflow run](${process.env.RUN_URL})
167- - Download long screenshot artifact: **${process.env.SCREENSHOT_NAME}** from [this workflow run](${process.env.RUN_URL})
168- ${postLine}
16994 ${cloudflareLine}
170- - Quick local preview:
171- - Extract artifact zip
172- - Run: \`python3 -m http.server --directory _site 4173\`
173- - Open: \`http://localhost:4173${process.env.POST_URL}\`
17495 `;
17596
17697 const { owner, repo } = context.repo;
0 commit comments