Skip to content

Commit bd3aabc

Browse files
Chore: Added build script
1 parent 11af4fc commit bd3aabc

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

.github/workflows/npm-publish.yml

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,89 @@
11
name: Node.js Package
22

3+
# This workflow runs only when a tag starting with "v" is pushed (e.g., v1.0.0)
34
on:
45
push:
56
tags:
6-
- "v*" # Trigger when a version tag like v1.0.0 is pushed
7+
- "v*"
78

89
jobs:
910
build:
1011
runs-on: ubuntu-latest
1112
steps:
12-
- uses: actions/checkout@v4
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
1315

14-
- uses: actions/setup-node@v4
16+
- name: Set up Node.js 20
17+
uses: actions/setup-node@v4
1518
with:
1619
node-version: 20
1720

18-
- run: npm ci
19-
- run: npm run build || echo "No build step defined"
21+
- name: Install dependencies
22+
run: npm ci
23+
24+
- name: Run build script (if defined)
25+
# The '|| echo' allows the workflow to pass even if the project
26+
# does not have a 'build' script defined in package.json
27+
run: npm run build || echo "No build step defined, skipping."
2028

2129
publish-npm:
30+
name: Publish to npm
31+
# This job only runs after the 'build' job successfully completes
2232
needs: build
2333
runs-on: ubuntu-latest
2434
steps:
25-
- uses: actions/checkout@v4
26-
27-
- uses: actions/setup-node@v4
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
38+
- name: Set up Node.js 20 for npm publishing
39+
uses: actions/setup-node@v4
2840
with:
2941
node-version: 20
42+
# Sets up the registry for publishing and adds .npmrc for authentication
3043
registry-url: https://registry.npmjs.org/
3144

32-
- run: npm ci
33-
- run: npm publish
45+
- name: Install dependencies
46+
run: npm ci
47+
48+
- name: Publish package
49+
run: npm publish
3450
env:
51+
# This secret is needed to authenticate with npm
3552
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3653

3754
release:
55+
name: Create GitHub Release and Upload Artifact
56+
# This job only runs after the 'build' job successfully completes
3857
needs: build
3958
runs-on: ubuntu-latest
4059
steps:
41-
- uses: actions/checkout@v4
60+
- name: Checkout repository
61+
uses: actions/checkout@v4
4262

63+
# Creates the custom build artifact .tgz file
4364
- name: Create versioned release tarball
4465
run: |
66+
# Extract the version (tag name) from GITHUB_REF (e.g., 'v1.0.0')
4567
VERSION=${GITHUB_REF#refs/tags/}
68+
# Extract the repository name (e.g., 'my-repo')
4669
REPO_NAME=$(basename $GITHUB_REPOSITORY)
70+
4771
mkdir -p release
48-
tar -czf release/${REPO_NAME}-${VERSION}.tgz dist/ || tar -czf release/${REPO_NAME}-${VERSION}.tgz .
4972
73+
# Attempt to tar the 'dist/' folder first, or fall back to the root if 'dist/' doesn't exist
74+
# This creates a file like 'release/my-repo-v1.0.0.tgz'
75+
echo "Creating ${REPO_NAME}-${VERSION}.tgz..."
76+
tar -czf release/${REPO_NAME}-${VERSION}.tgz dist/ 2>/dev/null || tar -czf release/${REPO_NAME}-${VERSION}.tgz .
77+
78+
# Creates the release and uploads the custom artifact
5079
- name: Upload versioned release tarball
5180
uses: softprops/action-gh-release@v1
5281
with:
82+
# Automatically uses the tag that triggered the workflow
83+
# Uses the custom tarball generated in the previous step
5384
files: release/${{ github.event.repository.name }}-${{ github.ref_name }}.tgz
85+
# The body will contain a list of changes
86+
generate_release_notes: true
5487
env:
88+
# This token is automatically available and needed for GitHub API access
5589
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)