Search code examples
pythongitgithubversionsemantic-versioning

Bump Python package version upon main branch merge


My Problem

I am developing a Python package consumed by several internal consumers in my company using their CI/CD pipeline. Therefore, it's important for them to know what is the latest version of the package so that they can install it if it has been changed. The package uses the classic PEP 440 SemVer structure, so I usually just update the minor number.

I'm using Git and GitHub, so every new commit to the main branch means a new version. I'm currently updating the version manually, which is error-prone and tedious. I'm looking for a way to automatically bump the minor version whenever I merge a PR to the main branch.

What Have I Tried

  • Manual version bump before the merge
  • Using bump2version - I don't know how to make it happen automatically on a merge to the main branch

My Question

How can I automatically bump the minor version of a Python package upon a PR merged to the main branch of a repo?


Solution

  • Instead of using bump2version locally, you can add to your GitHub repostory the bump2version-action, which states:

    Every time you merge something to main branch, you'll receive an additional direct commit to main that increments the version in version.md.

    Example:

    name: Bump version workflow
    
    on:
      push:
        branches:
        - main
        paths-ignore:
        - version.md
    
    jobs:
      bump-version:
        name: Bump package version
        if: "!contains(github.event.head_commit.message, 'Bump version')"
        runs-on: ubuntu-20.04
        steps:
        - name: actions/checkout
          uses: actions/checkout@v2
          with:
              persist-credentials: false
        - name: current_version
          run: echo "current_version=$(grep '# version' version.md | cut -d ' ' -f3)" >> $GITHUB_ENV
        - name: FragileTech/bump-version
          uses: FragileTech/bump-version@main
          with:
            current_version: "${{ env.current_version }}"
            files: version.md
            commit_name: Your Company Bot
            commit_email: bot@your-company.com
            login: your-bot-login
            token: "${{ secrets.BOT_TOKEN }}"
    

    As noted by C S in the comments:

    You can use part as an input argument.
    OP wanted to bump the minor version automatically on merge, not the patch version, which is the default with no part arg for this action.