Git manage environment specific configuration

旧时模样 提交于 2020-06-22 02:27:06

问题


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?


回答1:


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.

(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".



来源:https://stackoverflow.com/questions/59484603/git-manage-environment-specific-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!