-
Notifications
You must be signed in to change notification settings - Fork 2
221 lines (190 loc) · 7.99 KB
/
Copy pathdeploy.yml
File metadata and controls
221 lines (190 loc) · 7.99 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
name: Deploy to GitHub Pages
on:
push:
branches: [master]
workflow_dispatch:
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
permissions:
contents: read
pages: write
id-token: write
pull-requests: read
deployments: write
actions: read
jobs:
deploy:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Checkout
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: 22
cache: npm
- name: Install dependencies
run: npm ci
- name: Setup Pages
id: pages
uses: actions/configure-pages@v6
- name: Setup environment
run: ./scripts/ci-env.sh
- name: Build
run: |
npm run build
if [ -f CNAME ] && [ "${{ github.repository_owner }}" = "mume" ]; then cp CNAME dist/CNAME; fi
env:
VITE_BASE: ${{ env.VITE_BASE || format('{0}/', steps.pages.outputs.base_path) }}
- name: Collect PRs to deploy
uses: actions/github-script@v9
with:
script: |
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
per_page: 100,
});
const prsToDeploy = [];
for (const pr of pullRequests) {
prsToDeploy.push({
number: pr.number,
sha: pr.head.sha,
});
}
const fs = require('fs');
fs.writeFileSync('prs-to-deploy.json', JSON.stringify(prsToDeploy));
console.log(`PRs to deploy: ${prsToDeploy.map(p => p.number).join(', ') || 'none'}`);
- name: Download PR artifacts
uses: actions/github-script@v9
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_URL_PATH: ${{ steps.pages.outputs.base_path }}
with:
script: |
const fs = require('fs');
const path = require('path');
const prsToDeploy = JSON.parse(fs.readFileSync('prs-to-deploy.json', 'utf8'));
const distDir = 'dist';
for (const pr of prsToDeploy) {
try {
console.log(`Processing PR #${pr.number}...`);
const { data: runs } = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'pr-preview.yml',
head_sha: pr.sha,
status: 'success',
per_page: 1,
});
let artifactId;
if (runs.workflow_runs.length > 0) {
const runId = runs.workflow_runs[0].id;
const { data: runArtifacts } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId,
});
const prArtifact = runArtifacts.artifacts.find(a => a.name === `pr-${pr.number}`);
if (prArtifact) artifactId = prArtifact.id;
}
if (!artifactId) {
const { data: artifacts } = await github.rest.actions.listArtifactsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
name: `pr-${pr.number}`,
});
if (artifacts.artifacts.length === 0) {
console.log(`No artifact found for PR #${pr.number}`);
continue;
}
artifactId = artifacts.artifacts[0].id;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifactId,
archive_format: 'zip',
});
const prDir = path.join(distDir, `pr-${pr.number}`);
if (!fs.existsSync(prDir)) {
fs.mkdirSync(prDir, { recursive: true });
}
fs.writeFileSync(`/tmp/artifact-${pr.number}.zip`, Buffer.from(download.data));
const execSync = require('child_process').execSync;
execSync(`unzip -o /tmp/artifact-${pr.number}.zip -d /tmp`);
execSync(`unzip -o /tmp/pr-${pr.number}.zip -d ${prDir}`);
const rootRobots = path.join(distDir, 'robots.txt');
const disallowRule = `Disallow: /pr-${pr.number}/`;
if (fs.existsSync(rootRobots)) {
const content = fs.readFileSync(rootRobots, 'utf8');
if (!content.includes(disallowRule)) {
fs.appendFileSync(rootRobots, `\n${disallowRule}`);
}
} else {
fs.writeFileSync(rootRobots, `User-agent: *\n${disallowRule}`);
}
const prRef = `refs/pull/${pr.number}/head`;
const { data: existingDeployments } = await github.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
ref: pr.sha,
environment: `pr-${pr.number}`,
per_page: 1,
});
let deployment;
if (existingDeployments.length > 0) {
deployment = existingDeployments[0];
console.log(`Reusing existing deployment ${deployment.id} for PR #${pr.number}`);
} else {
const { data: created } = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: prRef,
environment: `pr-${pr.number}`,
required_contexts: [],
auto_merge: false,
description: `Preview for PR #${pr.number}`,
});
deployment = created;
console.log(`Created new deployment ${deployment.id} for PR #${pr.number}`);
}
const owner = context.repo.owner;
const repo = context.repo.repo;
const baseUrlPath = process.env.BASE_URL_PATH;
let base = `https://${owner}.github.io`;
const normalizedPath = baseUrlPath.startsWith('/') ? baseUrlPath : `/${baseUrlPath}`;
base = `${base}${normalizedPath}`.replace(/\/$/, '');
const url = `${base}/pr-${pr.number}/`;
console.log(`Deployment URL for PR #${pr.number}: ${url}`);
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.id,
state: 'success',
environment_url: url,
log_url: `https://github.com/${owner}/${repo}/actions/runs/${context.runId}`,
description: 'Preview deployed successfully',
});
} catch (e) {
console.log(`Failed to process PR #${pr.number}: ${e.message}`);
if (e.stack) console.log(e.stack);
}
}
- name: Upload artifact
uses: actions/upload-pages-artifact@v5
with:
path: dist
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v5