Git manage environment specific configuration
Asked Answered
D

1

6

I have a requirement to have a property configuration for different environment like dev, uat and production. For example a config.properties having and entry like environment=dev, this I need to change for staging branch as environment=uat and for master branch as environment=prd .

I tried to commit these files in each branch respectively and tried adding config.properties in gitignore so that it will not consider in next commits. But git ignore not getting updated so I ran command

git rm -rf --cached src/config.properties
git add src/config.properties
git commit -m ".gitignore fix"

But this command is deleting the file from local repository itself and the proceeding commits also deleting from branches. I want to handle the branch as such so as Jenkins will do the deployment without editing config file manually. I am using fork for git UI. Is there any way to handle this kind of situation?

Db answered 26/12, 2019 at 6:24 Comment(1)
while merging your dev branch to uat, don't commit that file.then uat Will use its from config.propertiesGoldschmidt
N
7

You should not version a config.properties (git rm is right), and ignore it indeed.
That way, it won't pose any issue during merge.

It is easier to have three separate files, one per environment:

  • config.properties.dev
  • config.properties.uat
  • config.properties.prd

In each branch, you would then generate config.properties, with the right value in it, from one of those files, depending on the current execution environment.

Since you have separate branches per environment, with the right file in it, you can have a generation script which will determine the name of the checked out branch with:

branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

That means you could:

  • version only a template file config.properties.<env>
  • version value files named after the branches: config.properties.dev, config.properties.uat...: since they are different, there is no merge issue when merging or switching branches.

Finally, you would register (in a .gitattributes declaration) a content filter driver.

smudge (image from "Customizing Git - Git Attributes", from "Pro Git book")

The smudge script, associated to the template file (package.json.tpl), would generate (automatically, on git checkout) the actual config.properties file by looking values in the right config.properties.<env> value file.
The generated actual config.properties file remains ignored (by the .gitignore).

See a complete example at "git smudge/clean filter between branches".

Naquin answered 26/12, 2019 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.