Using Semantic Release to Automate Versioning and Publishing

Using Semantic Release to Automate Versioning and Publishing

Semantic Release is a tool that automates the versioning and publishing of your codebase. It uses commit messages to determine the next version number and generate release notes. For example, if you follow the Conventional Commits specification, Semantic Release can automatically determine the next version number based on the commit history.

Why Semantic Release?

Semantic Release is a powerful tool that can save you time and effort when managing your project’s versioning and publishing process. Here are some of the benefits of using Semantic Release:

  • Automated Versioning: Semantic Release automatically determines the next version number based on the commit history.
  • Automated Publishing: Semantic Release can automatically publish new versions of your package to NPM or other registries.
  • Release Notes Generation: Semantic Release generates release notes based on the commit history, making it easy to keep track of changes between versions.
  • Consistent Versioning: Semantic Release enforces a consistent versioning scheme across your project, making it easier to manage dependencies and releases.
  • CI/CD Integration: Semantic Release can be integrated with CI/CD pipelines to automate the release process.
  • Community Standard: Semantic Release follows the Conventional Commits specification, which is a widely adopted standard for writing commit messages.
  • Extensible: Semantic Release is highly customizable and extensible, allowing you to tailor it to your project’s specific needs.
  • Open Source: Semantic Release is an open-source project with an active community of contributors and maintainers.

My Experience

I recently started using Semantic Release in one of my projects, and I’ve been impressed with how easy it is to set up and use. I was able to automate the versioning and publishing process with just a few simple steps, and I now have a consistent and reliable release process in place.

The best part about Semantic Release is that it takes the guesswork out of versioning and publishing. I no longer have to worry about manually updating version numbers or generating release notes – Semantic Release does it all for me. This has saved me a lot of time and effort, and I can now focus on writing code instead of managing releases.

It was akarjs npm package that I used Semantic Release for. It was a breeze to set up and use, and I highly recommend it to anyone looking to automate their versioning and publishing process.

Getting Started

To get started with Semantic Release, you’ll need to install the semantic-release package in your project. You can do this by running the following command:

npm install semantic-release --save-dev

Once you’ve installed the semantic-release package, you’ll need to set up a configuration file for Semantic Release. You can create a .releaserc file in the root of your project with the following content:

{
  "branches": [
    "main",
    {
      "name": "next",
      "prerelease": true
    }
  ],
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    "@semantic-release/npm",
    "@semantic-release/github",
    [
      "@semantic-release/changelog",
      {
        "changelogFile": "CHANGELOG.md"
      }
    ],
    [
      "@semantic-release/git",
      {
        "assets": [
          "package.json",
          "package-lock.json",
          "CHANGELOG.md"
        ],
        "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
      }
    ]
  ]
}

This configuration file tells Semantic Release which branches to release from and which plugins to use for versioning, release notes generation, changelog generation, publishing to NPM, and publishing to GitHub.

CI/CD Integration

To integrate Semantic Release with your CI/CD pipeline, you’ll need to add a few environment variables to your CI configuration. For example, if you’re using GitHub Actions, you can add the following configuration to your .github/workflows/release.yml file:

name: Release

on:
  push:
    branches:
      - main
      - next # For pre-releases

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 20

      - name: Install dependencies
        run: npm ci

      - name: Semantic Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npx semantic-release

Note: Make sure to set up the GITHUB_TOKEN and NPM_TOKEN secrets in your GitHub repository settings.

Conclusion

Semantic Release is a powerful tool that can automate the versioning and publishing of your codebase. By following the Conventional Commits specification and setting up Semantic Release, you can save time and effort when managing your project’s releases.

I highly recommend giving Semantic Release a try in your next project. It’s easy to set up and use, and it can help you maintain a consistent and reliable release process. For more, please follow the resources below.

Read this article on Dev.to