Skip to content

release 工作流

release 工作流 #49

Workflow file for this run

name: Deploy Pages
on:
push:
branches: [main]
paths:
- 'scripts/**'
- 'package.json'
- 'pnpm-lock.yaml'
- 'pnpm-workspace.yaml'
- 'packages/player/**'
- 'packages/web/**'
- '.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
- run: pnpm install --frozen-lockfile
- run: pnpm --filter @nes-emulator/web run build
- uses: actions/configure-pages@v5
- uses: actions/upload-pages-artifact@v3
with:
path: packages/web/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);
}