|
1 | 1 | name: Node.js Package |
2 | 2 |
|
| 3 | +# This workflow runs only when a tag starting with "v" is pushed (e.g., v1.0.0) |
3 | 4 | on: |
4 | 5 | push: |
5 | 6 | tags: |
6 | | - - "v*" # Trigger when a version tag like v1.0.0 is pushed |
| 7 | + - "v*" |
7 | 8 |
|
8 | 9 | jobs: |
9 | 10 | build: |
10 | 11 | runs-on: ubuntu-latest |
11 | 12 | steps: |
12 | | - - uses: actions/checkout@v4 |
| 13 | + - name: Checkout repository |
| 14 | + uses: actions/checkout@v4 |
13 | 15 |
|
14 | | - - uses: actions/setup-node@v4 |
| 16 | + - name: Set up Node.js 20 |
| 17 | + uses: actions/setup-node@v4 |
15 | 18 | with: |
16 | 19 | node-version: 20 |
17 | 20 |
|
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." |
20 | 28 |
|
21 | 29 | publish-npm: |
| 30 | + name: Publish to npm |
| 31 | + # This job only runs after the 'build' job successfully completes |
22 | 32 | needs: build |
23 | 33 | runs-on: ubuntu-latest |
24 | 34 | 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 |
28 | 40 | with: |
29 | 41 | node-version: 20 |
| 42 | + # Sets up the registry for publishing and adds .npmrc for authentication |
30 | 43 | registry-url: https://registry.npmjs.org/ |
31 | 44 |
|
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 |
34 | 50 | env: |
| 51 | + # This secret is needed to authenticate with npm |
35 | 52 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
36 | 53 |
|
37 | 54 | release: |
| 55 | + name: Create GitHub Release and Upload Artifact |
| 56 | + # This job only runs after the 'build' job successfully completes |
38 | 57 | needs: build |
39 | 58 | runs-on: ubuntu-latest |
40 | 59 | steps: |
41 | | - - uses: actions/checkout@v4 |
| 60 | + - name: Checkout repository |
| 61 | + uses: actions/checkout@v4 |
42 | 62 |
|
| 63 | + # Creates the custom build artifact .tgz file |
43 | 64 | - name: Create versioned release tarball |
44 | 65 | run: | |
| 66 | + # Extract the version (tag name) from GITHUB_REF (e.g., 'v1.0.0') |
45 | 67 | VERSION=${GITHUB_REF#refs/tags/} |
| 68 | + # Extract the repository name (e.g., 'my-repo') |
46 | 69 | REPO_NAME=$(basename $GITHUB_REPOSITORY) |
| 70 | +
|
47 | 71 | mkdir -p release |
48 | | - tar -czf release/${REPO_NAME}-${VERSION}.tgz dist/ || tar -czf release/${REPO_NAME}-${VERSION}.tgz . |
49 | 72 |
|
| 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 |
50 | 79 | - name: Upload versioned release tarball |
51 | 80 | uses: softprops/action-gh-release@v1 |
52 | 81 | with: |
| 82 | + # Automatically uses the tag that triggered the workflow |
| 83 | + # Uses the custom tarball generated in the previous step |
53 | 84 | files: release/${{ github.event.repository.name }}-${{ github.ref_name }}.tgz |
| 85 | + # The body will contain a list of changes |
| 86 | + generate_release_notes: true |
54 | 87 | env: |
| 88 | + # This token is automatically available and needed for GitHub API access |
55 | 89 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments