.Net Core - setting an environment variable in an Azure deployment task

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

My question is this: in VSTS, how do I configure the ASPNETCORE_ENVIRONMENT environment ASPNETCORE_ENVIRONMENT in the Azure Deployment task? Or should he enter the dotnet publish task that I received at my build stages?

Any help would be greatly appreciated. If I need to provide additional information, please let me know, as I am also not sure that what I have provided is enough to make the question clear.

Greetings

Adam

+5
source share
3 answers

The reason ASPNETCORE_ENVIRONMENT is an environment variable, you can just point it to Azure. See SO's answer to How and where to determine the environment variable for azure?

+8
source

If you want your deployment process to be idempotent, I would suggest using this deployment step for installation in Azure Web App.

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

Technically, it adds release settings to web.config, which is not necessary for the main application, but, importantly, it also sets the environment variables for the Azure host.

If you indicated that you are using environment variables in 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 the variable release: appsetting.ASPNETCORE_ENVIRONMENT = Release, you will find that $ env: ASPNETCORE_ENVIRONMENT will really be "Release" if you check the PowerShell console on Kudu.

I really use this extension to override all my appsettings.json variables as well as ASPNETCORE_ENVIRONMENT at release time, rather than tokenzing some appsettings settings. {environment} .json. I can simply override environment variables using the correct naming convention in my VSTS Release Variable names.

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

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

I can override the release variable, for example:

appsetting.settings: secret: foo = "bar"

Then go to check $ env: settings: secret: foo in Azure Web App after deployment

Without doing anything extra in my source or removing the web deployment package, tokenizing the configuration file, and then re-looping to msdeploy, I have environment-specific settings.

+2
source

You can install the Replace Token extension and then add the Replace Token task to the build / release definition. This task can replace the lines in the file with the value of the variable that you added in the build / release definition.

0
source

All Articles