Files
hypit/.github/workflows/publish-npm.yml
2026-10-09 19:05:00 +08:00

114 lines
4.6 KiB
YAML

name: Publish npm
run-name: "npm ${{ github.event.release.tag_name || inputs.candidate_run }} — ${{ (github.event_name == 'release' || inputs.publish) && 'publish' || 'inspect candidate' }}"
on:
release:
types: [published]
workflow_dispatch:
inputs:
candidate_run:
description: "Successful main CI run ID containing the tested npm-package artifact"
type: string
required: true
publish:
description: "Publish the verified artifact to npm (unchecked: inspect only)"
type: boolean
default: false
packages:
description: "Optional space-separated package names; empty publishes the complete candidate"
type: string
required: false
permissions:
contents: read
actions: read
concurrency:
group: publish-npm
cancel-in-progress: false
jobs:
publish:
if: github.event_name == 'release' || github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: write
actions: read
id-token: write
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
ref: ${{ github.sha }}
fetch-depth: 0
persist-credentials: false
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: 24
registry-url: https://registry.npmjs.org
- name: Select a successful main CI candidate
id: candidate
env:
GH_TOKEN: ${{ github.token }}
CANDIDATE_RUN: ${{ inputs.candidate_run }}
RELEASE_PRERELEASE: ${{ github.event.release.prerelease }}
run: node scripts/select-release-run.mjs
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
with:
name: npm-package
path: release
run-id: ${{ steps.candidate.outputs.run_id }}
github-token: ${{ github.token }}
- name: Confirm candidate version and package selection
id: version
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
RELEASE_PACKAGES: ${{ inputs.packages }}
run: |
node --input-type=module <<'JS'
import { readFileSync, appendFileSync } from 'node:fs';
import { selectReleasePackages } from './scripts/release-candidate.mjs';
const plan = JSON.parse(readFileSync('release/release-plan.json', 'utf8'));
const version = plan.distribution?.version;
if (plan.format !== 'hypit.release-plan@1' || !/^\d+\.\d+\.\d+$/.test(version ?? '')) {
throw new Error('Candidate must declare a stable npm version.');
}
if (process.env.RELEASE_TAG && process.env.RELEASE_TAG !== `v${version}`) {
throw new Error('Release tag does not match the tested candidate version.');
}
const names = (process.env.RELEASE_PACKAGES ?? '').trim().split(/\s+/).filter(Boolean);
selectReleasePackages(plan, names);
appendFileSync(process.env.GITHUB_OUTPUT, `version=${version}\n`);
JS
- name: Use npm with trusted publishing support
if: github.event_name == 'release' || inputs.publish
run: npm install --global npm@11
- name: Inspect or publish the selected immutable packages
env:
RELEASE_PACKAGES: ${{ inputs.packages }}
PUBLISH: ${{ github.event_name == 'release' || inputs.publish }}
run: |
node --input-type=module <<'JS'
import { execFileSync } from 'node:child_process';
const names = (process.env.RELEASE_PACKAGES ?? '').trim().split(/\s+/).filter(Boolean);
execFileSync(process.execPath, [
'scripts/publish-release-candidate.mjs', 'release/release-plan.json',
...names.map(name => `--package=${name}`),
...(process.env.PUBLISH === 'true' ? [] : ['--preflight-only']),
], { stdio: 'inherit' });
JS
- name: Attach the tested root package to the GitHub Release
if: github.event_name == 'release'
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
VERSION: ${{ steps.version.outputs.version }}
run: |
filename="hypit-hypit-$VERSION.tgz"
assets="$(gh release view "$RELEASE_TAG" --json assets --jq '.assets[].name')"
if printf '%s\n' "$assets" | grep -Fxq "$filename"; then
echo "Release already contains $filename; leaving it unchanged."
else
gh release upload "$RELEASE_TAG" "release/$filename"
fi