From 4c25aa350f0e371b4739c8559558b4410baaf63d Mon Sep 17 00:00:00 2001 From: Paul Williams Date: Tue, 17 Feb 2026 20:10:31 +0530 Subject: [PATCH] ci: enhance pr site preview comment and add concurrency --- .github/workflows/pages.yml | 56 ++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index c7f2001..6734976 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -1,15 +1,22 @@ name: PR Preview & GitHub Pages Publish + on: pull_request: types: [opened, synchronize, reopened, closed] push: branches: - main + permissions: - contents: write # gh-pages + cleanup - pull-requests: write # PR comments - pages: write # deploy-pages - id-token: write # required by deploy-pages + contents: write + pull-requests: write + pages: write + id-token: write + +concurrency: + group: pages-${{ github.ref }} + cancel-in-progress: true + jobs: build: if: github.event.action != 'closed' @@ -39,7 +46,9 @@ jobs: with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: site + publish_branch: gh-pages destination_dir: pr-${{ github.event.pull_request.number }} + keep_files: true - name: Comment PR with preview URL if: github.event_name == 'pull_request' @@ -48,15 +57,38 @@ jobs: script: | const pr = context.payload.pull_request.number; const { owner, repo } = context.repo; - const url = `https://ff4j.github.io/docs/pr-${pr}/`; + const marker = ""; + + const body = `${marker} \n **Site preview available** 🔗 ${url}`; - await github.rest.issues.createComment({ + const { data: comments } = await github.rest.issues.listComments({ owner, repo, issue_number: pr, - body: `📘 **Site preview available**:\n\n${url}` }); + + const existing = comments.find(comment => + comment.body?.includes(marker) + ); + + if (existing) { + // Update existing comment + await github.rest.issues.updateComment({ + owner, + repo, + comment_id: existing.id, + body, + }); + } else { + // Create new comment + await github.rest.issues.createComment({ + owner, + repo, + issue_number: pr, + body, + }); + } - name: Upload Pages artifact if: github.event_name == 'push' @@ -76,8 +108,7 @@ jobs: cleanup: if: github.event_name == 'pull_request' && - github.event.action == 'closed' && - github.event.pull_request.merged == false + github.event.action == 'closed' runs-on: ubuntu-latest steps: @@ -88,7 +119,12 @@ jobs: - name: Remove PR preview folder run: | - rm -rf pr-${{ github.event.pull_request.number }} + if [ -d "pr-${{ github.event.pull_request.number }}" ]; then + rm -rf pr-${{ github.event.pull_request.number }} + echo "Preview folder removed." + else + echo "Preview folder does not exist." + fi - name: Commit cleanup run: |