Automating Versioning and Releases with semantic-release
Managing versions and releases in software development can often be a cumbersome, error-prone task. You have to follow a proper versioning system, ensure consistent release notes, and tag versions correctly—all while maintaining the momentum of your development process. Fortunately, semantic-release is a tool that automates these steps, ensuring you never have to worry about manual versioning again. In this blog post, I’ll introduce semantic-release, explain how it works, and demonstrate how it helps developers streamline their release process.
What is semantic-release?
semantic-release is a fully automated version management and release tool that integrates seamlessly with your CI/CD pipeline. It automatically determines the next version number, generates release notes, and publishes the new release based on the commit messages in your project. Following the principles of Semantic Versioning (SemVer), semantic-release ensures that each release follows a predictable, meaningful version numbering system (e.g., 1.0.0
, 1.1.0
, 2.0.0
, etc.).
How Does semantic-release Work?
At the core of semantic-release is the use of conventional commits—a standardized commit message format that describes the impact of a change. Based on the commit type (fix
, feat
, BREAKING CHANGE
), semantic-release automatically determines whether to make a patch, minor, or major release. Here’s how it works:
Commit Message Analysis: Each commit message must follow a specific convention, such as
fix
,feat
, orBREAKING CHANGE
. For example:fix: correct typo in README
feat: add user authentication
BREAKING CHANGE: remove support for Node.js 10
Automated Version Bumping: Based on the commits, semantic-release calculates whether the new release should be a patch (for fixes), minor (for new features), or major (for breaking changes).
Generating Release Notes: It generates a changelog with detailed release notes that describe the changes made in the new release, derived directly from the commit messages.
Tagging and Publishing: semantic-release tags the new version in your Git repository and publishes the release automatically (e.g., to npm, GitHub Releases, etc.).
CI/CD Integration: semantic-release integrates with CI/CD pipelines, ensuring that every time code is merged into the main branch, the correct version is calculated, and the release process is triggered automatically.
Benefits of Using semantic-release
1. Automated Versioning
semantic-release eliminates the need for manually updating version numbers in package.json
or CHANGELOG.md
. This reduces human error, ensuring that versions follow Semantic Versioning principles based on changes in the codebase.
2. Consistent Releases
With semantic-release, you get consistent releases because it follows a structured process. Developers don’t need to worry about accidentally forgetting to update version numbers or push release tags.
3. Better Team Collaboration
In collaborative environments, multiple developers might be pushing code changes at the same time. semantic-release ensures that each release is appropriately versioned and tagged, even when multiple feature branches are being merged.
4. Improved Changelogs
Writing changelogs can often be an afterthought, leading to incomplete or outdated documentation. semantic-release generates changelogs automatically based on commit messages, making sure the release notes are always up to date.
5. CI/CD Integration
Because it’s designed to work with continuous integration and continuous delivery (CI/CD) systems, semantic-release fits perfectly into modern development workflows. Tools like GitHub Actions, Travis CI, CircleCI, and others can trigger the release process after successful tests or builds.
Setting Up semantic-release in Your Project
Let’s walk through a basic setup for using semantic-release in a JavaScript project.
1. Install semantic-release
First, you need to install semantic-release and some additional plugins:
npm install --save-dev semantic-release @semantic-release/changelog @semantic-release/git @semantic-release/npm
2. Configure .releaserc File
Next, you’ll need to configure semantic-release by creating a .releaserc
file in your project’s root directory. Here’s an example configuration:
{
"branches": ["main"],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
}
This setup specifies:
The branch where releases are triggered (
main
).A list of plugins that help analyze commits, generate release notes, update changelogs, and publish to GitHub and npm.
3. Enforcing Conventional Commits
To make semantic-release work properly, you’ll need to adopt conventional commit messages. One way to enforce this is by using the commitizen tool, which helps standardize commit messages.
Install commitizen:
npm install --save-dev commitizen cz-conventional-changelog
Add it to your package.json
:
{
"scripts": {
"commit": "cz"
}
}
Now, whenever a developer commits, they can run npm run commit
to follow the conventional commit structure.
4. CI/CD Integration
To fully automate the release process, you’ll need to configure your CI tool (e.g., GitHub Actions, Travis CI) to run semantic-release after tests pass. For GitHub Actions, you can add a simple workflow:
name: Release
on:
push:
branches:
- main
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Release
run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Conclusion
semantic-release is a powerful tool for automating the release process, ensuring that versioning and changelog management happen without manual intervention. By analyzing commit messages and following Semantic Versioning, semantic-release helps developers maintain a consistent release cycle, reduces errors, and keeps changelogs up to date—all while integrating seamlessly into your CI/CD pipeline.
If you’re tired of manually bumping version numbers, updating changelogs, or ensuring that releases are correctly tagged, it’s time to give semantic-release a try. It saves time, improves collaboration, and makes the development process smoother for everyone on the team.
Subscribe to my newsletter
Read articles from Frank Lam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by