Changesets: Tags, Pre Mode & CI
The previous lesson versioned packages. This one finishes the release: tagging each package, pre-releases for a whole monorepo, throw-away snapshot versions for testing a pull request, and the GitHub Actions setup that turns all of it into a reviewed "Version Packages" pull request.
Per-package tags
In a monorepo one tag per release isn't enough: @acme/core and @acme/cli have independent versions. changeset git-tag creates an annotated tag <name>@<version> for every package whose current version isn't tagged yet (changeset publish does the same after publishing):
mkdir mono && cd mono && git init -q
echo '{ "name": "mono", "private": true, "workspaces": ["packages/*"] }' > package.json
mkdir -p packages/core packages/cli .changeset
echo '{ "name": "@acme/core", "version": "1.3.0" }' > packages/core/package.json
echo '{ "name": "@acme/cli", "version": "0.4.2", "dependencies": { "@acme/core": "^1.3.0" } }' > packages/cli/package.json
echo '{ "baseBranch": "main", "changelog": "@changesets/cli/changelog", "commit": false, "updateInternalDependencies": "patch", "access": "restricted", "fixed": [], "linked": [], "ignore": [] }' > .changeset/config.json
npm install --package-lock-only --offline >/dev/null
git add -A && git commit -q -m "chore: version packages"
changeset git-tag 2>&1 | sed 's/\x1b\[?25[lh]//g'
git for-each-ref refs/tags --format='%(refname:short) %(objecttype)'
echo "describe for core: $(git describe --match '@acme/core@*')"🦋 changeset v3.0.3
◇ Created git tags:
- @acme/cli@0.4.2
- @acme/core@1.3.0
@acme/cli@0.4.2 tag
@acme/core@1.3.0 tag
describe for core: @acme/core@1.3.0
git describe --match '@acme/core@*' scopes describe to one package's tags, the same trick as --match 'v[0-9]*' in git describe & Version Strings. Other tools use other per-package formats (core-v1.3.0, core/v1.3.0); Go modules in subdirectories require the core/v1.3.0 form (Go Modules in Depth).
Pre mode
A pre-release across the monorepo (say, a next line for the upcoming major) uses pre mode. While it's on, changeset version produces -next.N versions. .changeset/pre.json records the mode and tag, and consumed changesets are moved into .changeset/pre/ instead of being deleted, because the final release still needs their text:
mkdir mono && cd mono && git init -q
echo '{ "name": "mono", "private": true, "workspaces": ["packages/*"] }' > package.json
mkdir -p packages/core .changeset
echo '{ "name": "@acme/core", "version": "1.3.0" }' > packages/core/package.json
echo '{ "baseBranch": "main", "changelog": "@changesets/cli/changelog", "commit": false, "updateInternalDependencies": "patch", "access": "restricted", "fixed": [], "linked": [], "ignore": [] }' > .changeset/config.json
npm install --package-lock-only --offline >/dev/null
git add -A && git commit -q -m "chore: init"
v() { changeset version >/dev/null; echo " core is now $(jq -r .version packages/core/package.json)"; }
changeset pre enter next | tail -n +2
printf -- '---\n"@acme/core": major\n---\n\nNew session API.\n' > .changeset/session-api.md
echo "major changeset:"; v
printf -- '---\n"@acme/core": patch\n---\n\nFix the session timeout.\n' > .changeset/session-timeout.md
echo "patch changeset:"; v
jq -c . .changeset/pre.json
echo "files: $(cd .changeset && find . -type f | sort | tr '\n' ' ')"
changeset pre exit | tail -n +2
echo "after pre exit:"; v
echo "files: $(cd .changeset && find . -type f | sort | tr '\n' ' ')"
sed -n '1,12p' packages/core/CHANGELOG.md
Entered pre mode with tag next!
Run changeset version to version packages with prerelease versions.
major changeset:
core is now 2.0.0-next.0
patch changeset:
core is now 2.0.0-next.1
{"mode":"pre","tag":"next"}
files: ./config.json ./pre.json ./pre/session-api.md ./pre/session-timeout.md
Exited pre mode!
Run changeset version to version packages with normal versions.
Please also review the changesets in the .changeset/pre folder as they will be used as changelogs for the normal versions. Only include the changesets that will be relevant for the normal versions.
after pre exit:
core is now 2.0.0
files: ./config.json
# @acme/core
## 2.0.0
### Major Changes
- New session API.
### Patch Changes
- Fix the session timeout.
The sequence is the same as semantic-release's beta channel (Pre-release Branches & Channels), done with files instead of branches: 2.0.0-next.0, 2.0.0-next.1, then 2.0.0 after pre exit. The pre/ folder kept the consumed changesets; after pre exit, the final version used them to write the full 2.0.0 changelog entry (major and patch changes together) and deleted them along with pre.json. Pre counters start at 0, like npm's.
Pre mode lives in a committed file, so it applies to the whole branch. Teams usually run it on a dedicated branch (next) so main can keep shipping normal releases.
Snapshot releases
To let someone test a pull request from a registry without "using up" a real version, --snapshot makes a throw-away version. The template decides its shape; {commit} makes it traceable:
mkdir mono && cd mono && git init -q
echo '{ "name": "mono", "private": true, "workspaces": ["packages/*"] }' > package.json
mkdir -p packages/core .changeset
echo '{ "name": "@acme/core", "version": "1.3.0" }' > packages/core/package.json
echo '{ "baseBranch": "main", "changelog": "@changesets/cli/changelog", "commit": false, "updateInternalDependencies": "patch", "access": "restricted", "fixed": [], "linked": [], "ignore": [] }' > .changeset/config.json
npm install --package-lock-only --offline >/dev/null
printf -- '---\n"@acme/core": patch\n---\n\nTry the new parser.\n' > .changeset/try-parser.md
git add -A && git commit -q -m "feat: try the new parser"
changeset version --snapshot pr-42 --snapshot-prerelease-template "{tag}-{commit}" | tail -1
jq -r .version packages/core/package.jsonAll files have been updated. Review them and commit at your leisure
0.0.0-pr-42-1d77a1dac844ad0e1708800a6aee143420bc2ce6
Snapshots are 0.0.0-… by default, so they sort below every real version and no range ever picks them by accident ("snapshot": { "useCalculatedVersion": true } bases them on the computed version instead). They are published under their own dist-tag (changeset publish --tag pr-42) and the version change is never committed.
Publishing
changeset publish runs npm publish (or pnpm's) for every package whose version isn't on the registry yet, then creates the git tags. It needs a registry, so it can't run in this offline image; the Publishing to a Registry lesson publishes to a local Verdaccio registry in the JS course's image. What it does, in order:
- for each non-private package, ask the registry whether
name@versionexists; - publish the ones that don't (
npm publish, with--tagfor pre-releases and snapshots, and--accessfrom the config); - create the
name@versiontags for what it published, which CI then pushes (git push --follow-tags, or the GitHub Action does it).
Because step 1 checks the registry, publish is idempotent: re-running it after a partial failure publishes only what is missing.
The "Version Packages" pull request
The official GitHub Action turns the workflow into a loop that never needs a human to type a version:
- on every push to
mainwith pending changesets, it runschangeset versionand opens (or updates) a pull request titled "Version Packages" containing the bumped versions and changelogs; - when that PR is merged, the push has no pending changesets, so it runs your publish script, which publishes and tags.
# .github/workflows/release.yml (non-run)
name: release
on:
push:
branches: [main]
concurrency: ${{ github.workflow }}-${{ github.ref }}
permissions:
contents: write # commit versions, push tags
pull-requests: write # open the Version Packages PR
id-token: write # npm trusted publishing (OIDC), no NPM_TOKEN needed
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
- run: npm ci
- uses: changesets/action@v2
with:
publish-script: npx changeset publish
create-github-releases: trueThe repository setting "Allow GitHub Actions to create and approve pull requests" must be on for the action to open the PR. The review step is the point of this design: the generated changelog and versions are a diff a maintainer reads, edits and merges, and branch protection on main stays intact.
Test yourself
What does changeset git-tag name the tag for version 1.3.0 of @acme/core?
- v1.3.0
- @acme/core@1.3.0
- core-1.3.0
- acme/core/v1.3.0
In pre mode with tag next, a major changeset is versioned on top of 1.3.0. What version results?
- 2.0.0
- 2.0.0-next.0
- 1.4.0-next.1
- 1.3.1-next.0
When does changesets/action publish instead of opening a PR?
- On every push
- When a push to main has no pending changesets, i.e. after the Version Packages PR was merged
- Only on tags
- On a schedule