.NET Core - Set Environment Variable in Azure Deployment Task

前端 未结 3 1483
离开以前
离开以前 2021-02-18 18:01

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 en

3条回答
  •  醉话见心
    2021-02-18 18:34

    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.

提交回复
热议问题