Search code examples
azure-devopsintegration-testingcontinuous-deploymentazure-pipelines-release-pipeline

How do I substitute values appsettings.json in a test project in Azure DevOps release pipeline?


What do I have

  • In my release pipeline I use IIS Web App Deployment Task that does a variable substitution in appsettings.json:

    task screenshot

  • I want to run some integration and end to end tests:

    enter image description here

Question

  1. How do I replace settings appsettings.json related to my test projects.
  2. How do I force the test runner to use a specific environment, e.g Staging, so that appsettings.Staging.json will is picked?

Solution

    1. How do I replace settings appsettings.json related to my test projects.

      There is File Trasnform task that does the variable substitution exactly like IIS Web App Deployment Task but in a separate step.


    1. How do I force the test runner to use a specific environment, e.g Staging, so that appsettings.Staging.json will is picked?

      Environment variables like ASPNETCORE_ENVIRONMENT can be configured in a .runsettings file:

       <?xml version="1.0" encoding="utf-8"?>
       <RunSettings>
         <RunConfiguration>
           <EnvironmentVariables>
             <ASPNETCORE_ENVIRONMENT>Development</ASPNETCORE_ENVIRONMENT>
           </EnvironmentVariables>
         </RunConfiguration>
       </RunSettings>
      

      The value of environment variable can be then replaced in a separate pipeline step. I've used powershell task:

       steps:
       - powershell: |
          $fileName = "$(System.DefaultWorkingDirectory)/MyBuildArtifact/test.runsettings";
          [xml]$xml = Get-Content $fileName
          $xml.SelectSingleNode("//ASPNETCORE_ENVIRONMENT").InnerText = "$(System.StageDisplayName)";
          $xml.Save($fileName);
         failOnStderr: true
         showWarnings: true
         displayName: 'Configure ASPNETCORE_ENVIRONMENT'
      

    I also needed to make sure the test.runsettings file is included in my build artifact and configure the Visual Studio Test task to use that file