We have been having an issue migrating to Azure Devops with .Net Core to deploy to our on-prem Windows servers. It's probably due to a lack of knowledge of the platform but nonetheless...
The problem we are trying to figure out is when following a "build once and deploy everywhere" structure, how do we transform our web.config file to set the "ASPNETCORE_ENVIRONMENT" env variable to the correct environment that is being deployed. Currently our apps need that variable set because we have our appsettings split between 2 files, appSettings.json and appSettings.Env.json, where Env is the current environment we are deploying to (Dev or Prod).
For example: Azure Pipelines builds our project and the resulting web.config in the output artifact has the ASPNETCORE_ENVIRONMENT variable set to "Development". That's great since we then deploy that artifact to our dev environment. But what happens when we release that to our prod stage? That web.config still has the ASPNETCORE_ENVIRONMENT variable set to "Development".
What is the proper (2021) way to transform that config? should I not even use that variable in the web.config and change up how we interact with our appSettings file?
I've read through many article and questions about similar issues but not many related to the ASPNETCORE_ENVIRONMENT env variable. Some say use the "Replace token task". Some say use config transforms (they don't work for ASPNETCORE_ENVIRONMENT).
Not sure where to go from here.
What I've recently tried:
Transform web.config for Azure Website Deployment for each release environment
Azure Pipelines Config Transform based on Stage
My generated web.config file on publish:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
Azure Pipeline file
name: WebApp-$(Date:yyyyMMdd).$(rev:rr)
trigger:
branches:
include:
- master
variables:
major: 2
minor: 0
patch: 0
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
Parameters.WebsiteName: 'webapp'
jobs:
- job: Build
pool:
vmImage: 'windows-latest'
steps:
- task: DotNetCoreCLI@2
displayName: 'dotnet restore'
inputs:
command: 'restore'
projects: '$(solution)'
- task: VSBuild@1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="webapp" /p:EnvironmentName="Development"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest@2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: ArchiveFiles@2
displayName: 'Create Zip Archive'
inputs:
rootFolderOrFile: '$(build.artifactStagingDirectory)/'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildNumber).zip
replaceExistingArchive: true
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: true
- task: PublishBuildArtifacts@1
displayName: 'Web Artifact'