c4urself / bump2version

Version-bump your software with a single command
https://pypi.python.org/pypi/bump2version
MIT License
1.05k stars 134 forks source link

[Question] How to use bump version to add PR number to version #206

Open ecs-jnguyen opened 3 years ago

ecs-jnguyen commented 3 years ago

Hi I am trying to use bump2version to automate versioning during a CI build. My use case is to mark the package version with a pull request number when opening a pull request and to update the minor version when pushing to the master branch.

I have gotten it to work, during my master build, I am making 2 commands to increment the version. However I am wondering if there is a more elegant way to do this

What I'm trying to achieve

  1. I start off with some version, for example 1.0.0
  2. I open a pull request number 123 to the main branch master, then I want bump2version to modify the version to be 1.1.0-beta123 (I will not commit change and push to github)
  3. When I merge pull request 123 to master, then I want bump2version to modify the version to 1.1.0. Then my CI tool will push the .bumpversion.cfg and setup.py file to github (I am handling the additional git push webhook to stop incrementing version)
  4. Repeat

How I've accomplished this so far

Step 1 Open a pull request number 123 to master

CI tool commands

$ export PR_NUM=123
$ bump2version minor #  results in: 1.0.0 --> 1.1.0-beta123
$ push package to pypi repo

Step 2 pull request number 123 into master

CI tool commands

$ export PR_NUM="ignore"
$ bump2version minor #  results in: 1.0.0 --> 1.1.0-betaignore
$ bump2version release #  results in: 1.1.0-betaignore --> 1.1.0
$ push package to pypi repo
$ git commit / tag / push

.bumpversion.cfg I have configured

[bumpversion]
current_version = 1.0.0
False = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?
serialize = 
    {major}.{minor}.{patch}-{release}{$PR_NUM}
    {major}.{minor}.{patch}

[bumpversion:part:release]
optional_value = ignore
first_value = beta
values = 
    beta
    ignore

[bumpversion:file:setup.py] # setup.py also starts version at 1.0.0
florisla commented 3 years ago

I think currently this is not possible.

Eachbump2version invocation can only bump one part. It can not change minor and release in one go.

Ugly workarounds are to use --new-version (which forces you to handle things manually) or to put the 'beta' string also in the environment variable.

ecs-jnguyen commented 3 years ago

One way I've gotten this to work is for Step 2 in the original post, do the following:

$ export PR_NUM="ignore"
$ bump2version minor --serialize ""major}.{minor}.{patch}" #  results in: 1.0.0 --> 1.1.0