Automate Your Changelog and Release with Git Hooks and Node.js

The Power of a Changelog

A changelog is more than just a record of changes to your project. It’s a valuable resource for fixing bugs, educating new developers, and tracking progress. But maintaining a changelog can be a tedious task, especially when working with a large team. That’s why we’ll explore a method for automatically generating and releasing a changelog using git hooks and Node.js.

Conventional Commits: The Key to Easy Changelogs

The Conventional Commits specification provides a set of rules for creating a specific commit history. By structuring commit messages in a specific way, you can easily generate a changelog that follows semantic versioning. A conventional commit message consists of:

  • <type>: A type of commit that affects the version number of the release (e.g., fix or feat)
  • <scope>: An optional noun that describes the part of the codebase changed (e.g., pages)
  • <description>: A brief explanation of the changes made
  • <body>: An optional field for describing the commit in more detail
  • <footer>: An optional field for adding additional information (e.g., BREAKING CHANGE)

Creating a Project and Automating Releases

Let’s create a new project and automate our changelog and release using standard-version and Node.js. First, we’ll create a new npm-based project and make it a git repository. Then, we’ll install standard-version and add it to our npm scripts.

Generating a Changelog and Release

Now, let’s create a dummy file and commit it with a conventional commit message. We’ll then run our newly added script to create a release. This will increase the SemVer version number, create a CHANGELOG.md file, commit the changes, and print out a message to push tags and publish our package to npm.

Simplifying Commit Messages with Commitizen

Manually writing conventional commits can be error-prone. That’s why we’ll use Commitizen to auto-generate them. Commitizen asks you questions in the command prompt and generates the commits based on your answers.

Enforcing Conventions with commitlint and Husky

To ensure that all developers on our project follow the same conventions, we’ll use git hooks with Husky and commitlint. We’ll install commitlint and Husky, configure commitlint, and use the commit-msg hook to lint messages before they are committed.

Final Workflow for Managing Releases

To manage our releases, we’ll follow this workflow:

  1. Create features and commit them.
  2. Execute npm run commit to make a commit with Commitizen.
  3. Run npm run release to create a changelog and a semantic versioning-based release.

By automating our changelog and release process, we can save time and ensure that our project is always up-to-date.

Leave a Reply

Your email address will not be published. Required fields are marked *