|
| 1 | +name: Remove preview for PR |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: |
| 6 | + - "Clean up after closed PR" |
| 7 | + types: |
| 8 | + - completed |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + id-token: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }} |
| 17 | + cancel-in-progress: false |
| 18 | + |
| 19 | +jobs: |
| 20 | + remove-preview: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + if: > |
| 23 | + github.event.workflow_run.event == 'pull_request' && |
| 24 | + github.event.workflow_run.conclusion == 'success' |
| 25 | +
|
| 26 | + steps: |
| 27 | + - name: Download artifacts (PR number remove file) |
| 28 | + uses: actions/download-artifact@v4 |
| 29 | + with: |
| 30 | + name: pr-num-rm |
| 31 | + path: ./ |
| 32 | + run-id: ${{ github.event.workflow_run.id }} |
| 33 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + |
| 35 | + - name: Read PR number remove from file |
| 36 | + id: read-pr-num-rm |
| 37 | + run: | |
| 38 | + echo "PR_NUMBER_RM=$(cat pr-num-rm.txt)" >> $GITHUB_ENV |
| 39 | + echo ${{ env.PR_NUMBER_RM }} |
| 40 | +
|
| 41 | + - name: Remove comment with preview URL |
| 42 | + uses: actions/github-script@v7 |
| 43 | + with: |
| 44 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + script: | |
| 46 | + // Fetch all comments on the PR |
| 47 | + const comments = await github.rest.issues.listComments({ |
| 48 | + issue_number: ${{ env.PR_NUMBER_RM }}, |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo |
| 51 | + }); |
| 52 | +
|
| 53 | + // Loop through comments and delete those matching a specific condition |
| 54 | + for (const comment of comments.data) { |
| 55 | + if (comment.body.includes("🎉 A preview for this PR is available at:")) { |
| 56 | + await github.rest.issues.deleteComment({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + comment_id: comment.id |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | +
|
| 64 | + - name: Remove PR-specific subdirectory from S3 |
| 65 | + run: | |
| 66 | + echo "Cleaning up preview folder for PR #${{ env.PR_NUMBER_RM }}" |
| 67 | + aws s3 rm "s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ env.PR_NUMBER_RM }}" --recursive |
| 68 | + env: |
| 69 | + AWS_REGION: ${{ secrets.AWS_REGION }} |
| 70 | + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 71 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 72 | + |
| 73 | + - name: Invalidate CloudFront cache for PR |
| 74 | + uses: chetan/invalidate-cloudfront-action@v2 |
| 75 | + env: |
| 76 | + PATHS: "/pr${{ env.PR_NUMBER_RM }}/*" |
| 77 | + DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }} |
| 78 | + AWS_REGION: ${{ secrets.AWS_REGION }} |
| 79 | + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 80 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
0 commit comments