【问题标题】:How do I substitute values appsettings.json in a test project in Azure DevOps release pipeline?如何在 Azure DevOps 发布管道的测试项目中替换值 appsettings.json?
【发布时间】:2021-03-22 12:33:17
【问题描述】:

我有什么

  • 在我的发布管道中,我使用 IIS Web 应用部署任务在 appsettings.json 中进行变量替换:

  • 我想运行一些集成和端到端测试:

问题

  1. 如何替换与我的测试项目相关的设置 appsettings.json。
  2. 如何强制测试运行程序使用特定环境,例如 Staging,以便选择 appsettings.Staging.json?

【问题讨论】:

  • 您可以尝试添加单独的file transform 任务。另外,能否详细说明一下appsettings.json和test的关系?
  • @HiGuy:这解决了 #1。我也解决了 #2,但暂时不会将其标记为答案,以防您有更好的想法。

标签: azure-devops integration-testing continuous-deployment azure-pipelines-release-pipeline


【解决方案1】:
  1. 如何替换与我的测试项目相关的设置 appsettings.json。

    File Trasnform 任务执行变量替换与 IIS Web 应用部署任务完全相同,但在单独的步骤中。


  1. 如何强制测试运行器使用特定环境,例如 Staging,以便选择 appsettings.Staging.json?

    ASPNETCORE_ENVIRONMENT 等环境变量可以在 .runsettings 文件中配置:

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

    然后可以在单独的管道步骤中替换环境变量的值。我用过powershell任务:

     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'
    

我还需要确保 test.runsettings 文件包含在我的构建工件中,并将 Visual Studio 测试任务配置为使用该文件

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2020-03-11
    • 2020-12-13
    • 2020-01-08
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多