DEV Community

Sibiraj
Sibiraj

Posted on

Automate changelog generation and publish with Changesets

Often, while working with packages, managing changelogs and versioning manually can be tedious, especially in fast-moving projects. Keeping track of what changed, bumping versions correctly, and ensuring a smooth release process requires discipline and consistency.

Changesets is a robust tool designed to streamline the entire process, from making changes and version bumps to generating changelogs and publishing the package npm.

Read more about the problem and what changesets solve here: https://github.com/changesets/changesets/blob/main/docs/detailed-explanation.md

In this article, we’ll explore how to integrate Changesets into your project.

In our workflow, we use npm to publish packages and leverage Changesets GitHub Action to automate the entire process. This ensures that every release is handled efficiently, with minimal manual intervention.

Setup

To set up changesets. Install the package and initialize

npm i -D @changesets/cli && npx changeset init
Enter fullscreen mode Exit fullscreen mode

This will create a .changeset directory with a config.json, which maintains the configuration. This is also the directory where the changelog entries are stored before they are applied.

Generate changesets

Changesets are markdown files with YAML front matter, which contains the summary of the changes made and the kind of versioning. A changeset can be generated by running

npx changeset
Enter fullscreen mode Exit fullscreen mode

This will prompt you to enter

  • kind of change (patch/minor/major)
  • Summary of the change

Once entered, a changeset with a unique name is generated into the .changeset directory. This file needs to be committed.

Automate releases

The process of automating changelog generation and publishing the packages to registry is handled with changesets action

Copy the following YAML to the .github/workflows/release.yaml

name: Release

on:
  push:
    branches:
      - main

concurrency: ${{ github.workflow }}-${{ github.ref }}

env:
  NODE_VERSION: 22

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      id-token: write
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v4

      - name: Setup Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Install Dependencies
        run: npm ci

      - name: Create Release Pull Request or Publish to npm
        id: changesets
        uses: changesets/action@v1
        with:
          publish: npx changeset publish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

Now when you push the commit with changesets to the main branch. The action will run, and depending on the changesets created, a PR with a changelog and version bump like in the following screenshot will be created.

PR Preview

When you merge this, Version Packages PR, the changeset action will again run, but this time it will publish the version bump to the package registry.

Yup, that's it, your package should be published to npm 🎉.

Top comments (0)