In this post we’ll consider rather common situation when you’ve got application with dedicated configuration file and at least two environments: dev and prod.
Application in dev env is using dev database and prod application is using prod database;
Database connection strings are stored in app.config file, so you have to have different app.config files in dev and prod Git branches.
The most common suggestion about managing this challenge is to use git attributes merging policy, so let’s see how to deal with it.
Create merge strategy which will use driver ours:
Create (I’ll use BitBucket) and clone our demo repo:
Now we can create and commit .gitattributes file on root level of the repo (use .git/info/attributes if you want to use it only in the local repo);
this file will set merge behavior for the app configuration file which happens to be app.config in our example:
echo 'app.config merge=ours' >> .gitattributes
git add .gitattributes
git commit -m 'gitattributes file has been added'
Well, it’s time to create dev branch, add our application files and configuration file:
Ok, our dev app is fine, so we can create prod branch from dev:
First of all we want to change database connection string:
Perfect, our app is running correctly in both dev and prod environments now,
But what if some changes were added to app in dev and now they need to be merged to prod?
Let’s take a look at this:
As you can see, application code was successfully updated and configuration files are still different for dev and prod:
But what if app.config file has been changed on dev?
As you can see we’re still on the safe side – changes in dev config don’t affect our prod configurations.
You can ensure that if configuration files were changed in both dev and prod, they still will be merged correctly:
So what’s the problem with this scenario?
In real life you probably don’t want to do merges using CLI but do Pull requests;
The very pity thing is than considered merging policy doesn’t support in Pull requests:
Situation is the same for BitBucket, GitHub and Visual Studio Team Services.
However, you can review pull request and keep in mind that you don’t have to care about certain files (app.config in our example),
When you finish Pull request review, perform merge using CLI and ensure that it has been completed successfully despite warnings in GUI:
As for me, this solution looks like:
So how the general challenge can be managed?
I suggest using two different files – app.config.dev and app.config.prod ;
During CD suitable file will be copied and renamed.
You can also consider using OS env variables or parsing config with tools like AWK.