Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase Version number if Travis at github was successful

I wrote a simple script in Python.

Now I would like travis to check my code. After travis was successful, the version number should get increased.

Up to now my script has no version number yet. I can store it anywhere where it makes sense for the auto-increment workflow.

How to do this for Python code?

Update

It works now:

  1. run tests
  2. bumpversion
  3. push tag to master

Unfortunately travis does not support "after-all". This means if I want to run the tests for several Python versions, I have no way to bumpversion after the tests of all python versions were successful.

In my case I will check on Python2.7 only until travis resolved this issue: https://github.com/travis-ci/travis-ci/issues/929

Here is my simple script: https://github.com/guettli/compare-with-remote

Solved :-)

It works now:

  1. Developer pushes to github
  2. Travis-CI runs
  3. If all tests are successful bumpversion increases the version
  4. The new version in setup.py get's pushed to the github repo
  5. A new release of the python package gets uploaded to pypi with the tool twine.

I explain the way I do CI with github, travis and pypi here: https://github.com/guettli/github-travis-bumpversion-pypi

like image 543
guettli Avatar asked Mar 10 '17 12:03

guettli


2 Answers

If you accept having extra commit for your versioning, you could add this script in continuous_integration/increment_version.py

import os
import pkg_resources


if __name__ == "__main__":
    version = pkg_resources.get_distribution("compare_with_remote").version
    split_version = version.split('.')
    try:
        split_version[-1] = str(int(split_version[-1]) + 1)
    except ValueError:
        # do something about the letters in the last field of version
        pass
    new_version = '.'.join(split_version)
    os.system("sed -i \"s/version='[0-9.]\+'/version='{}'/\" setup.py"
              .format(new_version))
    os.system("git add -u")
    os.system("git commit -m '[ci skip] Increase version to {}'"
              .format(new_version))
    os.system("git push")

And change your .travis.yml to

after_success:
  - python continuous_integration/increment_version.py

I am not sure about how to make the git push part work as it need some testing with the repo rights but I assume that you could probably setup something to allow travis to push in your repo. you could look into that post for instance.

Also note that I used python to perform the git operations but they can be added as extra line in the after_success field:

after_success:
  - python continuous_integration/increment_version.py
  - git add -u
  - git commit -m "[ci skip] version changed"
  - git push

I just find it convenient to put the version number in the commit msg.

Also, it is very important to add [ci skip] in the commit message to avoid infinite increment. Maybe it would be safer to trigger the version change on a specific commit msg tag.

like image 146
Thomas Moreau Avatar answered Nov 11 '22 15:11

Thomas Moreau


Not Python-specific, but this tutorial explains auto-incrementing version numbers by adding .travis.yaml entries which update git tags with each successful build. It seems like a good balance between manual and auto-versioning.

While the tutorial does use npm's package.json for the initial version-check, you could implement a simple equivalent (in Python or otherwise).

like image 4
codemacabre Avatar answered Nov 11 '22 14:11

codemacabre