6

I’m fairly new to implementing CI/CD through Azure DevOps and I have what is probably a typical scenario that I’m not sure how to address. Most of the articles I’ve found dealing with file transformation deal with IIS Deploy but I’m currently working with .Net Framework console apps.

In my console apps we have certain settings, usually file paths, that are different based on the environment we are in (Dev, Stage, Prod) as well as the database connection string being different in each environment.

I was shown how to use variables, ex: __connectionstring__, that can be set and replaced in a Azure DevOps release pipeline using the Tokenizer app. However, having that variable in my development environment doesn’t work. When I debug in Visual Studio it still sees the above variable name and doesn’t have something like the tokenizer to populate that variable locally on my development machine.

Can someone point me to an article or example on a good way to have appsettings specific to each environment I’m in that will allow me to still debug locally but also change the settings in the ADO release pipeline?

Caverman
  • 3,371
  • 9
  • 59
  • 115

2 Answers2

8

You can use task File transform to replace certain settings in Azure DevOps release pipeline.

Variables defined in the build or release pipeline will be matched against the 'key' or 'name' entries in the appSettings, applicationSettings, and connectionStrings sections of any config file and parameters.xml. Variable Substitution is run after config transforms.

For example you have below appsetting.json file. And you want to change the default log level to Error.

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

1, First you need to define a Release Variable Logging.LogLevel.Default in the Variables section of the release pipeline edit page with the value Error assigned to it. See below

enter image description here

2, Add the File transform task in your release pipeline. enter image description here

For more information about XML variable substitution, please check it out here.

There are also third party substitution tools(ie. Magic Chunks/ RegEx Find & Replace ) that are very convenient to use to replace the values in your settings files in azure pipeline. Please check out the example in this thread.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Thanks. This looks very promising as well. I'll look at trying this in the next couple of days and let you know how that works. Of course I'm working with .Net Framework at the moment but trying to move our team to use .Net Core as well. – Caverman Nov 11 '20 at 05:26
  • @Caverman Did you get a chance to try above out. How did it go? – Levi Lu-MSFT Nov 13 '20 at 09:56
  • no I haven't had a chance to work on that one yet. Other commitments at work keep taking priority. I just completed my first .Net Core app for production use and was able to get a build done for it yesterday. Hope to work on the release pipeline this week or maybe next week. – Caverman Nov 18 '20 at 04:50
  • Wanted to come back and say I was finally able to try this out and "File Transform" seems to work just as shown. I'm using this on a .Net Core app and I'm able to keep my local database settings in my appsettings but then use this to transform my values for STAGE and PROD through the release pipeline. This also keeps my PROD connectionstring values out of my repository as well. Easy setup and worked perfectly for the appsettings.json file. Thanks! – Caverman Nov 25 '20 at 21:46
  • I came back to this answer for half a year now, and I just figured out how to do it... Thanks a lot man. – Kadaj Jan 24 '23 at 16:08
0

To transform .config files of non-web applications, I always use SlowCheetah:

It works just like the web.config transformation, but for app.config.

giacomelli
  • 7,287
  • 2
  • 27
  • 31