diff --git a/.github/workflows/pr-close-cleanup.yml b/.github/workflows/pr-close-cleanup.yml new file mode 100644 index 00000000..9c25118f --- /dev/null +++ b/.github/workflows/pr-close-cleanup.yml @@ -0,0 +1,19 @@ +name: Clean up after closed PR + +on: + pull_request: + types: + - closed + +jobs: + clean-up-after-closed-pr: + runs-on: ubuntu-latest + steps: + - name: Create PR number remove file + run: echo "${{ github.event.pull_request.number }}" > pr-num-rm.txt + + - name: Upload PR number remove artifact + uses: actions/upload-artifact@v4 + with: + name: pr-num-rm + path: pr-num-rm.txt diff --git a/.github/workflows/pr-remove-preview.yml b/.github/workflows/pr-remove-preview.yml deleted file mode 100644 index 2b4e2f21..00000000 --- a/.github/workflows/pr-remove-preview.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Remove preview for PR - -on: - pull_request: - types: - - closed - -jobs: - remove-preview: - name: Remove preview for PR - runs-on: ubuntu-latest - - steps: - # Remove the PR-specific folder from S3 - - name: Remove PR-specific subdirectory from S3 - run: | - echo "Cleaning up preview folder for PR #${{ github.event.pull_request.number }}" - aws s3 rm "s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ github.event.pull_request.number }}" --recursive - env: - AWS_REGION: ${{ secrets.AWS_REGION }} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - - - name: Invalidate CloudFront cache for PR - uses: chetan/invalidate-cloudfront-action@v2 - env: - PATHS: "/pr-${{ github.event.pull_request.number }}/*" - DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }} - AWS_REGION: ${{ secrets.AWS_REGION }} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/.github/workflows/pr-deploy-preview.yml b/.github/workflows/prw-deploy-preview.yml similarity index 100% rename from .github/workflows/pr-deploy-preview.yml rename to .github/workflows/prw-deploy-preview.yml diff --git a/.github/workflows/prw-remove-preview.yml b/.github/workflows/prw-remove-preview.yml new file mode 100644 index 00000000..275e088c --- /dev/null +++ b/.github/workflows/prw-remove-preview.yml @@ -0,0 +1,80 @@ +name: Remove preview for PR + +on: + workflow_run: + workflows: + - "Clean up after closed PR" + types: + - completed + +permissions: + contents: read + id-token: write + pull-requests: write + +concurrency: + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }} + cancel-in-progress: false + +jobs: + remove-preview: + runs-on: ubuntu-latest + if: > + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' + + steps: + - name: Download artifacts (PR number remove file) + uses: actions/download-artifact@v4 + with: + name: pr-num-rm + path: ./ + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Read PR number remove from file + id: read-pr-num-rm + run: | + echo "PR_NUMBER_RM=$(cat pr-num-rm.txt)" >> $GITHUB_ENV + echo ${{ env.PR_NUMBER_RM }} + + - name: Remove comment with preview URL + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + // Fetch all comments on the PR + const comments = await github.rest.issues.listComments({ + issue_number: ${{ env.PR_NUMBER_RM }}, + owner: context.repo.owner, + repo: context.repo.repo + }); + + // Loop through comments and delete those matching a specific condition + for (const comment of comments.data) { + if (comment.body.includes("🎉 A preview for this PR is available at:")) { + await github.rest.issues.deleteComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: comment.id + }); + } + } + + - name: Remove PR-specific subdirectory from S3 + run: | + echo "Cleaning up preview folder for PR #${{ env.PR_NUMBER_RM }}" + aws s3 rm "s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ env.PR_NUMBER_RM }}" --recursive + env: + AWS_REGION: ${{ secrets.AWS_REGION }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + + - name: Invalidate CloudFront cache for PR + uses: chetan/invalidate-cloudfront-action@v2 + env: + PATHS: "/pr${{ env.PR_NUMBER_RM }}/*" + DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }} + AWS_REGION: ${{ secrets.AWS_REGION }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}