Changelog¶
This guide explains how changelogs are automatically generated in this Python package template using git-cliff and conventional commits.
Overview¶
The template uses git-cliff for automated changelog generation based on conventional commit messages. This ensures:
- Consistent formatting: Standardized changelog structure
- Automatic updates: No manual changelog maintenance
- Conventional commits: Structured commit messages
- Release integration: Changelogs generated on releases
Conventional Commits¶
All commits must follow the Conventional Commits specification:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
Commit Types¶
feat: New featuresfix: Bug fixesdocs: Documentation changesstyle: Code style changes (formatting, etc.)refactor: Code refactoringtest: Test additions or modificationschore: Maintenance tasksperf: Performance improvementsci: CI/CD changesbuild: Build system changes
Examples¶
# Feature commit
git commit -m "feat: add user authentication"
# Bug fix with scope
git commit -m "fix(api): resolve login timeout issue"
# Breaking change
git commit -m "feat!: change API response format
BREAKING CHANGE: The response format has changed from XML to JSON"
# Commit with body and footer
git commit -m "fix: correct typo in documentation
The word 'recieve' was misspelled as 'receive'.
Closes #123"
Git-cliff Configuration¶
Basic Setup (cliff.toml)¶
# git-cliff ~ configuration file
# https://git-cliff.org/docs/configuration
[changelog]
# A Tera template to be rendered for each release in the changelog.
# See https://keats.github.io/tera/docs/#introduction
body = """
{% macro print_commit(commit) -%}
- {% if commit.scope %}*({{ commit.scope }})* {% endif %}\
{% if commit.breaking %}[**breaking**] {% endif %}\
{{ commit.message | upper_first }} - \
([{{ commit.id | truncate(length=7, end="") }}](<REPO>/commit/{{ commit.id }}))\
{% endmacro -%}
{% if version %}\
{% if previous.version %}\
## [{{ version | trim_start_matches(pat="v") }}]\
(<REPO>/compare/{{ previous.version }}..{{ version }}) - {{ timestamp | date(format="%Y-%m-%d") }}
{% else %}\
## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }}
{% endif %}\
{% else %}\
## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | striptags | trim | upper_first }}
{% for commit in commits
| filter(attribute="scope")
| sort(attribute="scope") %}
{{ self::print_commit(commit=commit) }}
{%- endfor %}
{% for commit in commits %}
{%- if not commit.scope -%}
{{ self::print_commit(commit=commit) }}
{% endif -%}
{% endfor -%}
{% endfor -%}
"""
# A Tera template to be rendered as the changelog's footer.
# See https://keats.github.io/tera/docs/#introduction
footer = """
---
**Contributing**: We welcome contributions! Please see our [Contributing Guide](<REPO>/blob/main/CONTRIBUTING.md) for details.
**Questions?** Open an issue on [GitHub](<REPO>/issues) or join our discussions.
"""
# Remove leading and trailing whitespaces from the changelog's body.
trim = true
# Render body even when there are no releases to process.
render_always = true
# An array of regex based postprocessors to modify the changelog.
postprocessors = [
# Replace the placeholder <REPO> with a URL.
{ pattern = '<REPO>', replace = 'https://github.com/isaac-cf-wong/python-package-template' },
]
[git]
# Parse commits according to the conventional commits specification.
# See https://www.conventionalcommits.org
conventional_commits = true
# Exclude commits that do not match the conventional commits specification.
filter_unconventional = false
# Takes precedence over filter_unconventional.
require_conventional = false
# Split commits on newlines, treating each line as an individual commit.
split_commits = false
# An array of regex based parsers to modify commit messages prior to further processing.
commit_preprocessors = [
# Replace issue numbers with link templates to be updated in `changelog.postprocessors`.
{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](<REPO>/issues/${2}))" },
# Check spelling of the commit message using https://github.com/crate-ci/typos.
# If the spelling is incorrect, it will be fixed automatically.
# { pattern = '.*', replace_command = 'typos --write-changes -' },
]
# Prevent commits that are breaking from being excluded by commit parsers.
protect_breaking_commits = false
# An array of regex based parsers for extracting data from the commit message.
# Assigns commits to groups.
# Optionally sets the commit's scope and can decide to exclude commits from further processing.
commit_parsers = [
{ message = "^Merge pull request", skip = true },
{ message = "^feat", group = "<!-- 0 -->๐ Features" },
{ message = "^fix", group = "<!-- 1 -->๐ Bug Fixes" },
{ message = "^doc", group = "<!-- 3 -->๐ Documentation" },
{ message = "^perf", group = "<!-- 4 -->โก Performance" },
{ message = "^refactor", group = "<!-- 2 -->๐ Refactor" },
{ message = "^style", group = "<!-- 5 -->๐จ Styling" },
{ message = "^test", group = "<!-- 6 -->๐งช Testing" },
{ message = "^chore\\(release\\): prepare for", skip = true },
{ message = "^chore\\(pr\\)", skip = true },
{ message = "^chore\\(pull\\)", skip = true },
{ message = "^chore|^ci", group = "<!-- 7 -->โ๏ธ Miscellaneous Tasks" },
{ body = ".*security", group = "<!-- 8 -->๐ก๏ธ Security" },
{ message = "^revert", group = "<!-- 9 -->โ๏ธ Revert" },
{ message = ".*", group = "<!-- 10 -->๐ผ Other" },
]
# Exclude commits that are not matched by any commit parser.
filter_commits = false
# Regex to select git tags that represent releases.
tag_pattern = "v[0-9].*"
# Regex to select git tags that do not represent proper releases.
# Takes precedence over `tag_pattern`.
# Changes belonging to these releases will be included in the next release.
skip_tags = "beta|alpha"
# Regex to exclude git tags after applying the tag_pattern.
ignore_tags = "rc"
# An array of link parsers for extracting external references, and turning them into URLs, using regex.
link_parsers = []
# Include only the tags that belong to the current branch.
use_branch_tags = false
# Order releases topologically instead of chronologically.
topo_order = false
# Order releases topologically instead of chronologically.
topo_order_commits = true
# Order of commits in each group/release within the changelog.
# Allowed values: newest, oldest
sort_commits = "newest"
# Process submodules commits
recurse_submodules = false
Configuration Sections¶
Changelog Template¶
The body template defines the changelog format using Tera templating:
- Version headers: Automatic version and date formatting
- Commit grouping: Commits grouped by type with emojis
- Link generation: Automatic GitHub links for commits and comparisons
- Contributor recognition: New contributors section
Git Configuration¶
conventional_commits: Enforce conventional commit formatcommit_preprocessors: Transform commit messages (e.g., issue links)commit_parsers: Define how commits are categorizedtag_pattern: Pattern for recognizing version tags
Generating Changelogs¶
Manual Generation¶
# Generate changelog for latest release
git cliff --latest --strip header
# Generate full changelog
git cliff -o CHANGELOG.md
# Generate for specific version
git cliff --tag v1.2.3
# Preview unreleased changes
git cliff --unreleased
Pre-commit Integration¶
Changelog generation can be automated via pre-commit:
repos:
- repo: https://github.com/orhun/git-cliff
rev: v2.4.0
hooks:
- id: git-cliff
args: [--latest, --strip header]
CI/CD Integration¶
Changelogs are automatically generated during releases:
# In release workflow
- name: Generate Changelog
run: |
git cliff --latest --strip header > changelog.md
Commit Message Validation¶
Commitlint Configuration¶
Commit messages are validated using commitlint:
Configuration (commitlint.config.js):
Pre-commit Hook¶
- id: check-toml
stages: [pre-commit]
- id: debug-statements
stages: [pre-commit]
- id: end-of-file-fixer
stages: [pre-commit]
Release Process¶
Version Tagging¶
-
Create annotated tag:
git tag -a v1.2.3 -m "Release v1.2.3" git push origin v1.2.3 -
GitHub Actions will:
- Generate changelog
- Create release
- Publish to PyPI
Automated Changelog¶
The changelog is automatically included in GitHub releases:
# In release workflow
- name: Create Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
$(git cliff --latest --strip header)
Customizing Changelog¶
Modifying Groups¶
Add custom commit groups:
commit_parsers = [
{ message = "^feat", group = "<!-- 0 -->๐ Features" },
{ message = "^custom", group = "<!-- 11 -->๐ฏ Custom Changes" },
]
Changing Template¶
Modify the changelog template:
[changelog]
body = """
# Custom Changelog Format
{% for group, commits in commits | group_by(attribute="group") %}
## {{ group }}
{% for commit in commits %}
- {{ commit.message }}
{% endfor %}
{% endfor %}
"""
Adding Links¶
Include additional links in commits:
commit_preprocessors = [
{ pattern = '#(\d+)', replace = "[#$1](https://github.com/org/repo/issues/$1)" },
{ pattern = '@(\w+)', replace = "[@$1](https://github.com/$1)" },
]
Best Practices¶
Commit Message Guidelines¶
- Be descriptive: Explain what and why, not just what
- Use scopes: Group related changes (e.g.,
fix(api)) - Reference issues: Include issue numbers when relevant
- Keep it concise: Subject line under 72 characters
Version Management¶
- Semantic versioning: Major.minor.patch
- Breaking changes: Use
!suffix for breaking changes - Pre-releases: Use alpha/beta/rc suffixes
Changelog Maintenance¶
- Review before release: Check generated changelog
- Manual edits: Edit template for special cases
- Consistency: Keep formatting consistent
- Automation: Let tools handle routine updates
Troubleshooting¶
Common Issues¶
- Commits not appearing: Check conventional commit format
- Wrong grouping: Verify commit parser patterns
- Links not working: Check remote URL configuration
- Template errors: Validate Tera template syntax
Debugging¶
# Debug commit parsing
git cliff --verbose
# Test template
git cliff --template cliff.toml
# Check commit format
git log --oneline | head -10
Configuration Validation¶
# Validate cliff.toml
git cliff --config cliff.toml --dry-run
# Test with sample commits
git cliff --with-commit "feat: test feature"
Integration Examples¶
With GitHub Releases¶
name: Release
on:
push:
tags: ['v*']
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Generate Changelog
run: git cliff --latest --strip header > changelog.md
- name: Create Release
uses: softprops/action-gh-release@v1
with:
body_path: changelog.md
With Release Drafter¶
name: Release Drafter
on:
push:
branches: [main]
jobs:
update_release_draft:
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
For more information, see the git-cliff documentation and Conventional Commits specification.