Azure / Azure/azure-cli

az containerapp update - cannot take environment variables with values containing spaces

Open
#30,396 16 comments 0 reactions 0 assignees View on GitHub
act-observability-squad Auto-Assign Auto-Resolve bug ContainerApp customer-reported Service Attention Similar-Issue
Dominant language
Python
Stars
4.6k
Forks
3.5k
Avg merge
3d 2h
Merged PRs (30d)
60

Description

### Describe the bug

It's not possible to pass environment variables with values that contain spaces to az containerapp update command with --replace-env-vars parameter.

### Related command

az containerapp update

### Errors

`ERROR: Environment variables must be in the format "= =secretref: ...".`

### Issue script & Debug output

```
RESOURCE_GROUP='some-rg'
CONTAINER_APP_NAME='some-container-app'
LOCATION='West Europe'
ACR_NAME='some-acr'
IMAGE_TAG='$(transformedDockerTag)'
ENV_FILE_PATH='$(Pipeline.Workspace)/BuildArtifact/Configuration/environment-variables.acc.env'

# az containerapp command cannot take environment variables from file directly, only via command line, such as:
# key1="value1" key2="value2" key3="value3"
# convert .env file to Azure CLI-compatible environment variables format
# remove \r from .env file because these Linux commands expect \n only as newline character
# trim trailing spaces
REPLACE_ENV_VARS=$(awk -F= 'NF==2 {
gsub(/\r/, "");
printf "%s=%s ", $1, $2
}' $ENV_FILE_PATH | tr -d '\r' | tr '\n' ' ')
REPLACE_ENV_VARS=$(echo $REPLACE_ENV_VARS | sed 's/[[:space:]]*$//')

echo The environment variables were successfully read from .env file and will be passed as --replace-env-vars argument of az command as follows:
echo $REPLACE_ENV_VARS

# deploy a new revision of the container app
az containerapp update \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--image $ACR_NAME/censored:$IMAGE_TAG \
--debug \
--replace-env-vars $REPLACE_ENV_VARS
```
Environment variables are read from an .env file in my solution where one line is one variable in key="value" format (quotes included)
The `echo $REPLACE_ENV_VARS` echoes the variables correctly, such as:
`ActiveDirectoryClientId="someguid" ActiveDirectoryClientSecret="something" OAuthUserExpirationFrom="Test User 07"`
but the az containerapp update command fails when there are spaces in the value of an environment variable. I have confirmed that removing spaces fixes the problem but I need the spaces there.
Also I need double quotes in the value, such as `"Test User 07" ` (this should be the environment variable value) and at first I thought that the double quotes are the culprit, but no, it's the spaces. I hope passing double quotes in variable value is also possible...

### Expected behavior

No error, just environment value being passed as is, along with spaces and other characters between double quotes.
Also it would be best to have support for reading environment variables from a file, avoiding this CLI hassle.

### Environment Summary

==============================================================================
Task : Azure CLI
Description : Run Azure CLI commands against an Azure subscription in a PowerShell Core/Shell script when running on Linux agent or PowerShell/PowerShell Core/Batch script when running on Windows agent.
Version : 2.248.1
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-cli
==============================================================================
/usr/bin/az --version
WARNING: You have 2 update(s) available. Consider updating your CLI installation with 'az upgrade'
azure-cli 2.66.0 *

core 2.66.0 *
telemetry 1.1.0

Extensions:
azure-devops 1.0.1

Dependencies:
msal 1.31.0
azure-mgmt-resource 23.1.1

Python location '/opt/az/bin/python3'
Extensions directory '/opt/az/azcliextensions'

Python (Linux) 3.12.7 (main, Oct 30 2024, 03:56:40) [GCC 11.4.0]

### Additional context

az is run inside a linux build agent on devops (ubuntu-latest) in an Azure CLI build task.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.