Search code examples
azure-devopsazure-pipelinesazure-pipelines-yamlazure-pipelines-tasks

Variable values are not injected into local.settings.json


I have an Azure Function project. It includes local.settings.json configuration file with some secrets. Those values are retrieved and used by the xUnit testing project. Everything works well when run on Visual Studio, but in the Azure build pipeline, the values are not injected, and the file contains original placeholder values. The local.settings.json file has Build Action as "Content", and Copy to Output Directory as "Copy if newer". In the pipeline, I use a File Transform task to inject values. Here is a part of my pipeline:

        - 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          

What can I be doing wrong?


Solution

  • Not sure about the structure of your json file, and how the variables are defined. But according to your description, the variable values are not injected into local.settings.json. It seems that the variables are not defined correctly, or the json file is not found during running the pipeline.

    In targetFiles field provide the json file path directly or relatively, so if you have multiple appsettings files inside folder path you have given in first field than giving relative path will see for all the settings json file like this:'**/settings.json'.

    File transform task will update the fields inside the settings json file from the available variables based on name of variable matching with the field inside the settings json file. For example, take the below json file as our app settings json file:

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

    In this appsettings file if you want to replace the parent elements of the json file then you should add the variables inside the yaml pipeline or as a pipeline variable with the name of the element - for example, to update 'firstResource' field you need to create a variable with name firstResource.

    If you want to update inner child elements than you have to create variables with their path separated values by dot (.). For example, to update firstFieldInOne field which is a child of element groupOne we have to create a variable with name :groupOne.firstFieldInOne

    So, for above two values update we must create following variables in the yaml or in the pipeline variables:

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