Among the automated versioning tools, ‘commitizen’ came out as the most suitable option for our requirements since it is not limited to a specific technology stack like Java or React. It can be used globally for any project and can automate the process if the project is in a proper CI environment.
Here's how to integrate commitizen into the Github actions of any project.
- Create two files named .cz.yaml and Changelog in the root directory of the project.
- Add the following content into the .cz.yaml file.
---
commitizen:
name: cz_conventional_commits
tag_format: $version
version: 3.0.0
-
Create a personal access token. Follow the instructions here. And copy the generated key (you can use any among the fine-grained or classic personal access tokens).
-
Add the read-and-write access to the content permission of the token by clicking edit right next to the token.
-
Create a secret called PERSONAL_ACCESS_TOKEN, with the copied key, by going to your project repository and then Settings -> Secrets and variables -> New repository secret -> Add new secret.
-
In your project create a new file .github/workflows/bumpversion.yml and add the below content.
name: Bump version
on:
push:
branches:
- main
permissions:
contents: write
jobs:
bump-version:
if: "!startsWith(github.event.head_commit.message, 'bump:')"
runs-on: ubuntu-latest
name: "Bump version and create changelog with commitizen"
steps:
- name: Checkout Latest Commit
uses: actions/checkout@v2
with:
token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}"
fetch-depth: 0
- 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.GITHUB_TOKEN }}
- name: Update version in pom.xml
run: |
mvn -B versions:set -DnewVersion=${{ env.REVISION }} -DgenerateBackupPoms=false
mvn versions:commit
- name: Commit files
uses: test-room-7/action-update-file@v1
with:
file-path: pom.xml
commit-msg: bump:update pom
github-token: ${{ secrets.GITHUB_TOKEN }}
- Push this file and that’s it!