diff --git a/.github/workflows/pr-deploy-preview.yml b/.github/workflows/pr-deploy-preview.yml index 071185486..bde08d53b 100644 --- a/.github/workflows/pr-deploy-preview.yml +++ b/.github/workflows/pr-deploy-preview.yml @@ -67,22 +67,51 @@ jobs: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # - name: Post Preview Link to PR + # uses: actions/github-script@v7 + # with: + # github-token: ${{secrets.GITHUB_TOKEN}} + # script: | + # github.rest.issues.createComment({ + # issue_number: context.issue.number, + # owner: context.repo.owner, + # repo: context.repo.repo, + # body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }} ` + # }) + - name: Post Preview Link to PR uses: actions/github-script@v7 with: - github-token: ${{secrets.GITHUB_TOKEN}} + github-token: ${{ secrets.GITHUB_TOKEN }} script: | - github.rest.issues.createComment({ + const commentIdentifier = "🎉 A preview of this PR is available at:"; // Unique identifier for the preview comment + + // Fetch existing comments + const { data: comments } = await github.rest.issues.listComments({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }} ` - }) + }); - invalidate-cache: - needs: build-and-deploy-preview - runs-on: ubuntu-latest - steps: + let previewComment = comments.find(comment => comment.body.includes(commentIdentifier)); + + if (previewComment) { + // Update the existing comment + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: previewComment.id, + body: `🎉 A preview of this PR is available at: ${process.env.HUGO_BASEURL}`, + }); + } else { + // Post a new comment if none exists + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `🎉 A preview of this PR is available at: ${process.env.HUGO_BASEURL}`, + }); + } - name: Invalidate CloudFront cache for PR uses: chetan/invalidate-cloudfront-action@v2