-
Notifications
You must be signed in to change notification settings - Fork 4
196 lines (174 loc) · 6.75 KB
/
Copy pathpreview-changes.yml
File metadata and controls
196 lines (174 loc) · 6.75 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
name: Preview Pages Management
on:
issue_comment:
types: [created]
pull_request:
branches: [main]
types: [closed]
paths-ignore:
- 'bot_challenges/**'
jobs:
check-permission:
name: Check Author & Command
if: github.event.issue.pull_request && contains(github.event.comment.body, '/preview')
runs-on: ubuntu-latest
outputs:
authorized: ${{ steps.check.outputs.authorized }}
steps:
- name: Check user permissions
id: check
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const username = context.actor;
const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username,
});
const allowed = ['admin', 'write'].includes(permission.permission);
if (!allowed) {
core.setFailed(`User @${username} is not authorized to trigger preview deployments.`);
}
core.setOutput('authorized', allowed);
notify-preview-start:
name: Post Building Comment & Cleanup Trigger
needs: check-permission
runs-on: ubuntu-latest
permissions:
pull-requests: write
outputs:
pr_num: ${{ steps.pr_data.outputs.pr_num }}
sha: ${{ steps.pr_data.outputs.sha }}
ref: ${{ steps.pr_data.outputs.ref }}
pr_author: ${{ steps.pr_data.outputs.pr_author }}
comment_id: ${{ steps.create_comment.outputs.comment_id }}
steps:
- name: Get PR Information
id: pr_data
uses: actions/github-script@v7
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
core.setOutput('pr_num', pr.data.number);
core.setOutput('sha', pr.data.head.sha);
core.setOutput('ref', pr.data.head.ref);
core.setOutput('pr_author', pr.data.user.login);
- name: Post "Preview building" comment
id: create_comment
uses: actions/github-script@v7
with:
script: |
const triggerUser = context.actor;
const prAuthor = "${{ steps.pr_data.outputs.pr_author }}";
let mentions = `@${triggerUser}`;
if (prAuthor && prAuthor !== triggerUser) {
mentions += ` @${prAuthor}`;
}
const { data: comment } = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `⏳ **Preview building...**\n\ncc ${mentions}`,
});
core.setOutput('comment_id', comment.id);
- name: Delete triggering /preview comment
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
});
deploy-preview:
name: Deploy Preview to Cloudflare Pages
needs: [check-permission, notify-preview-start]
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: read
steps:
- name: Checkout Curriculum (PR Code)
uses: actions/checkout@v4
with:
path: Curriculum
ref: ${{ needs.notify-preview-start.outputs.sha }}
- name: Checkout Engine
uses: actions/checkout@v4
with:
repository: ThePythonLedger/Docusaurus-engine
path: engine
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
cache-dependency-path: engine/package-lock.json
- name: Build Site Assets
env:
DOCUSAURUS_URL: https://pr-${{ needs.notify-preview-start.outputs.pr_num }}.python-ledger-preview.pages.dev
DOCUSAURUS_BASE_URL: /
run: |
cd engine
npm ci
npm run build
- name: Deploy to Cloudflare Pages
uses: cloudflare/wrangler-action@v4
id: cloudflare_deploy
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
command: pages deploy engine/build --project-name=python-ledger-preview --branch=pr-${{ needs.notify-preview-start.outputs.pr_num }}
- name: Update Preview Comment with Link
uses: actions/github-script@v7
with:
script: |
const prNumber = ${{ needs.notify-preview-start.outputs.pr_num }};
const prAuthor = "${{ needs.notify-preview-start.outputs.pr_author }}";
const triggerUser = context.actor;
const previewUrl = `https://pr-${prNumber}.python-ledger-preview.pages.dev`;
let mentions = `@${triggerUser}`;
if (prAuthor && prAuthor !== triggerUser) {
mentions += ` @${prAuthor}`;
}
const message = `🚀 **Preview Deployment Ready!**\n\ncc ${mentions}\n\nView changes live at:\n${previewUrl}`;
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ needs.notify-preview-start.outputs.comment_id }},
body: message,
});
- name: Mark Preview as Failed
if: failure()
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ needs.notify-preview-start.outputs.comment_id }},
body: `❌ **Preview build failed.** Check the [workflow run](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}) for details.`,
});
cleanup-preview:
name: Delete Cloudflare Pages Preview
if: github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- name: Delete Cloudflare Pages Branch via API
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
run: |
BRANCH_NAME="pr-${{ github.event.number }}"
PROJECT_NAME="python-ledger-preview"
echo "Deleting Cloudflare Pages branch: $BRANCH_NAME"
curl -s -X DELETE \
"https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/pages/projects/${PROJECT_NAME}/branches/${BRANCH_NAME}" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json"