In my azure pipeline I used to substitute variables in appsettings.json file the following way :
My appsettings file defines variables with token values
{
"AzureAd": {
"Instance": "__AAD_Instance__",
"Domain": "__AAD_Domain__",
"TenantId": "__AAD_TenantId__",
"ClientId": "__AAD_AuthorPortalApi_ClientId__"
}
In the release, pipeline variables are defined

Note that some variables are similar across different applications. For instance above I have two applications (AuthorPortalApi and FileApi) that both have a AzureAD ClientId, so I must include the name of the application in the token in order to replace it with the correct value.
This all works really well but now I am changing to use YAML release pipeline. The only difference is that now the variables are defined in a dedicated YAML file:
variables:
- name: AAD_FilesApi_ClientId
value: some value
I could keep it this way. But I have noticed that the task for deploying azure webapp AzureRmWebAppDeployment provides a way to do variable substitution in appsettings.json:
- task: AzureRmWebAppDeployment@4
displayName: 'Deploy my api'
inputs:
azureSubscription: 'my sub'
WebAppName: 'my-api'
packageForLinux: '$(workFolder)/my-api.zip'
JSONFiles: '**/appsettings.json'
In a variable yaml file, I have defined my variables like this:
variables:
- name: AzureAd.ClientId
value: some value
It works, but like I said above, if two applications have the same json structure in appsettings.json, then how do I differentiate them to replace by the correct values ?
I have seen the documentation, but it does not show how to define the variables in a YAML template.
if two applications have the same json structure in appsettings.json, then how do I differentiate them to replace by the correct values ?
We could create two job template yaml for the pipeline with different variables.
Template AuthorPortalApi.yml:
jobs:
- job: AuthorPortalApi
variables:
AAD_AuthorPortalApi_ClientId: xxx
Template FilesApi.yml:
jobs:
- job: FilesApi
variables:
AAD_FilesApi_ClientId: xxx
You could check the document Define variables:Variable scopes for some more details.
On the other hand, we could also just use one task template with Runtime parameters:
parameters:
- name: ClientId
displayName: AzureAd.ClientId
type: string
default: Vlaue_AAD_AuthorPortalApi_ClientId
values:
- Value_AAD_FilesApi_ClientId
- Value_OhterThings_ClientId
jobs:
- job: build
displayName: build
pool:
vmImage: ubuntu-latest
steps:
- script: echo The value is ${{ parameters.ClientId }}
Select the value when we queue the pipeline or use the default value Vlaue_AAD_AuthorPortalApi_ClientId:

The test result:

Update:
If none of the above methods are suitable for you, then the method I can think of is to use template parameters, but this method requires us to find a condition to pass different parameters to the template:
jobs:
- template: ClientId.yml
parameters:
ClientId: AAD_AuthorPortalApi_ClientId
condition: and(succeeded(), eq(variables['xx.xx'], 'xx'))
- template: ClientId.yml
parameters:
ClientId: AAD_FilesApi_ClientId
condition: and(succeeded(), eq(variables['yy.yy'], 'yy'))
Check this document Job, stage, and step templates with parameters for some more details.
In the end I decided to use variable groups in the library tab :

And then in my Yaml pipeline I simply reference the right group according to the stage that is running:
variables:
- group: 'dev'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With