Git管理特定于环境的配置 [英] Git manage environment specific configuration
问题描述
我需要为开发,uat和生产等不同环境进行属性配置。例如,一个config.properties具有 environment = dev
之类的条目,我需要更改其暂存分支为 environment = uat
和对于master分支为 environment = prd
。
我尝试分别在每个分支中提交这些文件,尝试在gitignore中添加config.properties,以便在下次提交时不考虑。
但是git忽略了没有更新,所以我运行了命令
git rm -rf --cached src / config.properties
git add src / config.properties
git commit -m .gitignore fix
但是此命令正在从本地存储库本身中删除文件,并且进行的提交也从分支中删除。我想像这样处理分支,因此Jenkins无需手动编辑配置文件即可进行部署。我在git UI上使用fork。有什么方法可以处理这种情况?
您不应该版本 config.properties
( git rm
是正确的),并且确实忽略它。
这样,在执行过程中不会造成任何问题
拥有三个单独的文件更容易,每个环境一个文件:
-
config.properties.dev
-
config.properties.uat
-
config.properties.prd
然后在每个分支中,根据其中一个文件,从其中一个文件中生成 config.properties
,并在其中包含正确的值。执行环境。
由于每个环境都有单独的分支,并且其中有正确的文件,因此您可以使用一个生成脚本来确定已签出分支的名称,其中:
branch = $(git rev-parse --symbolic --abbrev-ref HEAD)
这意味着您可以:
- 仅使用模板文件版本
config.properties。< env>
- 以分支命名的版本值文件:
config.properties。 dev
,config.properties.uat
...:由于它们不同,因此合并或切换分支时没有合并问题。
最后,您将进行注册(在
(来自 自定义Git-Git属性,来自 Pro Git书)
涂抹
脚本,与模板文件( package.json.tpl
),将自动生成 git checkout
c $ c> config.properties 文件,方法是在正确的 config.properties。< env>
值文件中查找值。
生成的实际 config.properties
文件仍然被忽略( .gitignore
)。
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?
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".
这篇关于Git管理特定于环境的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!