Azure pipeline task AzureContainerApps environment variables in DevOps

43 Views Asked by At

I have the 'AzureContainerApps@1' functioning and deploying. But each time the environment variables are reset (which i understand).

I also have a variable group, the key/values of this group needs to be passed into the container for the Node.js app to function.

My code below sets the build id, but the variable group has 50 values that need to be passed in with the same key/value.

Is there a smart way to do this so as not to have to edit the pipeline.yml file if a values is added to the variable group?

My task:

variables:
- group: cms-aeh-poc

     
task: AzureContainerApps@1
    inputs:
      azureSubscription: ${{parameters.container_service_resource_manger_connection}}
      containerAppName: ${{parameters.container_app_name}}
      resourceGroup: ${{parameters.resource_group}}
      imageToDeploy: "${{parameters.container_reg_login_server}}/${{parameters.repository}}:$(Build.BuildId)"
      environmentVariables: BUILD_ID=$(Build.BuildId)

For example the variable group has these values:

APP_NAME = my lovely app REGION = UK BASE_DIR = /lovely/stuff

I need these AND the BUILD_ID passed in

1

There are 1 best solutions below

0
Kevin Lu-MSFT On BEST ANSWER

I am afraid that there is no out-of-box method can read all variables in the variable group and pass them to environmentVariables of AzureContainerApps task.

To meet your requirement, I suggest that you can use the PowerShell script to run the Rest API: Variablegroups - Get to list all variable names in the variable group and add them to the string list.

Then you can define Pipeline variable to save the variable list and pass it to AzureContainerApps task.

For example:

variables:
 - group: cms-aeh-poc
steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $token = "$(PAT)"
      
      $url="https://dev.azure.com/{orgname}/{projectname}/_apis/distributedtask/variablegroups/{variablegroupid}?api-version=7.1-preview.2"
      
      $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
      
      $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
      
      $string=""
      
      foreach($variablename in $response.variables.psobject.properties.name)
      {
      
        $env = "$variablename=`$($variablename`)"
        
      
        $string= -join("$string", " ", "$env")
        
      }
      
      echo $string
      echo "##vso[task.setvariable variable=variablelist]$string"


- task: AzureContainerApps@1
  inputs:
    azureSubscription: 'xx'
    xxxx
    environmentVariables: 'BUILD_ID=$(Build.BuildId) $(variablelist)'