deploying web app with env specific appsetting

Viewed 340

I am currently deploying an web application on which I have an appsetting for production purposes and one for test purposes. how do i during deploy replace the appsetting.json with the content of the production or test?

to deploy i use IIS web app manage and IIS web app deploy.

What I currently do is everytime something is pushed to main, I have setup Azure to trigger an

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  testConfiguration: 'Test'
  prodConfiguration: 'Production'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(testConfiguration)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(prodConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
   command: 'publish'
   publishWebProjects: true
   zipAfterPublish: true
   arguments: '--output $(build.artifactstagingdirectory)'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

I then use the artifact created in drop to for deploy purposes. The problem occur when I want to deploy to either my test or production environment.

The solution itself has an a transform on the appsetting.json, namely the appsetting.Test.json and appsetting.Production.json but what is located in the drop folder is only the prod build published, and not both test and prod.

How do i include both under the drop folder?

So when I release it via the deploy pipeline can specify that I want to deploy the test build or prod build?

4 Answers

If the issue that the appsetting.Test.json not include in your drop folder, so add next section into your .csproj:

<ItemGroup>
   <None Include="appsettings.Test.json" CopyToPublishDirectory="Always" />
</ItemGroup>

Instead of transforming the appsettings.*.json files into a single appsettings.json file during the solution build, make sure both original files are included in the drop folder. If you struggle with that, you need to include more detail about your csproj file into the question.

Upon deployment to IIS, make sure you set ASPNETCORE_ENVIRONMENT environment variable to test if you are on the test environment so that the correct configuration will be applied.

I also have a similar answer that deals specifically with using this way of switching environments: .NET Core 3.1 Worker Service - set EnvironmentName on publish

Related