Skip to content

Commit a248cbc

Browse files
committed
feat: support per sub-package npm publish for mono-repo
1 parent 3db3c17 commit a248cbc

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

.github/workflows/reusable-publish-release.yml

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ on:
3434
type: string
3535
default: ''
3636

37+
package-overrides:
38+
description: 'JSON object mapping mono-repo sub-package to its specific inputs.'
39+
required: false
40+
type: string
41+
default: '{}'
42+
3743
secrets:
3844
ACCESS_TOKEN:
3945
required: true
@@ -73,11 +79,14 @@ jobs:
7379
ORIGINAL_TAG_NAME="${PACKAGE_NAME}/${VERSION_TAG}"
7480
else
7581
# Monolith mode: label format is v<semver>.
82+
PACKAGE_NAME=""
7683
RELEASE_VERSION="${RELEASE_LABEL#v}"
7784
RELEASE_TITLE="${RELEASE_VERSION}"
7885
ORIGINAL_TAG_NAME="${RELEASE_LABEL}"
7986
fi
8087
88+
printf 'package_name=%s\n' "$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
89+
8190
TAG_COMMIT_HASH=$(git rev-list -n 1 "$ORIGINAL_TAG_NAME")
8291
8392
if [ -z "$TAG_COMMIT_HASH" ]; then
@@ -116,6 +125,38 @@ jobs:
116125
117126
printf 'enabled=true\n' >> "$GITHUB_OUTPUT"
118127
128+
- name: 📝 Resolve NPM Config
129+
id: resolve_npm_config
130+
if: steps.verify_npm_token.outputs.enabled == 'true'
131+
env:
132+
PACKAGE_NAME: ${{ steps.extract_tag.outputs.package_name }}
133+
PACKAGE_OVERRIDES: ${{ inputs.package-overrides }}
134+
DEFAULT_NODE_VERSION: ${{ inputs.npm-node-version }}
135+
DEFAULT_PACKAGE_DIR: ${{ inputs.npm-package-dir }}
136+
DEFAULT_DEPLOY_COMMAND: ${{ inputs.npm-deploy-command }}
137+
run: |
138+
NODE_VERSION="$DEFAULT_NODE_VERSION"
139+
PACKAGE_DIR="$DEFAULT_PACKAGE_DIR"
140+
DEPLOY_COMMAND="$DEFAULT_DEPLOY_COMMAND"
141+
142+
if [ -n "$PACKAGE_NAME" ]; then
143+
OVERRIDE=$(printf '%s' "$PACKAGE_OVERRIDES" | jq -c --arg pkg "$PACKAGE_NAME" '.[$pkg] // empty')
144+
145+
if [ -n "$OVERRIDE" ]; then
146+
OVERRIDE_NODE_VERSION=$(printf '%s' "$OVERRIDE" | jq -r '."npm-node-version" // empty')
147+
OVERRIDE_PACKAGE_DIR=$(printf '%s' "$OVERRIDE" | jq -r '."npm-package-dir" // empty')
148+
OVERRIDE_DEPLOY_COMMAND=$(printf '%s' "$OVERRIDE" | jq -r '."npm-deploy-command" // empty')
149+
150+
[ -n "$OVERRIDE_NODE_VERSION" ] && NODE_VERSION="$OVERRIDE_NODE_VERSION"
151+
[ -n "$OVERRIDE_PACKAGE_DIR" ] && PACKAGE_DIR="$OVERRIDE_PACKAGE_DIR"
152+
[ -n "$OVERRIDE_DEPLOY_COMMAND" ] && DEPLOY_COMMAND="$OVERRIDE_DEPLOY_COMMAND"
153+
fi
154+
fi
155+
156+
printf 'npm_node_version=%s\n' "$NODE_VERSION" >> "$GITHUB_OUTPUT"
157+
printf 'npm_package_dir=%s\n' "$PACKAGE_DIR" >> "$GITHUB_OUTPUT"
158+
printf 'npm_deploy_command=%s\n' "$DEPLOY_COMMAND" >> "$GITHUB_OUTPUT"
159+
119160
- name: 🔍 Verify TRB Inputs
120161
id: verify_trb_inputs
121162
run: |
@@ -147,9 +188,9 @@ jobs:
147188
if: steps.verify_npm_token.outputs.enabled == 'true'
148189
uses: ./.release-workflow/.github/actions/npm-publish
149190
with:
150-
node-version: ${{ inputs.npm-node-version }}
151-
package-dir: ${{ inputs.npm-package-dir }}
152-
deploy-command: ${{ inputs.npm-deploy-command }}
191+
node-version: ${{ steps.resolve_npm_config.outputs.npm_node_version }}
192+
package-dir: ${{ steps.resolve_npm_config.outputs.npm_package_dir }}
193+
deploy-command: ${{ steps.resolve_npm_config.outputs.npm_deploy_command }}
153194
access-token: ${{ secrets.NPM_TOKEN }}
154195

155196
- name: 🚀 Bump TRB Project Node Version

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ This workflow also releases individual sub-packages inside a mono-repo. Each sub
8787

8888
The **workspace** is the work directory that binds a sub-package's changes. Only commits that touch files inside it are considered for that sub-package's changelog.
8989

90+
Likewise, sub-packages that need different publish settings are configured via the `package-overrides` input in your user-side entry workflow (`.github/workflows/publish-release.yml`), the same way as `packages`:
91+
92+
```yaml
93+
jobs:
94+
call-publish:
95+
uses: leoweyr/github-release-workflow/.github/workflows/reusable-publish-release.yml@develop
96+
with:
97+
package-overrides: |
98+
{
99+
"core": {
100+
"npm-package-dir": "packages/core"
101+
},
102+
"cli": {
103+
"npm-package-dir": "packages/cli"
104+
}
105+
}
106+
secrets:
107+
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
108+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
109+
```
110+
90111
2. **Tag a sub-package release**
91112

92113
Use a tag in the form `<package>/v<semver>`:
@@ -105,4 +126,4 @@ How it differs from a monolith release:
105126
| Changelog Commit Range | Latest commit → previous `v*` tag | Latest commit → previous `core/v*` tag |
106127
| Commit Filtering | All conventional commits | Only commits that modified files inside the sub-package workspace |
107128
| Changelog Location | `CHANGELOG.md` (project root) | `packages/core/CHANGELOG.md` (inside the workspace) |
108-
| GitHub Release Title | `1.0.0` | `core@v1.0.0` |
129+
| GitHub Release Title | `1.0.0` | `core@v1.0.0` |

0 commit comments

Comments
 (0)