Automate npm releases with semantic-release and human-written change logs
Making new releases is one of the most boring and tedious tasks in open source.
There are many tools that try to automate publishing and one of the most interesting is semantic-release. I was avoiding it for a long time because it makes publishing fully automated with changelogs generated from commit messages, and that I thought that changelogs must be written by humans.
But actually it’s very flexible and I was able to customize it to do exactly what I want:
Publish a new PATCH version to npm as soon as a fix commit merged to the master branch, generate changelog from commit messages.
Postpone MINOR and MAJOR release until a proper changelog is written by a project maintainer.
Generate changelog draft: Markdown file with all important commits since the latest release grouped into three sections: breaking changes, new features and bugfixes.
Below I’ll describe my own set of scripts that implements this workflow.
You can change semantic-release behavior with plugins: detect release type, check release requirements (like a changelog), generate changelog, and so on I made a package with all plugins I need to support my workflow.
First install the plugins:
Then add to your package.json:
Run npm install and npm run semantic-release to test if everything works. You’ll see something like that:
Which is fine and means two things: semantic-release will not make a release until it runs in a CI environment and you have no changes that could be published.
Type: Feat for new feature, Fix for bugfix, and so on
Subject: short change description.
Body (optional): long change description.
Footer (optional): breaking changes, GitHub issues references, and so on
Semantic-release uses this tags to find all important commits for the release (Fix is important, Docs is not) and determine which version (MAJOR, MINOR or PATCH) should be released.
First run sr-changelog. It will create a file with all important commits for the release grouped by type (breaking changes, new features and bugfixes) and open it in your default editor.
Now you can rewrite your changelog to make it useful and easy to read for your users.
Then run sr-changelog commit. It will make a commit without changes (git commit --allow-empty) of type Changelog and changelog in the commit message body.