Skip to content

Commit d5cfcb0

Browse files
committed
Set up trusted publishing
1 parent 87a98a3 commit d5cfcb0

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

.github/workflows/ci.yml

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,79 @@ jobs:
77

88
strategy:
99
matrix:
10-
node-version: [10.x, 12.x, 14.x, 16.x, '*']
10+
node-version: [10.x, 12.x, 14.x, 16.x, 22.x, '*']
1111

1212
steps:
13-
- uses: actions/checkout@v2
13+
- uses: actions/checkout@v4
1414

15-
- uses: actions/setup-node@v2
15+
- uses: actions/setup-node@v4
1616
with:
1717
node-version: ${{ matrix.node-version }}
18+
cache: 'npm'
19+
cache-dependency-path: 'package.json'
1820

1921
- uses: shivammathur/setup-php@v2
2022
with:
2123
php-version: '7.0'
2224

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

0 commit comments

Comments
 (0)