Adam Grande
Adam Grande

Reputation: 253

.NET Core - Set Environment Variable in Azure Deployment Task

I'm currently trying to use the launchSettings.json file to manage the environment variables of the application, so my Setup.cs file can manage the environments in the way of env.IsDevelopmentEnvironment(), etc.

In VSTS, how do I go about setting the environment variable ASPNETCORE_ENVIRONMENT on an Azure Deployment task? Or should it get in the dotnet publish task I've got in my build steps?

Upvotes: 25

Views: 43238

Answers (3)

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29958

You can install the Replace Token extension and then add a Replace Token task in your build/release definition. This task could replace the strings in a file with the variable value you added in the build/release definition.

Upvotes: 0

Set
Set

Reputation: 49769

Because ASPNETCORE_ENVIRONMENT is an environment variable, you can just specify it on Azure.

See the Stack Overflow answer on How and where to define an environment variable on Azure.

Upvotes: 31

Matthew
Matthew

Reputation: 726

If you want to keep your deployment process idempotent, I'd suggest using this deployment step to set on the Azure Web App.

https://marketplace.visualstudio.com/items?itemName=pascalnaber.PascalNaber-Xpirit-WebAppConfiguration

Technically it adds release settings to the web.config as well, which isn't necessary for a core app, but importantly, it also sets the Environment Variables for the Azure host.

Provided you have specified to use environment variables in your Startup.cs:

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
                         .SetBasePath(env.ContentRootPath)
                         .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)                             
                         .AddEnvironmentVariables(); //override settings with environment variables

        var config = builder.Build();

        Configuration = config;

    }

So if you have a release variable: appsetting.ASPNETCORE_ENVIRONMENT = Release, you will find that $env:ASPNETCORE_ENVIRONMENT will indeed be "Release" if you're checking via the PowerShell console on Kudu.

I'm actualy using this extension to override all of my appsettings.json variables as well as ASPNETCORE_ENVIRONMENT at release-time instead of tokenzing some appsettings.{environment}.json file. I can just override with environment variables by using the right naming convention in my VSTS Release Variable names.

For example, if my appsettings.json has this structure:

{
  settings: {
    secret: {
      foo: "bar"
    }
  }
}

I can override with a release variable such as:

appsetting.settings:secret:foo = "bar"

Then go check $env:settings:secret:foo on the Azure Web App after deployment

Without doing anything additional in my source or uzipping a web deployment package, tokenizing a config file and then re-zipping prior to msdeploy, I've got enviornment-specific configurations.

Upvotes: 6

Related Questions