|
1 | 1 | name: Publish Packages |
2 | 2 |
|
3 | | -# This workflow publishes packages to npm when changes are merged to main branch or when manually triggered. |
| 3 | +# Changesets opens or updates a version PR when release notes are present. Once that PR is merged, |
| 4 | +# this workflow publishes the versioned packages to npm. |
4 | 5 |
|
5 | 6 | on: |
6 | 7 | push: |
7 | | - paths: |
8 | | - - 'packages/**' |
9 | 8 | # For security reasons, this should never be set to anything but `main` |
10 | 9 | branches: [main] |
11 | | - workflow_dispatch: |
12 | | - inputs: |
13 | | - package: |
14 | | - description: 'Specific package to publish (leave empty for all packages)' |
15 | | - required: false |
16 | | - type: string |
17 | 10 |
|
18 | 11 | permissions: |
19 | 12 | contents: read |
|
26 | 19 | COMMIT_SHA: ${{ github.sha }} |
27 | 20 |
|
28 | 21 | jobs: |
29 | | - prepare-packages: |
30 | | - name: Prepare Packages |
| 22 | + release: |
| 23 | + name: Create Release PR or Publish |
31 | 24 | runs-on: ubuntu-latest |
32 | | - outputs: |
33 | | - # Output the matrix of packages to publish for use in the publish job |
34 | | - matrix: ${{ steps.generate-matrix.outputs.matrix }} |
| 25 | + # Never attempt to publish from forks (no trusted-publisher match / secrets). |
| 26 | + if: github.repository == 'nodejs/nodejs.org' |
| 27 | + permissions: |
| 28 | + contents: write |
| 29 | + id-token: write |
| 30 | + pull-requests: write |
35 | 31 | steps: |
36 | 32 | - name: Harden Runner |
37 | 33 | uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4 |
@@ -62,83 +58,47 @@ jobs: |
62 | 58 |
|
63 | 59 | echo "✅ Commit is verified and trusted." |
64 | 60 |
|
65 | | - - name: Checkout repository |
66 | | - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
67 | | - with: |
68 | | - fetch-depth: 2 # Need at least 2 commits to detect changes between commits |
69 | | - persist-credentials: false |
70 | | - |
71 | | - - name: Generate package matrix |
72 | | - id: generate-matrix |
73 | | - env: |
74 | | - PACKAGE: ${{ github.event.inputs.package }} |
75 | | - EVENT_NAME: ${{ github.event_name }} |
76 | | - run: | |
77 | | - if [ -n "$PACKAGE" ]; then |
78 | | - # If a specific package is requested via workflow_dispatch, just publish that one |
79 | | - echo "matrix={\"package\":[\"$PACKAGE\"]}" >> $GITHUB_OUTPUT |
80 | | - else |
81 | | - CHANGED_PACKAGES=() |
82 | | - for pkg in $(ls -d packages/*); do |
83 | | - PKG_NAME=$(basename "$pkg") |
84 | | - PKG_JSON="$pkg/package.json" |
85 | | -
|
86 | | - # Determine if the package has changed (or include all on manual trigger) |
87 | | - if [ "$EVENT_NAME" == "workflow_dispatch" ] || ! git diff --quiet $COMMIT_SHA~1 $COMMIT_SHA -- "$pkg/"; then |
88 | | - OLD_VERSION=$(git show $COMMIT_SHA~1:$PKG_JSON | jq -r '.version') |
89 | | - NEW_VERSION=$(jq -r '.version' "$PKG_JSON") |
90 | | - if [ "$OLD_VERSION" != "$NEW_VERSION" ]; then |
91 | | - CHANGED_PACKAGES+=("$PKG_NAME") |
92 | | - fi |
93 | | - fi |
94 | | - done |
95 | | -
|
96 | | - # Format the output for GitHub Actions matrix using jq |
97 | | - PACKAGES_JSON=$(jq -n '$ARGS.positional' --args "${CHANGED_PACKAGES[@]}" -c) |
98 | | - echo "matrix={\"package\":$PACKAGES_JSON}" >> $GITHUB_OUTPUT |
99 | | - fi |
100 | | -
|
101 | | - publish: |
102 | | - name: Publish |
103 | | - needs: prepare-packages |
104 | | - runs-on: ubuntu-latest |
105 | | - permissions: |
106 | | - # Required for npm OIDC publishing (https://docs.npmjs.com/trusted-publishers) |
107 | | - id-token: write |
108 | | - # Skip if no packages need to be published |
109 | | - if: fromJson(needs.prepare-packages.outputs.matrix).package[0] != null |
110 | | - # Use the dynamic matrix from prepare-packages job to create parallel jobs for each package |
111 | | - strategy: |
112 | | - matrix: ${{ fromJson(needs.prepare-packages.outputs.matrix) }} |
113 | | - fail-fast: false # Continue publishing other packages even if one fails |
114 | | - steps: |
115 | 61 | - uses: nodejs/web-team/actions/setup-environment@9f3c83af227d721768d9dbb63009a47ed4f4282f |
116 | 62 | with: |
117 | 63 | pnpm: true |
118 | 64 | use-version-file: true |
119 | 65 | registry-url: 'https://registry.npmjs.org' |
| 66 | + fetch-depth: 0 |
120 | 67 |
|
121 | | - - name: Publish |
122 | | - working-directory: packages/${{ matrix.package }} |
123 | | - run: | |
124 | | - # Check if a custom publish script exists in package.json |
125 | | - if jq -e '.scripts.release' package.json > /dev/null; then |
126 | | - pnpm run release |
127 | | - fi |
128 | | -
|
129 | | - # Then publish the package to npm |
130 | | - pnpm publish --access public --no-git-checks |
| 68 | + - name: Create release pull request or publish packages |
| 69 | + id: changesets |
| 70 | + uses: changesets/action@a45c4d594aa4e2c509dc14a9f2b3b67ba3780d0d # v1.9.0 |
| 71 | + with: |
| 72 | + commit: 'chore: version packages' |
| 73 | + title: 'chore: version packages' |
| 74 | + version: node --run changeset:version |
| 75 | + publish: node --run release |
| 76 | + commitMode: github-api |
| 77 | + env: |
| 78 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
131 | 79 |
|
132 | | - - name: Notify on Manual Release |
133 | | - if: ${{ github.event_name == 'workflow_dispatch' }} |
| 80 | + - name: Format published packages |
| 81 | + if: steps.changesets.outputs.published == 'true' |
| 82 | + id: notification |
| 83 | + env: |
| 84 | + PUBLISHED_PACKAGES: ${{ steps.changesets.outputs.publishedPackages }} |
| 85 | + run: | |
| 86 | + { |
| 87 | + echo 'packages<<EOF' |
| 88 | + jq -r '.[] | ":package: *Package*: `\(.name)@\(.version)` (<https://www.npmjs.com/package/\(.name)/v/\(.version)|View on npm>)"' <<< "$PUBLISHED_PACKAGES" |
| 89 | + echo 'EOF' |
| 90 | + } >> "$GITHUB_OUTPUT" |
| 91 | +
|
| 92 | + - name: Notify |
| 93 | + if: steps.changesets.outputs.published == 'true' |
134 | 94 | uses: rtCamp/action-slack-notify@33ca3be66c6f378fe1610fd1d5258632dbed5e58 # v2.4.0 |
135 | 95 | env: |
136 | 96 | SLACK_COLOR: '#43853D' |
137 | 97 | SLACK_ICON: https://github.com/nodejs.png?size=48 |
138 | | - SLACK_TITLE: ':rocket: Package Published: ${{ matrix.package }}' |
| 98 | + SLACK_TITLE: ':rocket: Packages Published' |
139 | 99 | SLACK_MESSAGE: | |
140 | | - :package: *Package*: `${{ matrix.package }}` (<https://www.npmjs.com/package/@node-core/${{ matrix.package }}|View on npm>) |
| 100 | + ${{ steps.notification.outputs.packages }} |
141 | 101 | :bust_in_silhouette: *Published by*: ${{ github.triggering_actor }} |
142 | | - :octocat: *Commit*: <https://github.com/${{ github.repository }}/commit/${{ env.COMMIT_SHA }}|${{ env.COMMIT_SHA }}> |
| 102 | + :octocat: *Commit*: <https://github.com/${{ github.repository }}/commit/${{ github.sha }}|${{ github.sha }}> |
143 | 103 | SLACK_USERNAME: nodejs-bot |
144 | 104 | SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} |
0 commit comments