Developer Guides

How to Use Conventional Commits with Semantic Versioning

Learn step-by-step how to implement Conventional Commits and leverage Semantic Versioning for workflow automation.

4 min read

Learn how to streamline your development workflow using Conventional Commits and Semantic Versioning. This guide will walk you through setup and configuration steps, so you can automate versioning and changelog generation efficiently.

Prerequisites

Before you begin, ensure you meet the following requirements:

prerequisites

  • Basic familiarity with Git and a Git repository set up in your environment.
  • Node.js installed on your system.
  • Understanding of the Conventional Commits format.
  • Installation of the semantic-release package.

Step 1: Set Up a Git Repository

To start, you need a functional Git repository. If you don't already have one, follow these steps:

steps

  1. Initialize a new Git repository:
    bash
    git init
  2. Add your project files:
    bash
    git add .
  3. Commit your files:
    bash
    git commit -m "Initial commit"

Step 2: Learn the Conventional Commits Syntax

Conventional Commits uses a structured format to ensure clarity and consistency in commits. Here's how to adapt your commit messages:

steps

  1. Use standard headers like feat, fix, or chore:
    • Example for a new feature:
      bash
      git commit -m "feat: add user authentication module"
    • Example for a bug fix:
      bash
      git commit -m "fix: resolve issue with login timeout"
  2. Include a BREAKING CHANGE footer for significant updates affecting the API or behavior:
    bash
    BREAKING CHANGE: refactor API to use GraphQL
  3. Maintain a clear structure:
    • Header: <type>(optional scope): <description>
    • Body: (optional) Additional context.
    • Footer: (optional) Breaking changes or references to issues.

Step 3: Implement Semantic Versioning

Semantic Versioning (MAJOR.MINOR.PATCH) correlates directly with commit types:

steps

  1. MAJOR: Introduce breaking changes, noted by BREAKING CHANGE.
  2. MINOR: Add features without breaking existing functionality (feat commits).
  3. PATCH: Fix bugs or small tweaks (fix commits).
  4. Ensure your team adheres to these guidelines for consistent version histories.

Step 4: Configure Semantic-Release

With your commit practices standardized, it's time to implement automation with semantic-release.

steps

  1. Install the required package:
    bash
    npm install semantic-release --save-dev
  2. Create a .releaserc.json file in your project directory with basic configuration:
    json
    {
      "branches": ["main"],
      "plugins": [
        "@semantic-release/commit-analyzer",
        "@semantic-release/release-notes-generator",
        "@semantic-release/npm",
        "@semantic-release/github"
      ]
    }
  3. Integrate semantic-release into your CI/CD workflow. For example, in GitHub Actions, add a job to your .github/workflows/release.yml:
    yaml
    name: Release
    
    on:
      push:
        branches:
          - main
    
    jobs:
      release:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - uses: actions/setup-node@v3
            with:
              node-version: '18'
          - run: npm ci
          - run: npx semantic-release

Verification and Automation

Common Mistakes

Avoid common pitfalls when setting up Conventional Commits and Semantic Versioning:

Conclusion

FAQ

What are Conventional Commits?

Conventional Commits is a convention for writing consistent commit messages. It specifies a structure, including keywords like feat and fix to categorize changes.

How does Semantic Versioning work?

Semantic Versioning specifies a version format: MAJOR.MINOR.PATCH. Each segment reflects a different level of change—major updates include compatibility-breaking changes, minor updates add features, and patch updates fix bugs.

Why use semantic-release in my project?

Semantic-release automates the versioning and release process based on your commit history. It ensures consistent release tags, changelogs, and version management across development workflows.

Do I need a CI/CD pipeline for semantic-release?

Yes. Semantic-release works best when integrated into a CI/CD pipeline to automatically publish new releases when changes are pushed to the repository’s main branch.


Official reference: Conventional Commits specification.