diff --git a/.github/workflows/preview-url-comment.yml b/.github/workflows/preview-url-comment.yml new file mode 100644 index 00000000000..830013a1eb0 --- /dev/null +++ b/.github/workflows/preview-url-comment.yml @@ -0,0 +1,58 @@ +name: Comment Preview URLs + +on: + workflow_run: + workflows: ["Generate Preview URLs"] + types: + - completed + +jobs: + comment: + name: Post Preview URLs Comment + runs-on: ubuntu-latest + if: > + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' + permissions: + pull-requests: write + + steps: + - name: Download artifacts + uses: actions/download-artifact@v4 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + + - name: Read PR metadata + id: pr-metadata + run: | + PR_NUMBER=$(find . -name "pr_number.txt" -exec cat {} \;) + PR_SHA=$(find . -name "pr_sha.txt" -exec cat {} \;) + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT + echo "pr_sha=$PR_SHA" >> $GITHUB_OUTPUT + + - name: Read preview URLs + id: preview-urls + run: | + PREVIEW_CONTENT=$(find . -name "preview_urls.txt" -exec cat {} \;) + { + echo 'content<> $GITHUB_OUTPUT + + - name: Find existing comment + uses: peter-evans/find-comment@v4 + id: fc + with: + issue-number: ${{ steps.pr-metadata.outputs.pr_number }} + comment-author: 'github-actions[bot]' + body-includes: '本次 PR 文档预览链接' + + - name: Create or update comment + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ steps.pr-metadata.outputs.pr_number }} + body: ${{ steps.preview-urls.outputs.content }} + edit-mode: replace diff --git a/.github/workflows/preview-url-generate.yml b/.github/workflows/preview-url-generate.yml new file mode 100644 index 00000000000..e12f9b74056 --- /dev/null +++ b/.github/workflows/preview-url-generate.yml @@ -0,0 +1,52 @@ +name: Generate Preview URLs + +on: + pull_request: + branches: ["develop"] + paths: + - 'docs/**.rst' + - 'docs/**.md' + +jobs: + generate-urls: + name: Generate Preview URLs + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - name: Checkout PR branch + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Fetch base branch + run: | + git fetch origin develop:develop + + - name: Generate preview URLs + id: generate + run: | + chmod +x ci_scripts/report_preview_url.sh + ./ci_scripts/report_preview_url.sh ${{ github.event.pull_request.number }} > preview_urls.txt + + - name: Upload preview URLs as artifact + uses: actions/upload-artifact@v4 + with: + name: preview-urls-${{ github.event.pull_request.number }} + path: preview_urls.txt + retention-days: 1 + + - name: Save PR metadata + run: | + echo "${{ github.event.pull_request.number }}" > pr_number.txt + echo "${{ github.event.pull_request.head.sha }}" > pr_sha.txt + + - name: Upload PR metadata + uses: actions/upload-artifact@v4 + with: + name: pr-metadata-${{ github.event.pull_request.number }} + path: | + pr_number.txt + pr_sha.txt + retention-days: 1 diff --git a/ci_scripts/report_preview_url.sh b/ci_scripts/report_preview_url.sh new file mode 100644 index 00000000000..993bd53da18 --- /dev/null +++ b/ci_scripts/report_preview_url.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +pr_id="$1" + +if [ -z "$pr_id" ]; then + echo "Error: Pull Request ID is not provided." + exit 1 +fi + +generate_preview_url() { + local file_path="$1" + local pr_id="$2" + local path_no_ext="${file_path%.*}" + local base_url="http://preview-pr-${pr_id}.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/zh/" + local final_url="${base_url}${path_no_ext}.html" + echo "$final_url" +} + +mapfile -t all_git_files < <(git diff --name-only --diff-filter=ACMR develop | sed 's#^docs/##') + +output_lines=() + +for file in "${all_git_files[@]}"; do + if [[ "$file" == *.rst || "$file" == *.md ]]; then + url=$(generate_preview_url "$file" "$pr_id") + output_lines+=("- \`docs/${file}\`: [点击预览](${url})") + fi +done + + +if [ ${#output_lines[@]} -gt 0 ]; then + cat <<-EOF +
+📚 本次 PR 文档预览链接 (点击展开) + +以下是本次 PR 中变更文档的预览链接: + +$(printf '%s\n' "${output_lines[@]}") + +
+EOF +fi