skip to Main Content

I’m trying to update my json file using FileTransform@2 by using variables from the Azure DevOps Library to populate these, but I am just getting an error that the value is already there.

##[error]Failed to apply JSON variable substitution. Changes are already present in the package.

This is part of my YAML file:

stages:
  - stage: UpdateSettingsFile
    displayName: 'Update settings file with ADO values'
    jobs:
      - job: UpdateSettings
        steps:
          - task: FileTransform@2
            displayName: 'Transform JSON Configuration'
            inputs:
              folderPath: '**/'
              fileType: 'json'
              jsonTargetFiles: '**/${{ parameters.powerPlatformEnvironment }}-deployment-settings.json'
              JSONVariableSubstitution: true 
              Overwrite: true  # Overwrite the existing file

and this is an example of my .json file

{
  "EnvironmentVariables": [
      {
          "SchemaName": "firstEmail",
          "Value": "$(firstEmail)"
      }
]
}

Should the value be left blank? How can i get around this?

The values should be updating in the .json file to import to Power Platform

2

Answers


  1. You can use extension Replace Tokens, which can replace tokens in text based files with variable value. For example,

    my.json:

    {
      "EnvironmentVariables": [
          {
              "SchemaName": "firstEmail",
              "Value": "$(firstEmail)"
          }
    ]
    }
    

    YAML file:

    variables:
    - group: Test
    
    steps:
      - task: replacetokens@6
        inputs:
          sources: 'my.json'
          tokenPattern: 'azpipelines'
      - task: CmdLine@2
        inputs:
          script: 'cat my.json'
    

    Variable Group:

    enter image description here

    Result:

    enter image description here

    Login or Signup to reply.
  2. The reason of the issue is that you defined the variable with wrong name. To use the FileTransform@2 task to perform variable substitution in JSON file, you need to must define the variable using JSONPath expressions.

    See below example as reference:

    1. The sample of JSON file (config.json). And want to use the FileTransform@2 task to replace "[email protected]" and "[email protected]" with two new values.

      // config.json
      
      {
          "EnvironmentVariables": [
              {
                  "SchemaName": "firstEmail",
                  "Value": "[email protected]"
              },
              {
                  "SchemaName": "secondEmail",
                  "Value": "[email protected]"
              }
          ]
      }
      

      enter image description here

    2. The sample of pipeline main YAML (azure-pipelines.yml).

      # azure-pipelines.yml
      
      # Define the pipeline variables with the new values to replace the old value in JSON file.
      # The names of variables must use JSONPath expressions.
      variables:
        EnvironmentVariables.0.Value: '[email protected]'
        EnvironmentVariables.1.Value: '[email protected]'
      
      steps:
      - bash: |
          cat config.json
        displayName: 'Print JSON before Variable substitution'
      
      - task: FileTransform@2
        displayName: 'Variable substitution for JSON'
        inputs:
          folderPath: ''
          xmlTransformationRules: ''
          jsonTargetFiles: 'config.json'
      
      - bash: |
          cat config.json
        displayName: 'Print JSON after Variable substitution'
      
    3. The result.

      enter image description here

    Related documentations:


    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search