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?
To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.
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"
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With