1+ # .github/workflows/comment-docs-preview.yml
2+ name : Comment Docs Build Status
3+
4+ on :
5+ workflow_run :
6+ workflows : ["Build Docs Preview"]
7+ types :
8+ - completed
9+ pull_request :
10+ types :
11+ - closed
12+ paths :
13+ - ' **/*.po'
14+
15+ jobs :
16+ comment :
17+ if : github.event_name == 'workflow_run'
18+ runs-on : ubuntu-latest
19+ permissions :
20+ pull-requests : write
21+ contents : write
22+ actions : read
23+
24+ 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+
47+ - name : Download build result
48+ uses : actions/download-artifact@v4
49+ with :
50+ name : build-result
51+ github-token : ${{ secrets.GITHUB_TOKEN }}
52+ run-id : ${{ github.event.workflow_run.id }}
53+
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 }}
61+
62+ - name : Read PR number
63+ id : pr
64+ run : echo "number=$(find . -name "pr_number.txt" | head -1 | xargs cat)" >> $GITHUB_OUTPUT
65+
66+ - name : Read build log
67+ id : log
68+ run : |
69+ LOG=$(find . -name "build.log" | head -1 | xargs tail -20)
70+ echo "content<<EOF" >> $GITHUB_OUTPUT
71+ echo "$LOG" >> $GITHUB_OUTPUT
72+ echo "EOF" >> $GITHUB_OUTPUT
73+
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+
83+ - name : Post comment
84+ uses : actions/github-script@v7
85+ with :
86+ script : |
87+ const success = '${{ github.event.workflow_run.conclusion }}' === 'success';
88+ const icon = success ? '✅' : '❌';
89+ 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`;
94+
95+ const body = `### ${icon} Docs build ${status}
96+
97+ ${success ? `📖 **[Preview the docs](${previewUrl})**` : ''}
98+
99+ <details>
100+ <summary>Build log (last 20 lines)</summary>
101+
102+ \`\`\`
103+ ${{ steps.log.outputs.content }}
104+ \`\`\`
105+
106+ </details>`;
107+
108+ github.rest.issues.createComment({
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