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-releasepackage.
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
- Initialize a new Git repository:bash
git init - Add your project files:bash
git add . - 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
- Use standard headers like
feat,fix, orchore:- 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"
- Example for a new feature:
- Include a
BREAKING CHANGEfooter for significant updates affecting the API or behavior:bashBREAKING CHANGE: refactor API to use GraphQL - Maintain a clear structure:
- Header:
<type>(optional scope): <description> - Body: (optional) Additional context.
- Footer: (optional) Breaking changes or references to issues.
- Header:
Step 3: Implement Semantic Versioning
Semantic Versioning (MAJOR.MINOR.PATCH) correlates directly with commit types:
steps
- MAJOR: Introduce breaking changes, noted by
BREAKING CHANGE. - MINOR: Add features without breaking existing functionality (
featcommits). - PATCH: Fix bugs or small tweaks (
fixcommits). - 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
- Install the required package:bash
npm install semantic-release --save-dev - Create a
.releaserc.jsonfile 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" ] } - Integrate semantic-release into your CI/CD workflow. For example, in GitHub Actions, add a job to your
.github/workflows/release.yml:yamlname: 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.