How to Version and Publish a Monorepo With Changesets in GitHub Actions
changesets/action opens a Version Packages PR from pending changesets and publishes to npm when that PR merges.
Run changesets/action on push to main. It bumps versions and opens a release PR; merging that PR triggers the same action to run publish against npm.
Steps
- Add
permissions: contents: writeandpull-requests: write. - Run
changesets/actionwith apublish:command. - Provide
GITHUB_TOKENandNPM_TOKEN.
Workflow
.github/workflows/release.yml
on:
push:
branches: [main]
permissions:
contents: write
pull-requests: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- uses: changesets/action@v1
with:
publish: npm run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}Gotchas
- Set npm provenance or auth via
.npmrc; the action does not log in for you. - The release PR only appears once at least one changeset file exists on main.
Related guides
How to Enforce Package Boundaries in a Monorepo With GitHub ActionsFail CI when a package imports across forbidden boundaries using an Nx module boundary lint rule or eslint-pl…
How to Run Codegen Once and Share It Across Jobs in GitHub ActionsGenerate code once in a GitHub Actions job and hand the output to downstream package jobs with upload-artifac…