Azure container app set environment variables

894 Views Asked by At

I am trying to set the environment variables of container in azure container app but getting issue when there is space on the value. my .env is as below

BD_PASSWORD=v=K1i2
WS_PROJECT_AR_DSO=https://example.com
AUTO_BATCH_SUBMIT_CRON_START_TIME=*/2 * * * *
EMAIL_WHITELIST=["a.com","b.net"]

I tried with key=value space-separated, but it failed I tried "key1=value" "key2=value2" , but it again failed

How to send space separated and/or value with contains double quotes like Email whitelist value. I am using Azure CLI with the following command

  az containerapp update \
  --name ca-devpi \
  --resource-group rg-dev-pi \
  --image ${{ secrets.ACR_SERVER }}/dev-pi:latest  \
  --container-name dev-pi \
  --cpu 1.0 --memory 2Gi \
  --min-replicas 0 --max-replicas 2 \
  --scale-rule-name my-http-rule \
  --scale-rule-type http \
  --scale-rule-http-concurrency 15 \
  --set-env-vars $SPACE_SEPARATED_VALUES

And the error I am getting

 ERROR: Environment variables must be in the format "<key>=<value> <key>=secretref:<value> ...".

Update

After trying the suggestion from @Azeem and @SiddheshDesai. I can see similar behavior. i.e. if I print $SPACE_SEPARATED_VALUES I can see values are in the format of "AUTO_BATCH_SUBMIT_CRON_START_TIME=*/2 * * * *" "EMAIL_WHITELIST=[\"a.com\",\"b.net\"]"

but I pass the same variable as --set-env-vars $SPACE_SEPARATED_VALUES then I am getting the same error that

ERROR: Environment variables must be in the format "<key>=<value> <key>=secretref:<value> ...

If i pass with double quotes like this

--set-env-vars "$SPACE_SEPARATED_VALUES"

then I am getting error

ERROR: (ContainerAppInvalidEnvVarName) Env variable name '"AUTO_BATCH_SUBMIT_CRON_START_TIME' contains invalid character, regex used for validation is '^[-._a-zA-Z][-._a-zA-Z0-9]*$'

And I pass direct values like this

--set-env-vars "AUTO_BATCH_SUBMIT_CRON_START_TIME=*/2 * * * *" "EMAIL_WHITELIST=[\"a.com\",\"b.net\"]"

then it works. Now this my be because of Azure CLI shell because this is how I am using

- name: Update Container app
        uses: azure/CLI@v1
        with:
          inlineScript: |
            az containerapp update \
            --name ${{ vars.DBA_APP }} \
            --resource-group ${{ vars.CONTAINER_RG }} \
            --set-env-vars $SPACE_SEPARATED_VALUES
2

There are 2 best solutions below

3
On

I tried the below Azure CLI command and put the env vars in double quotes like below:-

My Github Action workflow:-

Complete workflow here

on: [push]

name: AzureLoginSample

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Login to Azure
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Upload to blob storage
        uses: azure/CLI@v1
        with:
          inlineScript: |
            az containerapp update \
              --name valleycontainerapp95 \
              --resource-group siliconrg98 \
              --image mcr.microsoft.com/k8se/quickstart:latest \
              --container-name simple-hello-world-container \
              --cpu 1.0 --memory 2Gi \
              --min-replicas 0 --max-replicas 2 \
              --scale-rule-name my-http-rule \
              --scale-rule-type http \
              --scale-rule-http-concurrency 15 \
              --set-env-vars "BD_PASSWORD=v=K1i2 WS_PROJECT_AR_DSO=example.com" "AUTO_BATCH_SUBMIT_CRON_START_TIME=*/2 * * * *" "EMAIL_WHITELIST=[\"a.com\",\"b.net\"]"

Output:-

enter image description here

Portal:-

enter image description here

0
On

If anyone came here looking for how to add multiple environment variables dynamically by using a variable here's a solution I found running az containerapp command in bash:

#!/bin/bash

# I usually run some commands to parse a json into a key=value space separated string
env_vars='"key1=val1" "key2=val2" "key3=val3"' 
create_revision_cmd="az containerapp update \
  -n $CONTAINERAPP \
  -g $RESOURCE_GROUP \
  --image "$IMAGE" \
  --replace-env-vars $env_vars" # or --set-env-vars

cmd_output=$(eval $create_revision_cmd)