0

I am deploying a web application to an on prim windows server via an Azure Release Pipeline that contains 2 tasks. IIS Web App Manage & IIS Web App Deploy.

The issue I'm running into is I need to replace the appsettings.json with a completely different file. We have 3 environments (dev/test/prod), and each of them have their own appsettings.json in source control.

I see that there is a JSON Variable Substitution, but I don't want to substitue variables, I want to say "Hey, use this file instead of this file".

How can I do this in Azure Devops? If I am going about this all wrong, please let me know. It's what my company has done for years and I really don't want to create variables in a pipeline to update my appsettings.json.

Thanks!

Godrules500
  • 467
  • 6
  • 25
  • Not get your latest information.If one of the below answers works for you, you could consider to mark it as an answer, if you have any concern, feel free to share it here. – Hugh Lin Apr 29 '20 at 09:21

2 Answers2

1

Do you want to work with the corresponding appsettings.json according to the different environment? If so ,the core project can retrieve data from appsettings.[environment].json file per to ASPNETCORE_ENVIRONMENT environment variable.

1.Add corresponding appsettings.[environment].json files to project, such as appsettings.Production.json, appsettings.Development.json and set the corresponding value in each files.

2.Code in Startup file:

public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

3.Set/add ASPNETCORE_ENVIRONMENT environment variable to each environment machine (just need to set/add once)

Here are the reference: Configuration in ASP.NET Core, case.

Hugh Lin
  • 17,829
  • 2
  • 21
  • 25
  • Thank you for your response! Is there a way to set the environment variable in the release pipeline? – Godrules500 Apr 27 '20 at 12:04
  • You can add [PowerShell on Target Machines](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/powershell-on-target-machines?view=azure-devops) task to release pipeline to set the environment variable. – Hugh Lin Apr 28 '20 at 01:44
0

You need to create appsettings.json per envrionement and then change you code in this manner:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables();
    this.configuration = builder.Build();
}

The you need to define environment variable called ASPNETCORE_ENVIRONMENT with value for your environment Dev, Staging, Wahtever you like env name.

If you need more info, here you found great blog post.

Here you have also plenty of info about setting your env file. Automatically set appsettings.json for dev and release environments in asp.net core?

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • Thank you for your response! Is there a way to set the environment variable in the release pipeline? – Godrules500 Apr 27 '20 at 12:04
  • Yes you can. You need to run for instance powershell script on remote machine to set `ASPNETCORE_ENVIRONMENT `. Please us this task for that - https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/powershell-on-target-machines?view=azure-devops But I recommend you to di it manually on your each environment. – Krzysztof Madej Apr 27 '20 at 12:13