12

Where is it best to set the environment name for each deployment of a ASP.NET 5 web application when publishing to Azure Web Apps?

I understand that the ASPNET_DEV environment variable needs to be set. Is this possible on publish?

This is our powershell publish script:

param($websiteName, $packOutput)

$website = Get-AzureWebsite -Name $websiteName

# get the scm url to use with MSDeploy.  By default this will be the second in the array
$msdeployurl = $website.EnabledHostNames[1]

$publishProperties = @{'WebPublishMethod'='MSDeploy';
                        'MSDeployServiceUrl'=$msdeployurl;
                        'DeployIisAppPath'=$website.Name;
                        'Username'=$website.PublishingUsername;
                        'Password'=$website.PublishingPassword}

Write-Output "Restarting web app..."
Restart-AzureWebsite -Name $websiteName

Write-Output "Publishing web app..."
$publishScript = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\Publish\Scripts\default-publish.ps1"

. $publishScript -publishProperties $publishProperties  -packOutput $packOutput

Or do we have set the environmental variable in the Azure portal? Will this persist between deployments?

I am using the ASP.NET 5 beta6 libraries with the dnx451 target framework, if that matters.

Dave New
  • 38,496
  • 59
  • 215
  • 394

2 Answers2

8

Where is it best to set the environment name for each deployment of a ASP.NET 5 web application when publishing to Azure Web Apps?

@cory-fowler is right. Use App Settings to set the environment name. There are at least four ways to do that.

portal.azure.com

Browse > All resources > MyApp > Settings > Application settings.

manage.windowsazure.net

Web apps > MyApp > CONFIGURE > app settings.

Azure Command Line Interface (CLI)

npm install -g azure-cli
azure account download
azure account import '~\Downloads\my-credentials.publishsettings'
site appsetting add ASPNET_ENV=Test MyApp 
azure site appsettings list MyApp 

Azure PowerShell

choco install -y windowsazurepowershell 
Get-AzurePublishSettingsFile
Import-AzurePublishSettingsFile '~\Downloads\my-credentials.publishsettings'
$hash = (Get-AzureWebsite -Name "MyApp").AppSettings
$hash.Add("ASPNET_ENV", "Test")
Set-AzureWebsite -Name "MyApp" -AppSettings $hash

As you can see, it is trickier with PowerShell than with the CLI. Note: restart the console window after installing Azure PowerShell.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 1
    Note: the manage.windowsazure.net portal has been [sunset](https://azure.microsoft.com/en-gb/updates/azure-portal-updates-for-classic-portal-users/). – valentin Nov 28 '18 at 08:56
2

App Settings definitely persist between deployments, they are a great way to distinguish environments in the cloud.

I wrote a blog post about this in 2012. Using Environment Variables in App Service vs Cloud Service

cory-fowler
  • 4,020
  • 2
  • 18
  • 29