Repository navigation
97 lines (92 loc) · 3.07 KB
/
Copy pathpublish.yml
File metadata and controls
97 lines (92 loc) · 3.07 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
name: Publish to npm
on:
release:
types: [published]
workflow_dispatch:
inputs:
dry_run:
description: Run preflight only (build, test, pack, npm publish --dry-run - no upload)
type: boolean
default: false
# One publish at a time; never cancel a release mid-upload.
concurrency:
group: publish-${{ github.ref }}
cancel-in-progress: false
permissions:
contents: read
jobs:
# ---------------------------------------------------------------------
# Gate. Runs the ENTIRE publish path without uploading. If anything here
# fails, `publish` never runs, so a release can never half-publish.
# ---------------------------------------------------------------------
preflight:
name: Preflight (dry-run)
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
cache: npm
- name: Ensure NPM_TOKEN is configured
shell: bash
run: |
if [ -z "${NODE_AUTH_TOKEN:-}" ]; then
echo "::error::NPM_TOKEN secret is not set for this repository."
exit 1
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- run: npm ci
- run: npm run build
- run: npm test
- name: Guard - release tag matches package.json version
if: github.event_name == 'release'
shell: bash
run: |
pkg="$(node -p "require('./package.json').version")"
tag="${GITHUB_REF_NAME#v}"
echo "package.json=$pkg release tag=$tag"
if [ "$pkg" != "$tag" ]; then
echo "::error::Version mismatch: package.json ($pkg) != release tag ($tag)"
exit 1
fi
- name: Guard - version is not already published
shell: bash
run: |
name="$(node -p "require('./package.json').name")"
ver="$(node -p "require('./package.json').version")"
if npm view "${name}@${ver}" version >/dev/null 2>&1; then
echo "::error::${name}@${ver} is already published - bump the version first."
exit 1
fi
echo "OK: ${name}@${ver} is clear to publish."
- name: Dry-run publish (validates tarball, files, lifecycle scripts)
run: npm publish --access public --dry-run
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
publish:
name: Publish
needs: preflight
if: github.event.inputs.dry_run != 'true'
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # required for npm provenance (--provenance)
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
cache: npm
- run: npm ci
- run: npm run build
- name: Publish to npm
run: npm publish --access public --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}