Pass env. specific values to Azure pipeline: create/edit variable groups in Azure Pipelines

I need to deploy a Service Fabric Application package to one of 15 devtest environments. I have a Service Connection to pass in the environment details, which is not an issue. The challenge is setting the other environment specific variables based on the target environment.

I tried using the Service Connection name to pick one of several variable template files, but UI variables are not set at compile time so the template expressions fail. I cannot use a pipeline variable as QA would have to make a file change and check it in each time they want to deploy to a different environment.

Currently, I have an empty variable template and a powershell script that sets the values based on different script names:

        - task: PowerShell@2
          inputs:
            targetType: 'filePath'
            filePath: '$(Build.ArtifactStagingDirectory)\drop\Deployment\Code\Scripts\Set-$(DevConnection)Variables.ps1'
            #arguments: # Optional
          displayName: Set environment variables

Is there a better way to set the environment specific variables for a Service Fabric Application deployment?

One approach to set the environment specific variables for a Service Fabric Application deployment is to use the “Replace Tokens” task from the Marketplace. This task can replace tokens in a file with variable values during the release process.

To use this task, you can create a tokenized version of the environment specific variables file, where the variable values are replaced with tokens. Then, in the release pipeline, you can add the “Replace Tokens” task to replace the tokens with the actual variable values based on the target environment.

Here is an example YAML snippet to replace tokens in a file named “appsettings.$(DevConnection).json”:

- task: replacetokens@3
  inputs:
    rootDirectory: '$(System.DefaultWorkingDirectory)'
    targetFiles: '**/appsettings.$(DevConnection).json'
    encoding: 'auto'
    writeBOM: true
    actionOnMissing: 'warn'
    keepToken: false
    tokenPrefix: '#{'
    tokenSuffix: '}#'

In this example, the task replaces tokens in all files named “appsettings.{environment}.json” in the release directory. The token prefix and suffix are set to “#{” and “}#” respectively, so a variable named “MyVariable” would be replaced with “#{MyVariable}#” in the file.

You can then set the environment specific variables as pipeline variables or variables in the release definition, and reference them in the tokenized files using the token syntax. For example, if you have a variable named “MyVariable” with value “123”, you can use the token “#{MyVariable}#” in the tokenized file, and it will be replaced with “123” during the release process.