0

I work on a custom documentation management system with Django.

Currently there is a productive envoirment on an IIS + Hyper-V with a database, let's call it productiveDB.

New features are implemented locally in different branches, using another database, let's call this one stagingDB.

When I work locally on a new branch I have to adjust the database configuration and credentials from productiveDB to stagingDB in my settings.py - furthermore I set the DEBUG Mode to True. When I commit my changes and merge into master, I sometimes forget to adjust the settings and this is where my question begins:

What is the best practice to handle this 'inattention'? Sure, I could keep the local settings adjusted for the staging envoirment and vice versa for the productive settings but here and then I have to add new settings to the aformentioned file and therefore I would have to commit the edited settings.py.

Is there a 'build in' way in GitLab or git to mark specific variables and change them according to the branch they are under? Like: if branch=master then set DEBUG=FALSE, DATABASE=productiveDB before while the CI/CD pipeline is running or do I have to stick to a custom script?

Dnz
  • 23
  • 4

1 Answers1

0

I suggest to add one environment variable for distinguishing different environments to the settings.py. Example:

# settings.py

import os

environment = os.environ.get("ENVIRONMENT", "DEVELOPMENT").lower()
if environment == "production":
    DATABASES = {
        ...
    }
else:
    DATABASES = {
        ...
    }

Second option is to create multiple settings files for each environment. Look at this article for detailed explanation.

Elgin Cahangirov
  • 1,932
  • 4
  • 24
  • 45