Search code examples
azure-devopsazure-pipelinesazure-pipelines-yaml

How to Pass ADO Library to Azure Container Apps


The reference for AzureContainerApps@1 contains an argument environmentVariables which takes a string in space-separated {key}={value} format.

environmentVariables: foo=fooby bar=barby
// $foo -> "fooby, $bar -> "barby

You can pass yaml variables to this string, like so

environmentVariables: foo=($foo) bar=($bar)

However, this requires manually specifying every potential environment variable. How can I take all variables from an Azure Devops library and pass them to this function? Something like

variables:
  - group: myGroup

...
  - task: AzureContainerApps@1
    inputs:
      environmentVariables: $myGroup


Solution

  • I'm afraid there is currently no functionality to read all variables in Variable groups and pass them to Azure Container Apps task environment variables directly.

    Workaround:

    You can run the Rest API Variablegroups - Get to list all the variable names in Variable groups in a script task and add them to a list of strings. Then, you can set it as variables in script and pass them to the Azure Container Apps task.

    Example YAML with PowerShell script:

    variables:
    - group: myGroup  
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $url = "https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups/{groupId}?api-version=7.1-preview.2"
          $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(System.AccessToken)"}  -Method Get -ContentType application/json
          $myGroupVars = ""
          foreach ($var in $response.variables.PSObject.Properties) {
              $myGroupVars += "$($var.Name)=$($var.Value.value) "
          }
          Write-Host "$myGroupVars"
          Write-Host "##vso[task.setvariable variable=myGroupVars]$myGroupVars"
    
    - task: AzureContainerApps@1
      inputs:
        environmentVariables: '$(myGroupVars)'
        xxx: xxx
    

    You can find the {groupId} in the URL of the variable group.

    groupId