Skip to content

Commit 0eac505

Browse files
authored
[CI] Add Report Preview URLs Workflow (#7524)
1 parent c3cf3eb commit 0eac505

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Comment Preview URLs
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Generate Preview URLs"]
6+
types:
7+
- completed
8+
9+
jobs:
10+
comment:
11+
name: Post Preview URLs Comment
12+
runs-on: ubuntu-latest
13+
if: >
14+
github.event.workflow_run.event == 'pull_request' &&
15+
github.event.workflow_run.conclusion == 'success'
16+
permissions:
17+
pull-requests: write
18+
19+
steps:
20+
- name: Download artifacts
21+
uses: actions/download-artifact@v4
22+
with:
23+
github-token: ${{ secrets.GITHUB_TOKEN }}
24+
run-id: ${{ github.event.workflow_run.id }}
25+
26+
- name: Read PR metadata
27+
id: pr-metadata
28+
run: |
29+
PR_NUMBER=$(find . -name "pr_number.txt" -exec cat {} \;)
30+
PR_SHA=$(find . -name "pr_sha.txt" -exec cat {} \;)
31+
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
32+
echo "pr_sha=$PR_SHA" >> $GITHUB_OUTPUT
33+
34+
- name: Read preview URLs
35+
id: preview-urls
36+
run: |
37+
PREVIEW_CONTENT=$(find . -name "preview_urls.txt" -exec cat {} \;)
38+
{
39+
echo 'content<<EOF'
40+
echo "$PREVIEW_CONTENT"
41+
echo EOF
42+
} >> $GITHUB_OUTPUT
43+
44+
- name: Find existing comment
45+
uses: peter-evans/find-comment@v4
46+
id: fc
47+
with:
48+
issue-number: ${{ steps.pr-metadata.outputs.pr_number }}
49+
comment-author: 'github-actions[bot]'
50+
body-includes: '本次 PR 文档预览链接'
51+
52+
- name: Create or update comment
53+
uses: peter-evans/create-or-update-comment@v4
54+
with:
55+
comment-id: ${{ steps.fc.outputs.comment-id }}
56+
issue-number: ${{ steps.pr-metadata.outputs.pr_number }}
57+
body: ${{ steps.preview-urls.outputs.content }}
58+
edit-mode: replace
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Generate Preview URLs
2+
3+
on:
4+
pull_request:
5+
branches: ["develop"]
6+
paths:
7+
- 'docs/**.rst'
8+
- 'docs/**.md'
9+
10+
jobs:
11+
generate-urls:
12+
name: Generate Preview URLs
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
17+
steps:
18+
- name: Checkout PR branch
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Fetch base branch
24+
run: |
25+
git fetch origin develop:develop
26+
27+
- name: Generate preview URLs
28+
id: generate
29+
run: |
30+
chmod +x ci_scripts/report_preview_url.sh
31+
./ci_scripts/report_preview_url.sh ${{ github.event.pull_request.number }} > preview_urls.txt
32+
33+
- name: Upload preview URLs as artifact
34+
uses: actions/upload-artifact@v4
35+
with:
36+
name: preview-urls-${{ github.event.pull_request.number }}
37+
path: preview_urls.txt
38+
retention-days: 1
39+
40+
- name: Save PR metadata
41+
run: |
42+
echo "${{ github.event.pull_request.number }}" > pr_number.txt
43+
echo "${{ github.event.pull_request.head.sha }}" > pr_sha.txt
44+
45+
- name: Upload PR metadata
46+
uses: actions/upload-artifact@v4
47+
with:
48+
name: pr-metadata-${{ github.event.pull_request.number }}
49+
path: |
50+
pr_number.txt
51+
pr_sha.txt
52+
retention-days: 1

ci_scripts/report_preview_url.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
pr_id="$1"
4+
5+
if [ -z "$pr_id" ]; then
6+
echo "Error: Pull Request ID is not provided."
7+
exit 1
8+
fi
9+
10+
generate_preview_url() {
11+
local file_path="$1"
12+
local pr_id="$2"
13+
local path_no_ext="${file_path%.*}"
14+
local base_url="http://preview-pr-${pr_id}.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/zh/"
15+
local final_url="${base_url}${path_no_ext}.html"
16+
echo "$final_url"
17+
}
18+
19+
mapfile -t all_git_files < <(git diff --name-only --diff-filter=ACMR develop | sed 's#^docs/##')
20+
21+
output_lines=()
22+
23+
for file in "${all_git_files[@]}"; do
24+
if [[ "$file" == *.rst || "$file" == *.md ]]; then
25+
url=$(generate_preview_url "$file" "$pr_id")
26+
output_lines+=("- \`docs/${file}\`: [点击预览](${url})")
27+
fi
28+
done
29+
30+
31+
if [ ${#output_lines[@]} -gt 0 ]; then
32+
cat <<-EOF
33+
<details>
34+
<summary>📚 本次 PR 文档预览链接 (点击展开)</summary>
35+
36+
以下是本次 PR 中变更文档的预览链接:
37+
38+
$(printf '%s\n' "${output_lines[@]}")
39+
40+
</details>
41+
EOF
42+
fi

0 commit comments

Comments
 (0)