25

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Grande
  • 253
  • 1
  • 3
  • 7
  • So you want to update the value of "ASPNETCORE_ENVIRONMENT" variable in "launchSettings.json" file before deploy it? – Eddie Chen - MSFT Aug 04 '16 at 11:43
  • Sort of yeah. Im struggling to convey what it is Im trying to do. Basically, when set up a build task (or a release task) in vsts, i want to set the ASPNETCORE_ENVIRONMENT variable at that point, so when its deployed it is essentially the 'correct' environment. basically, sort of like how the web.config transforms work. does that make sense? – Adam Grande Aug 05 '16 at 00:15
  • Its also entirely possible Im looking at this completely the wrong way. Given @set s answer below, Im wondering if i should be setting up the site with the environment variable preconfigured, and simply deploy to that. – Adam Grande Aug 05 '16 at 00:20

3 Answers3

30

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Set
  • 47,577
  • 22
  • 132
  • 150
  • ah thanks, thats super close to what im after. my point is how do i set that at deploy/build time from VSTS :) thanks for the response! – Adam Grande Aug 05 '16 at 00:16
  • @AdamGrande did you ever find a way to do this from VSTS? I would actually like to set all of my application settings via environment variables. Considering that env vars that follow the correct naming convention will effectively override appsettings.json values, it would remove the need to tokenize a second json file or similar. – Matthew Feb 24 '17 at 21:28
  • Just to emphasize this answer, that's also what the official documentation suggests for Azure: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments#setting-the-environment – Daniel Feb 16 '18 at 11:22
6

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.

Matthew
  • 706
  • 9
  • 12
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60