0
votes

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!

2
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

2 Answers

0
votes

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.

0
votes

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?