Repository navigation
181 lines (158 loc) · 5.6 KB
/
Copy pathdeploy-pages.yml
File metadata and controls
181 lines (158 loc) · 5.6 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
name: Deploy Pages
on:
push:
branches: [main]
paths:
- 'scripts/**'
- 'package.json'
- 'pnpm-lock.yaml'
- 'pnpm-workspace.yaml'
- 'packages/ui/**'
- 'apps/icon/**'
- 'apps/pwa/**'
- '.github/workflows/deploy-pages.yml'
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10.33.0
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- uses: while-coder/workflows/.github/actions/install-pnpm-dependencies@main
- run: pnpm --filter @nes-emulator/pwa run build
- uses: actions/configure-pages@v5
- uses: actions/upload-pages-artifact@v3
with:
path: apps/pwa/dist
deploy:
needs: build
runs-on: ubuntu-latest
timeout-minutes: 360
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
name: Deploy Pages until success
uses: actions/github-script@v9
with:
script: |
const artifactName = 'github-pages';
const retryDelayMs = 5000;
const { owner, repo } = context.repo;
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function getPagesArtifact() {
const artifacts = [];
let page = 1;
while (true) {
const response = await github.request(
'GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts',
{
owner,
repo,
run_id: Number(process.env.GITHUB_RUN_ID),
per_page: 100,
page,
},
);
const pageArtifacts = response.data.artifacts || [];
artifacts.push(...pageArtifacts);
if (pageArtifacts.length < 100) {
break;
}
page += 1;
}
const matches = artifacts.filter((artifact) => artifact.name === artifactName);
if (matches.length === 0) {
throw new Error(`No artifacts named "${artifactName}" were found for this workflow run.`);
}
if (matches.length > 1) {
throw new Error(`Multiple artifacts named "${artifactName}" were found for this workflow run.`);
}
return matches[0];
}
async function createDeployment(artifactId) {
const idToken = await core.getIDToken();
const response = await github.request(
'POST /repos/{owner}/{repo}/pages/deployments',
{
owner,
repo,
artifact_id: artifactId,
pages_build_version: context.sha,
oidc_token: idToken,
},
);
return response.data;
}
async function waitForDeployment(deploymentId) {
while (true) {
await sleep(retryDelayMs);
try {
const response = await github.request(
'GET /repos/{owner}/{repo}/pages/deployments/{deploymentId}',
{
owner,
repo,
deploymentId,
},
);
const status = response.data.status;
core.info(`Deployment ${deploymentId} status: ${status}`);
if (status === 'succeed') {
return true;
}
if (
status === 'deployment_failed' ||
status === 'deployment_content_failed' ||
status === 'deployment_cancelled' ||
status === 'deployment_lost'
) {
return false;
}
} catch (error) {
core.warning(`Could not get deployment ${deploymentId} status: ${error.message}`);
}
}
}
const artifact = await getPagesArtifact();
let attempt = 1;
while (true) {
try {
core.info(`Creating GitHub Pages deployment, attempt ${attempt}.`);
const deployment = await createDeployment(artifact.id);
const deploymentId = deployment.id || deployment.status_url?.split('/').pop();
if (!deploymentId) {
throw new Error('GitHub Pages deployment did not return an id.');
}
if (deployment.page_url) {
core.setOutput('page_url', deployment.page_url);
}
core.info(`Created deployment ${deploymentId}.`);
if (await waitForDeployment(deploymentId)) {
core.info('GitHub Pages deployment succeeded.');
return;
}
core.warning(`GitHub Pages deployment ${deploymentId} failed. Retrying in 5 seconds.`);
} catch (error) {
core.warning(`GitHub Pages deployment attempt ${attempt} failed: ${error.message}`);
}
attempt += 1;
await sleep(retryDelayMs);
}