【问题标题】:Variable values are not injected into local.settings.json变量值不会注入到 local.settings.json
【发布时间】:2022-02-02 21:19:50
【问题描述】:

我有一个 Azure Function 项目。它包括带有一些秘密的 local.settings.json 配置文件。这些值由 xUnit 测试项目检索和使用。在 Visual Studio 上运行时一切正常,但在 Azure 构建管道中,不会注入值,并且文件包含原始占位符值。 local.settings.json 文件的 Build Action 为“Content”,Copy to Output Directory 为“Copy if newer”。 在管道中,我使用文件转换任务来注入值。 这是我的管道的一部分:

        - task: FileTransform@1
        inputs:
          folderPath: '$(System.DefaultWorkingDirectory)/ProjectName'
          fileType: 'json'
          targetFiles: 'local.settings.json'
          
      - task: DotNetCoreCLI@2
        displayName: 'DotNet Build Projects'
        inputs:
          command: 'build'
          projects: '**/*.csproj'
          arguments: --configuration $(buildConfiguration)          
         
      - task: DotNetCoreCLI@2
        displayName: 'Run Unit Tests'
        inputs:
          command: 'test'
          projects: '**/ProjectName.Tests/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/'
          publishTestResults: true          

我做错了什么?

【问题讨论】:

    标签: azure-devops azure-pipelines azure-pipelines-yaml azure-pipelines-tasks


    【解决方案1】:

    不确定您的 json 文件的结构,以及如何定义变量。但是根据您的描述,变量值没有注入local.settings.json。好像是变量定义不正确,或者运行管道时找不到json文件。

    targetFiles 字段中直接或相对地提供 json 文件路径,因此,如果您在第一个字段中给出的文件夹路径中有多个 appsettings 文件,那么对于所有设置 json 文件,给出相对路径,如下所示:@987654324 @。

    File transform task 将根据与设置 json 文件中的字段匹配的变量名称,从可用变量中更新设置 json 文件中的字段。例如,将下面的 json 文件作为我们的应用设置 json 文件:

    {
        "customFunctions": [],
        "importedLibraries": [],
        "firstResource": "",
        "groupOne": {
            "firstFieldInOne": "Test",
            "name": "DemoApp"
        },
        "groupTwo": {
            "nameTwo": "TestValue2",
            "idTwo": ""
        },
        "runtimeSettings": {
            "storage": "",
            "telemetry": {
                "instrumentationKey": ""
            }
        }
    }
    

    在这个 appsettings 文件中,如果你想替换 json 文件的父元素,那么你应该在 yaml 管道内添加变量或作为带有元素名称的管道变量 - 例如,更新 'firstResource' 字段您需要创建一个名为 firstResource 的变量。

    如果您想更新内部子元素,则必须创建变量,变量的路径由点分隔值 (.)。例如,要更新元素 groupOne 的子元素 firstFieldInOne 字段,我们必须创建一个名称为 :groupOne.firstFieldInOne 的变量

    因此,对于以上两个值的更新,我们必须在 yaml 或管道变量中创建以下变量:

    variables:
      firstResource: "firstResourceValue"
      groupOne.firstFieldInOne: "FirstField Value"
    

    【讨论】:

    • 感谢您的回答。我标记了它,虽然刚刚意识到只有在本地运行 azure 函数时才使用 local.settings.json...
    猜你喜欢
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多