Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto increment version number in a Python webserver, with git

I have a Python webserver (using Bottle or Flask or any other) that I develop locally:

BUILDVERSION = "0.0.128"

@route('/')
def homepage():    
    ... # using a template, and the homepage shows the BUILDVERSION in the footer
        # so that I always know which live version is running

...

run(host='0.0.0.0', port=8080)

Each time I have a significant update, I do:

git commit -am "Commit name" && git push

and the distant version is updated. (Note: I use git config receive.denyCurrentBranch updateInstead on the distant repo).

Problem: I often forget to manually increment the BUILDVERSION on each commit, and then it's not easy to distinguish which version is running live, etc. (because two consecutive commits could have the same BUILDVERSION!)

Question: Is there a way to have an auto-incrementing BUILDVERSION on each commit with Python + Git? or anything similar (the BUILDVERSION could also be the commit ID...) that would be present in small characters in the footer of the website, allowing to distinguish consecutive versions of the Python code.

like image 287
Basj Avatar asked Jun 27 '18 21:06

Basj


People also ask

How do you increment version numbers?

There are simple rules that indicate when you must increment each of these versions: MAJOR is incremented when you make breaking API changes. MINOR is incremented when you add new functionality without breaking the existing API or functionality. PATCH is incremented when you make backwards-compatible bug fixes.


1 Answers

As mentioned in Change version file automatically on commit with git, git hooks and more specifically a pre-commit hook can be used to do that.

In the specific case of Python, versioneer or bumpversion can be used inside the .git/hooks/pre-commit script:

#!/bin/sh
bumpversion minor
git add versionfile

Another option is to use the git commit id instead of a BUILDVERSION:

import git
COMMITID = git.Repo().head.object.hexsha[:7]    # 270ac70

(This requires pip install gitpython first)

Then it's possible to compare it with the current commit ID with git log or git rev-parse --short HEAD (7 digits is the Git default for a short SHA).

like image 165
Basj Avatar answered Oct 21 '22 08:10

Basj