forked from espressif/developer-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
80 lines (70 loc) · 2.66 KB
/
prw-remove-preview.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 }}