3

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?

I am not Fat
  • 283
  • 11
  • 36
  • According to your description, I couldn’t understand your requirement clearly. What is your purpose in doing this? – samwu May 24 '22 at 07:35
  • The content of the transformed appsetting.json files is different when deploying I want to override use the environment specific and not the one set I appsetting.json – I am not Fat May 25 '22 at 10:02
  • Your problem seems complicated, I suggest you open a case via: https://support.microsoft.com. – samwu May 27 '22 at 09:30
  • I think you can use bash script in Linux or bat files in Windows , create a step in your pipeline to change appsetting.json – behrooz razzaghi May 30 '22 at 05:28

4 Answers4

0

Have you tried using the Azure App Service Settings task?

FinneVirta
  • 374
  • 1
  • 4
  • 14
0

If you do not want variable substitutions then maybe you can have 2 copies of the json one for prod and one for test and then use a copy task as described here: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/copy-files?view=azure-devops&tabs=yaml

You can add the copy in your deploy yaml and use the file you need based on the stage. After this copy task you can add your deploy task.

0

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>
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
0

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

Alex AIT
  • 17,361
  • 3
  • 36
  • 73