ERROR: (gcloud.run.deploy) argument --set-env-vars: Bad syntax for dict arg

3.8k Views Asked by At

I'm using Cloud Code (extension for Visual Studio Code) and during the deploy, via UI, I'm trying to set the Environment Variables field like this:

KEY1:value1
KEY2:value2,value3

But I'm having this error:

Failed to deploy the app. Error: ERROR: (gcloud.run.deploy) argument --set-env-vars: Bad syntax for dict arg: [value3]. Please see gcloud topic flags-file or gcloud topic escaping for information on providing list or dictionary flag values with special characters. ,Usage: gcloud run deploy [[SERVICE] --namespace=NAMESPACE] [optional flags] optional flags may be --add-cloudsql-instances | --allow-unauthenticated | --args | --async | --binary-authorization | --breakglass | --clear-binary-authorization | --clear-cloudsql-instances | --clear-config-maps | --clear-env-vars | --clear-key | --clear-labels | --clear-post-key-revocation-action-type | --clear-secrets | --clear-vpc-connector | --cluster | --cluster-location | --command | --concurrency | --connectivity | --context | --cpu | --cpu-throttling | --env-vars-file | --help | --image | --ingress | --key | --kubeconfig | --labels | --max-instances | --memory | --min-instances | --namespace | --platform | --port | --post-key-revocation-action-type | --region | --remove-cloudsql-instances | --remove-config-maps | --remove-env-vars | --remove-labels | --remove-secrets | --revision-suffix | --service-account | --set-cloudsql-instances | --set-config-maps | --set-env-vars | --set-secrets | --source | --tag | --timeout | --no-traffic | --update-config-maps | --update-env-vars | --update-labels | --update-secrets | --use-http2 | --vpc-connector | --vpc-egress For detailed information on this command and its flags, run: gcloud run deploy --help

So it seems the comma needs to be escaped. How to do that via Cloud Code UI, please?

4

There are 4 best solutions below

0
On

As mentioned, putting a comma in between values in an env var when using the --set-env-vars or --update-env-vars commands will get interpreted as declaring a new env var (https://cloud.google.com/sdk/gcloud/reference/topic/escaping).

A workaround I found was to swap out the comma for another punctuation mark/separator, e.g a semi-colon and then adapt my code to this new separator:

--set-env-vars KEY="val1;val2;val3"

I then just updated my Python code to something like below:

env_var = os.environ.get('KEY')
env_var_values = env_var.split(';') #instead of env_var.split(',')
0
On

For now I could solve that by making "Environment Variables" UI field empty and using a Dockerfile to set the variables, where this syntax works:

ENV KEY1='value1'
ENV KEY2='value2,value3'
2
On

If you set the env-var like this: --set-env-var "A=B,C,D" to gcloud, it will treat the comma (,) character as another environment variable declaration and will try to split the value into multiple environment variables. This is explained here in detail.

However, to prevent splitting with commas, you need to specify a different custom delimiter that you’re sure won’t occur in your value string, such as ##:

--set-env-vars "^##^A=B,C,D"

You may also use a format like this as mentioned in the official docs:

--set-env-vars "^@^KEY1=value1,value2,value3@KEY2=..."
0
On

I don't think there is a work around here. We are working to fix this through https://github.com/GoogleCloudPlatform/cloud-code-vscode/issues/560.