'GitLab CI/CD for Commitizen Bump and Changelog

Background

With Conventional Commits, commitizen, and pre-commit, one can enforce a standard rule for commit message and ensure they are properly formatted. Using Conventional Commits (or at least style-enforced commit messages):

  • You can use services like GitHub's dependabot to automatically tag PRs and run specific checks (or with a script that parses the commit message)
  • commitizen itself can bump the version of the code base (cz bump) per semantic versioning, and you can even set a CHANGELOG.md (keep a changelog) and tag to be generated on every bump, eg. in your pyproject.toml
[tool.commitizen]
update_changelog_on_bump = true
annotated_tag = true

Question

I write python (version 3.8, Windows) and store my versions in my pyproject.toml and mypkg/__version__.py files (FYI, my primary branch is called main (reference this article)).

I want commitizen to handle my versioning and CHANGELOG.md automatically. How can I perform the following?:

  1. Upon successful merge from merge request (MR) branch into main
  • Run cz bump --changelog --annotated-tag, but rather than create a separate merge commit for bumpversion x.y.z -> a.b.c, amend the merge commit with the updated changelog and tag

Attempt

I have tried the following in my gitlab-ci.yml:

deploy_job:
  stage: deploy
  rules:
    - if: $CI_PIPELINE_SOURCE == "push"
    - if: $CI_BUILD_REF_NAME == "main"
  before_script:
    - git remote set-url origin "https://path/to/repo.git"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git checkout main
  script:
    - git cz bump 
    - git push -o ci.skip --tags

but I can't seem to get it to work after the merge has taken place and I don't know how to bump my version without creating a separate commit.

Research

I found these useful:

  1. Commitizen > Tutorials > GitLab CI
  2. Automatic Software Version Bump Using GitLab CI/CD
  3. GitLab CI bump Python package version
  4. Automation of releases with gitlab CI
  5. What is the standard way of version bumping from gitlab CI? A few questions
  6. Automatic Semantic Versioning in GitLab CI

I have also looked at the following:

  1. Run pre-commit conventional commit check on squash commit before merge into master only
  2. How would I write a pre-merge hook in Git?
  3. commitizen-tools: What can we gain from crafting a git message convention?
  4. Automating semver releases with commitizen


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source