|
1 |
| -name: Publish to NPM |
| 1 | +name: Publish NPM and Release (on merged PR) |
2 | 2 |
|
3 | 3 | on:
|
4 |
| - workflow_dispatch: |
5 |
| - inputs: |
6 |
| - semver: |
7 |
| - description: "The semantic version to bump" |
8 |
| - required: true |
9 |
| - type: choice |
10 |
| - options: |
11 |
| - - patch |
12 |
| - - minor |
13 |
| - - major |
14 |
| - default: "patch" |
15 |
| - nodeVersion: |
16 |
| - description: "The Node.js version to use" |
17 |
| - required: true |
18 |
| - type: choice |
19 |
| - options: |
20 |
| - - "18.x" |
21 |
| - - "20.x" |
22 |
| - - "22.x" |
23 |
| - default: "18.x" |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - closed |
24 | 7 |
|
25 | 8 | jobs:
|
26 | 9 | release:
|
| 10 | + if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'master' }} |
27 | 11 | permissions:
|
28 | 12 | contents: write
|
| 13 | + packages: write |
29 | 14 | id-token: write
|
30 | 15 |
|
31 |
| - # Use the semver as the job name |
32 |
| - name: "Release ${{ github.event.inputs.semver }}" |
| 16 | + name: Publish on master (patch) |
33 | 17 | runs-on: ubuntu-latest
|
| 18 | + concurrency: |
| 19 | + group: publish-pr-${{ github.event.pull_request.number }} |
| 20 | + cancel-in-progress: true |
34 | 21 | steps:
|
35 | 22 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
|
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + persist-credentials: true |
36 | 26 |
|
37 | 27 | - uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610
|
38 | 28 | with:
|
39 |
| - node-version: ${{ github.event.inputs.nodeVersion }} |
40 |
| - registry-url: "https://registry.npmjs.org" |
| 29 | + node-version: '22' |
| 30 | + registry-url: 'https://registry.npmjs.org' |
| 31 | + |
| 32 | + - name: Install dependencies |
| 33 | + run: npm ci |
| 34 | + |
| 35 | + - name: Run tests |
| 36 | + run: npm test |
41 | 37 |
|
42 |
| - - name: Bump and commit version |
| 38 | + - name: Compute patch version, update package.json locally, tag and push |
| 39 | + env: |
| 40 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
43 | 41 | run: |
|
44 |
| - git config --global user.email "github-actions[bot]@github.com" |
45 |
| - git config --global user.name "github-actions[bot]" |
46 |
| - npm version ${{ github.event.inputs.semver }} --message "chore(release): bump version to %s" |
47 |
| - git push --follow-tags |
| 42 | + set -euo pipefail |
| 43 | + git config user.email "github-actions[bot]@github.com" |
| 44 | + git config user.name "github-actions[bot]" |
48 | 45 |
|
49 |
| - - name: Publish |
50 |
| - run: npm publish |
| 46 | + # Read current version |
| 47 | + CURR=$(node -p "require('./package.json').version") |
| 48 | + echo "Current version: $CURR" |
51 | 49 |
|
52 |
| - - name: Set version as env var |
| 50 | + # Bump package.json locally without creating a git tag or commit |
| 51 | + npm version patch --no-git-tag-version -m "chore(release): bump version to %s" |
| 52 | +
|
| 53 | + # New version from updated package.json |
| 54 | + VERSION=$(node -p "require('./package.json').version") |
| 55 | + TAG="v${VERSION}" |
| 56 | + echo "Bumped to version: $VERSION" |
| 57 | +
|
| 58 | + # Create annotated tag from HEAD and push only the tag (do not push commits to protected branch) |
| 59 | + git tag -a "$TAG" -m "chore(release): $TAG" |
| 60 | + git push origin "$TAG" |
| 61 | +
|
| 62 | + - name: Publish to npm (OIDC) |
53 | 63 | run: |
|
54 |
| - echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV |
| 64 | + npm publish |
| 65 | +
|
| 66 | + - name: Set version as env var |
| 67 | + run: echo "VERSION=$(node -p \"require('./package.json').version\")" >> $GITHUB_ENV |
55 | 68 |
|
56 |
| - - uses: softprops/action-gh-release@aec2ec56f94eb8180ceec724245f64ef008b89f5 |
57 |
| - name: Release |
| 69 | + - name: Create GitHub release |
| 70 | + uses: softprops/action-gh-release@aec2ec56f94eb8180ceec724245f64ef008b89f5 |
58 | 71 | with:
|
| 72 | + tag_name: v${{ env.VERSION }} |
59 | 73 | name: Release ${{ env.VERSION }}
|
0 commit comments