Privacy Policy
Snippets index

  Versioning a Django project

  1. define __version___ and __build__ into "PROJECT/__init__.py":

    from __future__ import absolute_import
    
    __version__ = '1.0.0'
    __build__ = ''
    
  2. automatic build update (file "settings.py"):

    import subprocess
    import PROJECT
    
    BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..',))
    
    try:
        PROJECT.__build__ = subprocess.check_output(["git", "describe", "--tags", "--always"], cwd=BASE_DIR).decode('utf-8').strip()
    except:
        PROJECT.__build__ = PROJECT.__version__ + " ?"
    
  3. update "PROJECT_settings" context processor (file "PROJECT/context_processors.py"):

    from django.conf import settings
    import PROJECT
    
    def PROJECT_settings(request):
    
        return {
            'PROJECT_VERSION': PROJECT.__version__,
            'PROJECT_BUILD': PROJECT.__build__,
        }
    

then, in settings:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'PROJECT.context_processors.PROJECT_settings',
            ],
        },
    },
]
  1. add config file ".bumpversion.cfg":

    [bumpversion]
    current_version = 1.0.0
    commit = True
    tag = True
    
    [bumpversion:file:PROJECT/__init__.py]
    
  2. create a CHANGELOG.rst file:

    1.0.1
    -----
    * versioning added
    * ...
    
  3. test bumpversion:

bumpversion patch --dry-run --verbose

    Would prepare Git commit
    Would add changes in file 'PROJECT/__init__.py' to Git
    Would add changes in file '.bumpversion.cfg' to Git
    Would commit to Git with message 'Bump version: 1.0.0 → 1.0.1'
    Would tag 'v1.0.1' in Git
  1. when approriate, bump version and push onto upstream:
(develop)$ bumpversion patch --verbose
(develop)$ git checkout master
(master)$ git merge develop
(master)$ git push
(master)$ git push --tags