Basj
Basj

Reputation: 46163

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.

Upvotes: 6

Views: 6030

Answers (1)

Basj
Basj

Reputation: 46163

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).

Upvotes: 12

Related Questions