Aller au contenu

Le versioning avec Commitizen

Non seulement Commitizen permet d'uniformiser la manière de rédiger les messages de commit (par défaut, Commitizen utilise Conventional Commits) mais aussi de simplifier la gestion de version d'un projet en créant des tags et en mettant à jour le changelog.

Mise en place et configuration

Nous souhaitons initialiser le projet à la version 0.0.0 :

pyproject.toml
[project]
dynamic = ["version"]

[tool.commitizen]
name = "cz_conventional_commits"
version = "0.0.0"
version_files = [
    "src/sandbox/__init__.py:__version__",
    "README.md:version-",
    "pyproject.toml:version",
]
tag_format = "$version"
update_changelog_on_bump = true
annotated_tag = true
src/sandbox/__init__.py
__version__ = "0.0.0"
README.md
![version](https://img.shields.io/badge/version-0.0.0-blue)
CHANGELOG.md
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Saut de version

Il suffit de lancer la commande :

cz bump

On peut aussi faire un essais avant :

cz bump --dry-run

Utiliser un GitHub Actions pour automatiser le versioning

.github/workflows/bumpversion.yml
name: Bump version

on:
  push:
    branches:
      - main

jobs:
  bump-version:
    name: "Bump version and create changelog with commitizen"
    if: ${{ !startsWith(github.event.head_commit.message, 'bump:') }}
    runs-on: self-hosted

    steps:
      - name: Check out
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}"

      - id: cz
        name: Create bump and changelog
        uses: commitizen-tools/commitizen-action@master
        with:
          github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          changelog_increment_filename: body.md

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          body_path: "body.md"
          tag_name: ${{ env.REVISION }}
        env:
          GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

      - name: Print Version
        run: echo "Bumped to version ${{ steps.cz.outputs.version }}"

Références