.NET Core - Set Environment Variable in Azure Deployment Task .NET Core - Set Environment Variable in Azure Deployment Task azure azure

.NET Core - Set Environment Variable in Azure Deployment Task


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.


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"    }  }}