Skip to content

Commit 00efe6f

Browse files
committed
Set up trusted publishing
1 parent a01ed9a commit 00efe6f

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,64 @@ jobs:
1717
node-version: ${{ matrix.node-version }}
1818

1919
- run: npm install --no-package-lock
20-
- run: npm test
20+
- run: npm test
21+
22+
publish:
23+
name: Publish to npm
24+
needs: build
25+
if: startsWith(github.ref, 'refs/tags/v')
26+
runs-on: ubuntu-latest
27+
environment:
28+
name: npm
29+
url: https://www.npmjs.com/package/http-encoding
30+
permissions:
31+
contents: read
32+
id-token: write
33+
34+
steps:
35+
- uses: actions/checkout@v4
36+
37+
- uses: actions/setup-node@v4
38+
with:
39+
node-version: 'v22.20.0'
40+
registry-url: 'https://registry.npmjs.org'
41+
cache: 'npm'
42+
cache-dependency-path: 'package.json'
43+
44+
- run: npm install
45+
- run: npm run build
46+
47+
- name: Verify tag matches package.json version
48+
id: version-check
49+
run: |
50+
TAG_VERSION=${GITHUB_REF#refs/tags/v}
51+
PACKAGE_VERSION=$(node -p "require('./package.json').version")
52+
if [ "$TAG_VERSION" != "$PACKAGE_VERSION" ]; then
53+
echo "Error: Tag version (v$TAG_VERSION) does not match package.json version ($PACKAGE_VERSION)"
54+
exit 1
55+
fi
56+
echo "✓ Tag version matches package.json version: $PACKAGE_VERSION"
57+
58+
# Check if version matches strict X.Y.Z format (stable release)
59+
if echo "$PACKAGE_VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then
60+
echo "Stable release version detected: $PACKAGE_VERSION"
61+
echo "is_prerelease=false" >> $GITHUB_OUTPUT
62+
else
63+
echo "Prerelease version detected: $PACKAGE_VERSION"
64+
echo "is_prerelease=true" >> $GITHUB_OUTPUT
65+
fi
66+
67+
# Make sure we have the latest npm for publishing:
68+
- run: npm install -g npm@latest
69+
70+
- name: Publish to npm
71+
run: |
72+
if [ "${{ steps.version-check.outputs.is_prerelease }}" == "true" ]; then
73+
echo "Publishing untagged prerelease"
74+
npm publish --provenance --tag test
75+
# We have to publish with a tag (so we use 'test') but we can clean it up:
76+
npm dist-tag rm http-encoding test --silent
77+
else
78+
echo "Publishing stable release with 'latest' tag"
79+
npm publish --provenance
80+
fi

0 commit comments

Comments
 (0)