Sam
Sam

Reputation: 14586

Azure : appsettings.json variable substitution in AzureRmWebAppDeployment task

In my azure pipeline I used to substitute variables in appsettings.json file the following way :

  1. My appsettings file defines variables with token values

     {
      "AzureAd": {
      "Instance": "__AAD_Instance__",
      "Domain": "__AAD_Domain__",
      "TenantId": "__AAD_TenantId__",
      "ClientId": "__AAD_AuthorPortalApi_ClientId__"
     }
    
  2. In the release, pipeline variables are defined

enter image description here

  1. A tokenizer task runs at the beginning of the release pipeline and substitutes the tokens with the correct value.

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.

Upvotes: 3

Views: 7560

Answers (2)

Sam
Sam

Reputation: 14586

In the end I decided to use variable groups in the library tab :

enter image description here

And then in my Yaml pipeline I simply reference the right group according to the stage that is running:

variables: 
- group: 'dev'  

Upvotes: 1

Leo Liu
Leo Liu

Reputation: 76720

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:

enter image description here

The test result:

enter image description here

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.

Upvotes: 1

Related Questions