Deploy Appsettings for Azure function app using Azure Devops and Yaml and Variable group

1.1k Views Asked by At

Is there a way to deploy complete variable group variables to Azure Function app settings using Yaml.

Below is the yaml code and how I am deploying variable :-

variables:
- group: MyDevVariable #This is my variable group name

#Place where I am referring my variables from variable groups 
   deploy:
        steps:
            -task: AzureFunctionApp@1
             inputs:
               appSetting: '-Key1 $(Key1) -Key2 $(Key2)' #Key1 and Key2 are stored in variable group.

but if I have many variables, I have to mentions every variable in appSettings as key $(key).

So Is there a way to deploying all my variables from variable group to azure function app setting.

Any response is greatly appreciated. Thanks in advance.

1

There are 1 best solutions below

1
WaitingForGuacamole On BEST ANSWER

I don't believe that you can iterate the group's variables in the YAML directly. There's no syntax to reach into the group from what I've seen. What you can do is to write a script that gets the variables out of the group (if you have Azure CLI installed on the agent) and then reference it later:

deploy:
  steps:
  - task: AzureCLI@2
    inputs:
      name: GetSettings
      azureSubscription: Your Service Connection
      scriptType: ps
      scriptLocation: inlineScript
      inlineScript: |
        $env:AZURE_DEVOPS_EXT_PAT = '$(System.AccessToken)'
        $groupVars = (az pipelines variable-group list --group-name "My Group Name") | ConvertFrom-JSON
        $settingsList = ""
        $groupVars.variables.PSObject.Properties | ForEach-Object {
            $settingsList += "-$($_.Name) `"$($_.Value.value)`" "
        }
        Write-Host "##vso[task.setvariable variable=settingsList;isOutput=true]$settingsList"
        Write-Host "Using settings $settingsList"
  - task: AzureFunctionApp@1
    inputs:
      appSetting: $(GetSettings.settingsList)

NOTE: This may encounter issues with secret variables, in which case I'd store them in Azure Key Vault and use the key vault secrets task to read them into variables.