-
Notifications
You must be signed in to change notification settings - Fork 1
71 lines (66 loc) · 2.34 KB
/
Copy pathpr-preview-cleanup.yml
File metadata and controls
71 lines (66 loc) · 2.34 KB
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
name: pr-preview-cleanup
on:
pull_request:
types: [closed]
workflow_dispatch:
inputs:
pr_number:
description: PR number to clean up
required: true
permissions:
contents: write
deployments: write
pull-requests: read
jobs:
cleanup:
runs-on: ubuntu-latest
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'workflow_dispatch'
env:
PR_NUMBER: ${{ github.event.pull_request.number || github.event.inputs.pr_number }}
steps:
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
fetch-depth: 1
- name: Remove pr-${{ env.PR_NUMBER }} preview directory
run: |
if [ -d "pr-${PR_NUMBER}" ]; then
rm -rf "pr-${PR_NUMBER}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
if git diff --staged --quiet; then
echo "Nothing staged after removal — skipping commit."
else
git commit -m "cleanup(ui): remove pr-${PR_NUMBER} preview"
git push
fi
else
echo "No preview directory pr-${PR_NUMBER} to remove."
fi
- name: Mark pr-preview-${{ env.PR_NUMBER }} deployment inactive
uses: actions/github-script@v7
with:
script: |
const envName = `pr-preview-${process.env.PR_NUMBER}`;
const { data: deployments } = await github.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
environment: envName,
per_page: 100,
});
if (deployments.length === 0) {
core.info(`No deployments found for environment ${envName}`);
return;
}
for (const dep of deployments) {
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: dep.id,
state: 'inactive',
description: 'PR closed; preview cleaned up.',
});
}
core.info(`Marked ${deployments.length} deployment(s) inactive for ${envName}.`);