Skip to content

Commit d00ded7

Browse files
committed
refactor: streamline GitHub Actions workflow for NPM package creation and publishing
1 parent 703c4f0 commit d00ded7

File tree

1 file changed

+111
-107
lines changed

1 file changed

+111
-107
lines changed
Lines changed: 111 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,117 @@
11
name: Create NPM package
22
on:
3-
push:
4-
tags:
5-
- v0.**
6-
- v1.**
3+
push:
4+
tags:
5+
- v0.**
6+
- v1.**
77
jobs:
8-
preflight:
9-
name: "Preflight checks"
10-
runs-on: ubuntu-22.04
11-
steps:
12-
- name: Checkout Code
13-
uses: actions/checkout@v4
14-
- name: Setup Node
15-
uses: actions/setup-node@v4
16-
with:
17-
node-version: 22
18-
- name: "Check package.json version against tag"
19-
run: |
20-
# Extract the version from package.json
21-
PACKAGE_VERSION=$(node -p "require('./package.json').version")
8+
preflight:
9+
name: "Preflight checks"
10+
runs-on: ubuntu-22.04
11+
steps:
12+
- name: Checkout Code
13+
uses: actions/checkout@v4
14+
- name: Setup Node
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: 22
18+
- name: "Check package.json version against tag"
19+
run: |
20+
# Extract the version from package.json
21+
PACKAGE_VERSION=$(node -p "require('./package.json').version")
2222
23-
# Remove the 'releases/v' prefix from the Git tag to get the version
24-
TAG_VERSION=${GITHUB_REF_NAME#v}
23+
# Remove the 'releases/v' prefix from the Git tag to get the version
24+
TAG_VERSION=${GITHUB_REF_NAME#v}
2525
26-
# Compare the versions
27-
if [ "$PACKAGE_VERSION" != "$TAG_VERSION" ]; then
28-
echo "❌ Error: The version in package.json ($PACKAGE_VERSION) does not match the Git tag ($TAG_VERSION)."
29-
exit 1
30-
fi
31-
echo "✅ Success: package.json version matches the Git tag."
26+
# Compare the versions
27+
if [ "$PACKAGE_VERSION" != "$TAG_VERSION" ]; then
28+
echo "❌ Error: The version in package.json ($PACKAGE_VERSION) does not match the Git tag ($TAG_VERSION)."
29+
exit 1
30+
fi
31+
echo "✅ Success: package.json version matches the Git tag."
3232
33-
run-tests:
34-
name: "Run tests"
35-
runs-on: ubuntu-22.04
36-
needs: preflight
37-
services:
38-
mongodb:
39-
image: mongo:7.0
40-
env:
41-
MONGO_INITDB_DATABASE: "testdb"
42-
ports:
43-
- 37017:27017
44-
steps:
45-
- name: Checkout Code
46-
uses: actions/checkout@v4
47-
- name: Setup Node
48-
uses: actions/setup-node@v4
49-
with:
50-
node-version: 22
51-
- name: Clear npm cache
52-
run: npm cache clean --force
53-
- name: Configure .npmrc
54-
run: |
55-
echo "${{secrets.NPMRC}}" >> ~/.npmrc
56-
- name: Install packages
57-
run: npm install
58-
- name: Run tests
59-
run: npm run test
60-
publish:
61-
permissions:
62-
contents: write
63-
name: "Build and publish package"
64-
needs: run-tests
65-
runs-on: ubuntu-22.04
66-
steps:
67-
- name: Checkout Code
68-
uses: actions/checkout@v4
69-
env:
70-
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }}
71-
# IMPORTANT this is important to make sure that tokens are not reused in subsequent steps
72-
# -> removing this will cause the "GITHUB_TOKEN: ${{ secrets.MY_TOKEN }}" to
73-
# reuse the token from the previous step although defined otherwise
74-
with:
75-
persist-credentials: false
76-
- name: Setup Node
77-
uses: actions/setup-node@v4
78-
with:
79-
node-version: 22
80-
env:
81-
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }}
82-
- name: Clear npm cache
83-
run: npm cache clean --force
84-
- name: Configure .npmrc
85-
run: |
86-
echo "${{secrets.NPMRC}}" >> ~/.npmrc
87-
- name: Install packages
88-
run: npm install
89-
- name: NPM build package
90-
run: npm run build
91-
- name: Create release
92-
uses: actions/create-release@v1
93-
env:
94-
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }} # This token is provided by Actions, you do not need to create your own token
95-
with:
96-
tag_name: ${{ github.ref_name }}
97-
release_name: Release ${{ github.ref_name }}
98-
body: |
99-
Various fixes and performance improvements.
100-
draft: false
101-
prerelease: false
102-
- name: Setup Node for publishing to GitHub Package Registry
103-
uses: actions/setup-node@v4
104-
with:
105-
node-version: 22
106-
registry-url: https://npm.pkg.github.com/
107-
scope: '@air-avionics'
108-
env:
109-
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }}
110-
- name: Publish to GitHub Package Registry
111-
run: npm publish
112-
env:
113-
NODE_AUTH_TOKEN: ${{secrets.AUTOMATOR_DEVOPS_PAT}}
33+
run-tests:
34+
name: "Run tests"
35+
runs-on: ubuntu-22.04
36+
needs: preflight
37+
services:
38+
mongodb:
39+
image: mongo:7.0
40+
env:
41+
MONGO_INITDB_DATABASE: "testdb"
42+
ports:
43+
- 37017:27017
44+
steps:
45+
- name: Checkout Code
46+
uses: actions/checkout@v4
47+
- name: Setup Node
48+
uses: actions/setup-node@v4
49+
with:
50+
node-version: 22
51+
- name: Clear npm cache
52+
run: npm cache clean --force
53+
- name: Configure .npmrc
54+
run: |
55+
echo "${{secrets.NPMRC}}" >> ~/.npmrc
56+
- name: Install packages
57+
run: npm install
58+
- name: Run tests
59+
run: npm run test
60+
publish:
61+
permissions:
62+
contents: write
63+
name: "Build and publish package"
64+
needs: run-tests
65+
runs-on: ubuntu-22.04
66+
steps:
67+
- name: Checkout Code
68+
uses: actions/checkout@v4
69+
env:
70+
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }}
71+
# IMPORTANT this is important to make sure that tokens are not reused in subsequent steps
72+
# -> removing this will cause the "GITHUB_TOKEN: ${{ secrets.MY_TOKEN }}" to
73+
# reuse the token from the previous step although defined otherwise
74+
with:
75+
persist-credentials: false
76+
- name: Setup Node
77+
uses: actions/setup-node@v4
78+
with:
79+
node-version: 22
80+
env:
81+
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }}
82+
- name: Clear npm cache
83+
run: npm cache clean --force
84+
- name: Configure .npmrc
85+
run: |
86+
echo "${{secrets.NPMRC}}" >> ~/.npmrc
87+
- name: Install packages
88+
run: npm install
89+
- name: NPM build package
90+
run: npm run build
91+
- name: Create release
92+
uses: actions/create-release@v1
93+
env:
94+
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }} # This token is provided by Actions, you do not need to create your own token
95+
with:
96+
tag_name: ${{ github.ref_name }}
97+
release_name: Release ${{ github.ref_name }}
98+
body: |
99+
Various fixes and performance improvements.
100+
draft: false
101+
prerelease: false
102+
- name: Setup Node for publishing to GitHub Package Registry
103+
uses: actions/setup-node@v4
104+
with:
105+
node-version: 22
106+
registry-url: https://npm.pkg.github.com/
107+
scope: "@air-avionics"
108+
env:
109+
GITHUB_TOKEN: ${{ secrets.AUTOMATOR_DEVOPS_PAT }}
110+
- name: Publish to GitHub Package Registry
111+
run: npm publish
112+
env:
113+
NODE_AUTH_TOKEN: ${{secrets.AUTOMATOR_DEVOPS_PAT}}
114+
- name: Publish to NPM Package Registry
115+
run: npm publish --access public
116+
env:
117+
NODE_AUTH_TOKEN: ${{secrets.NPM_PUBLISH_TOKEN}}

0 commit comments

Comments
 (0)