Do not overwrite config file on Azure using GIT

2019-02-19 21:19发布

I have a web application that is hosted on Azure.
There are currently a few web sites that are using the same code which is retrieved from bitbucket.
The only difference between the various installations is that they have a different connection string in the web.config files that targets different databases.

Whenever I am making a bug fix I upload my new code on bitbucket and Azure gets this code from BitBucket.
There are currently 5 applications that get the same code and each time I upload new code on bitbucket I have to manually edit the web.config file on Azure in order to target the correct database for each app.

Is there a way to instruct Azure not to overwrite the web.config file when It receives new code from Bitbucket?

3条回答
ら.Afraid
2楼-- · 2019-02-19 21:56

On possibility is to not version web.config, but a template file web.config.tpl, and a value file (with values for each applications)

Then, you can use a content filter driver, using .gitattributes declaration.

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

The generated actual web.config remains ignored (by the .gitignore).
That means your actual working tree does not get "dirty".

The smudge script selects the correct value file and generates the correct web.config based on the template the smudge script is applied on during a git checkout.

查看更多
欢心
3楼-- · 2019-02-19 21:58

The responsibility to change settings based on the website you are deploying could be in the release. VSTS supports releasing software from a source like BitBucket to Azure. Create a VSTS account. Create a project within the account. Create a new build and configure BitBucket as source. Create a release to deploy the webapplication to each of the five places. Use the variables to set a different connectionstring for each webapplication.

查看更多
贪生不怕死
4楼-- · 2019-02-19 22:00

Why don't need to hardcode connection strings in web.config at all. Just pick it up in your code from Application Settings.

From https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure/:

For .NET apps, these connection strings are injected into your .NET configuration connectionStrings settings at runtime, overriding existing entries where the key equals the linked database name..

using System.Configuration;

// Define AzureSQLConnStr in Portal -> Web App -> Application Settings
string AzureSQLConnStr = ConfigurationManager.ConnectionStrings["AzureSQLConnStr"];

Application Settings/Connection Strings defined in the Portal will override the values from web.config / app.config at runtime.

查看更多
登录 后发表回答