66 workflows : ["Build Docs Preview"]
77 types :
88 - completed
9+ pull_request :
10+ types :
11+ - closed
12+ paths :
13+ - ' **/*.po'
914
1015jobs :
1116 comment :
17+ if : github.event_name == 'workflow_run'
1218 runs-on : ubuntu-latest
1319 permissions :
1420 pull-requests : write
21+ contents : write
22+ actions : read
1523
1624 steps :
25+ - name : Checkout repo
26+ uses : actions/checkout@v4
27+ with :
28+ fetch-depth : 0
29+
30+ - name : Ensure gh-pages branch exists
31+ run : |
32+ git config user.name "github-actions[bot]"
33+ git config user.email "github-actions[bot]@users.noreply.github.com"
34+ if ! git ls-remote --exit-code --heads origin gh-pages; then
35+ echo "gh-pages branch not found, creating it..."
36+ git checkout --orphan gh-pages
37+ git rm -rf .
38+ echo "# Doc Previews" > README.md
39+ git add README.md
40+ git commit -m "chore: initialize gh-pages branch"
41+ git push origin gh-pages
42+ git checkout -
43+ else
44+ echo "gh-pages branch already exists, skipping."
45+ fi
46+
1747 - name : Download build result
1848 uses : actions/download-artifact@v4
1949 with :
2050 name : build-result
2151 github-token : ${{ secrets.GITHUB_TOKEN }}
2252 run-id : ${{ github.event.workflow_run.id }}
2353
24- - name : Debug artifact contents # <-- add this temporarily
25- run : find . -type f | head -30
54+ - name : Download HTML preview
55+ uses : actions/download-artifact@v4
56+ with :
57+ name : docs-html
58+ path : html-preview/
59+ github-token : ${{ secrets.GITHUB_TOKEN }}
60+ run-id : ${{ github.event.workflow_run.id }}
2661
2762 - name : Read PR number
2863 id : pr
@@ -31,23 +66,36 @@ jobs:
3166 - name : Read build log
3267 id : log
3368 run : |
34- # Grab the last 20 lines so the comment isn't massive
35- LOG=$(tail -20 build.log)
36- # GitHub Actions has a specific way to handle multiline strings
69+ LOG=$(find . -name "build.log" | head -1 | xargs tail -20)
3770 echo "content<<EOF" >> $GITHUB_OUTPUT
3871 echo "$LOG" >> $GITHUB_OUTPUT
3972 echo "EOF" >> $GITHUB_OUTPUT
4073
74+ - name : Deploy preview to gh-pages
75+ uses : peaceiris/actions-gh-pages@v4
76+ with :
77+ github_token : ${{ secrets.GITHUB_TOKEN }}
78+ publish_dir : ./html-preview
79+ destination_dir : previews/${{ steps.pr.outputs.number }}
80+ keep_files : true
81+ enable_jekyll : false
82+
4183 - name : Post comment
4284 uses : actions/github-script@v7
4385 with :
4486 script : |
4587 const success = '${{ github.event.workflow_run.conclusion }}' === 'success';
4688 const icon = success ? '✅' : '❌';
4789 const status = success ? 'succeeded' : 'failed';
90+ const prNumber = ${{ steps.pr.outputs.number }};
91+ const owner = context.repo.owner;
92+ const repo = context.repo.repo;
93+ const previewUrl = `https://${owner}.github.io/${repo}/previews/${prNumber}/index.html`;
4894
4995 const body = `### ${icon} Docs build ${status}
5096
97+ ${success ? `📖 **[Preview the docs](${previewUrl})**` : ''}
98+
5199 <details>
52100 <summary>Build log (last 20 lines)</summary>
53101
@@ -58,8 +106,44 @@ jobs:
58106 </details>`;
59107
60108 github.rest.issues.createComment({
61- owner: context.repo.owner,
62- repo: context.repo.repo,
63- issue_number: ${{ steps.pr.outputs.number }},
64- body: body
65- });
109+ owner,
110+ repo,
111+ issue_number: prNumber,
112+ body
113+ });
114+
115+ cleanup :
116+ if : github.event_name == 'pull_request' && github.event.action == 'closed'
117+ runs-on : ubuntu-latest
118+ permissions :
119+ contents : write
120+
121+ steps :
122+ - name : Check if gh-pages exists
123+ id : check
124+ run : |
125+ if git ls-remote --exit-code --heads https://github.com/${{ github.repository }}.git gh-pages; then
126+ echo "exists=true" >> $GITHUB_OUTPUT
127+ else
128+ echo "exists=false" >> $GITHUB_OUTPUT
129+ fi
130+
131+ - name : Checkout gh-pages
132+ if : steps.check.outputs.exists == 'true'
133+ uses : actions/checkout@v4
134+ with :
135+ ref : gh-pages
136+
137+ - name : Remove preview folder
138+ if : steps.check.outputs.exists == 'true'
139+ run : |
140+ PR=${{ github.event.pull_request.number }}
141+ if [ -d "previews/$PR" ]; then
142+ git config user.name "github-actions[bot]"
143+ git config user.email "github-actions[bot]@users.noreply.github.com"
144+ git rm -rf "previews/$PR"
145+ git commit -m "chore: remove preview for PR #$PR"
146+ git push
147+ else
148+ echo "No preview folder found for PR #$PR, nothing to clean up."
149+ fi
0 commit comments