0

I've got a pipeline I've created that deploys some app settings to all our websites (multiple per file), and I'm trying to conditionally set a variable within the JSON structure without any sort of anti-patterns.

For conditional variables, all we have this syntax:

${{ if endsWith('EXISTING_STRING','MATCH_STRING') }}:

${{ else }}:

This syntax assumes that you're going to put the entirety of the string after condition, though. If my JSON structure is 50 lines long, I don't want to do that. Is there a way to do this without that cluttered code, whilst avoiding any sort of anti-patterns as well?

Here's the Azure Piplines Module:

- task: AzureAppServiceSettings@1
displayName: 'Deploy App Settings'
inputs:
  azureSubscription: "${{ parameters.resource_group_name }}"
  appName: "wz${{ parameters.default_environment }}"
  resourceGroupName: "${{ parameters.resource_group_name }}"
  appSettings: |
     [
     {
        "name": "LaunchUrl",
        "value": "False", 
        /* ^^^^ 
          Value I want to be conditional based off:
            if endsWith( parameters['default_environment'], 'matchString'
          The resulting string also contains parameters.default_environment FYI
        */
        "slotSetting": true
    },
    {
        "name": "DisableFHIR",
        "value": "False",
        "slotSetting": true
    },
    ...

Thanks

1 Answer 1

2

Putting conditionals inside a string literal is unfortunately not supported. However, one technique my team uses is define our JSON values as a string parameter and populate it with variables that are evaluated at runtime.

parameters:
- name: appSettings
  type: string
  default: |
    '[
       { 
         "name": "appSetting1",
         "value": "$(appSetting1)",
         "slotSetting": "true"
       }
    ]'

Personally, I like to go one further and define them as a yaml parameter and then use the convertToJson expression. This avoids the potential for malformed JSON and I can also put comments in the YAML.

parameters:
- name: resource_group_name
  type: string

- name: appSettings
  type: object
  default:
    - name: 'LaunchUrl'
      value: '$(launchUrl)'
      slotSetting: true
    - name: 'DisableFHIR'
      value: 'false'
      slotSetting: true

job:
  variables:

    ${{ if endsWith('prd', parameters.resource_group_name) }}:
       launchUrl: https://production.dot.com
    ${{ else }}:
       launchUrl: http://somewhere.else.com

  steps:

  - task: AzureAppServiceSettings@1
    inputs:
      azureSubscription: "${{ parameters.resource_group_name }}"
      appName: "wz${{ parameters.default_environment }}"
      resourceGroupName: "${{ parameters.resource_group_name }}"
      appSettings: ${{ convertToJson(parameters.appSettings) }}
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm I definitely like this convertToJson approach! Assuming this doesn't cause any issues with evaluating runtime parameters in the end-result, this sounds like it might solve my issues! I'll follow up in the next week and get you some well awaited credit haha. Ty!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.