2

Say I have a main azure devops pipeline azure-pipelines.yml in which I call into my template deploy.yml.

In the main pipeline I'd like to be able to declare either a variable or a parameter of type hashset / map / dictionary or any other key value structure that I can then pass to the template.

I can see that it's possible to pass in an object type, but I can't wrap my head it's usage. How could I achieve the following?

Note the appSettings: {"key1":"value1","key2":"value2"} in azure-pipelines.yml is a fantasy, but showcases pretty well how I'd like this to work.

azure-pipelines.yml:

trigger:
 - main

- job: deploy
  pool:
    vmImage: ${{ parameters.poolVmImage }}
  steps:
  - template: deploy.yml
    parameters:
      azureServiceConnection: ${{ parameters.azureServiceConnection }}
      resourceGroupName: 'foo'
      appServiceName: 'bar'
      appSettings: {"key1":"value1","key2":"value2"}

deploy.yml:

parameters:
  - name: azureServiceConnection
  - name: resourceGroupName
  - name: appServiceName 
  - name: appSettings

steps:
- task: AzureCLI@2
  displayName: Deploy zip
  name: deployZip
  inputs:
    azureSubscription: ${{ parameters.azureServiceConnection }}
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az webapp deployment source config-zip \
        -g ${{ parameters.resourceGroupName }} \
        -n ${{ parameters.appServiceName }} \
        --src ./deployment.zip

      az webapp config appsettings set \
        -g ${{ parameters.resourceGroupName }} \
        -n ${{ parameters.appServiceName }} \
        --settings ${{ parameters.appSettings }}
Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
pijemcolu
  • 2,257
  • 22
  • 36
  • Looks like this is addressed extensively here https://stackoverflow.com/questions/59972329/how-to-pass-complex-devops-pipeline-template-parameter-to-script – GenericDisplayName Dec 28 '20 at 20:12
  • Does this answer your question? [How to pass complex DevOps pipeline template parameter to script](https://stackoverflow.com/questions/59972329/how-to-pass-complex-devops-pipeline-template-parameter-to-script) – GenericDisplayName Dec 28 '20 at 20:13
  • Hi @pijemcolu. Is there any update about this ticket? Feel free to let me know if the answer could give you some help. Just a remind of [this](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Kevin Lu-MSFT Dec 30 '20 at 01:04

1 Answers1

2

How could I achieve the following?

You can indeed use object type parameters.

But in the template, you cannot use multiple object parameters at once, so you need to use each expression to loop through each object.

For example: - ${{ each setting in parameters.appSettings }}:

Here is my sample, you could refer to:

Deploy.yml:

parameters:
  - name: azureServiceConnection
  - name: resourceGroupName
  - name: appServiceName 
  - name: appSettings
    type: object
    default: []

steps:

- task: AzureCLI@2
  displayName: Deploy zip
  name: deployZip
  inputs:
    azureSubscription: ${{ parameters.azureServiceConnection }}
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az webapp deployment source config-zip \
        -g ${{ parameters.resourceGroupName }} \
        -n ${{ parameters.appServiceName }} \
        --src ./deployment.zip


- ${{ each setting in parameters.appSettings }}: 
  - task: AzureCLI@2
    displayName: Deploy settings
    inputs:
      azureSubscription: ${{ parameters.azureServiceConnection }}
      scriptType: 'bash'
      scriptLocation: 'inlineScript'
      inlineScript: |
        az webapp config appsettings set \
          -g ${{ parameters.resourceGroupName }} \
          -n ${{ parameters.appServiceName }} \
          --settings ${{ setting }}

azure-pipelines.yml:

trigger:
 - none

parameters:
- name: InstanceArgs 
  type: object
  default: [key2=value2,key3=value3]
jobs:
- job: deploy
  pool:
    vmImage: windows-latest
  steps:

  - template: deploy.yml
    parameters:
      azureServiceConnection: '${{ parameters.azureServiceConnection }}'
      resourceGroupName: 'foo'
      appServiceName: 'bar'
      appSettings: ${{ parameters.InstanceArgs }}

Note: Since the parameters in the template are obejct type , the same type of parameters need to be set in the main yaml file to pass the object.

Workflow: Pass the object type parameters to the template. The template will run the deploy zip command first. Then it will traverse each object passed, and use the config settings command to pass them to the webapp one by one.

Result:

enter image description here

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Brilliant.Great example. – Oliver Nilsen Jan 16 '21 at 17:25
  • Kevin Lu-MSFT - can you please also, share your input on this question, i have a similar question, but sort of different, i.e. i am trying to use if else logic inside a template, how can i go about it? https://stackoverflow.com/questions/70682947/using-if-else-conditions-within-a-azure-devops-yaml-pipeline-template – lavoizer Jan 12 '22 at 16:19