[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Use environment variables\n\nYou can define\n[environment variables](https://wikipedia.org/wiki/Environment_variable) for\nWorkflows at deployment time. For example, you can create a workflow\nthat is dynamically configured depending on the environment to which it is\ndeployed. Or, you can create a workflow that can be reused as a template and\nconfigured according to separately maintained environment variables.\n\nEnvironment variables are set as arbitrary key and value string pairs that are\naccessible by your workflow at runtime. They are stored in the\nWorkflows backend, are scoped to the workflow execution, and\nare immutable during the execution of a workflow.\n\nAll environment variables are bound to a deployment of a workflow, and can\nonly be set or changed with a deployment. Creating or changing an environment\nvariable requires a successful deployment. If a deployment fails for any reason,\nany changes to environment variables are *not* applied.\n\nYou can add, update, or remove user-defined environment variables using the\nGoogle Cloud CLI.\n\nReserved names\n--------------\n\nThe [built-in environment variables](/workflows/docs/reference/environment-variables)\ndefined for Workflows are reserved and can't be set.\n\nNote that you can't use the following when defining environment variables for\nWorkflows:\n\nSet environment variables\n-------------------------\n\nYou can define new variables or replace existing variables when deploying a\nworkflow. To make additive changes, in this document, see\n[Update environment variables](#update-variable) instead. \n\n### Console\n\n1. In the Google Cloud console, go to the **Workflows** page:\n\n\n [Go to Workflows](https://console.cloud.google.com/workflows)\n\n \u003cbr /\u003e\n\n2. On the **Workflows** page, click add**Create**.\n\n3. On the **Create workflow** page, complete the appropriate fields to\n configure your workflow definition.\n\n4. In the **Environment variables (optional)** section, click **Add variable**.\n\n5. In the **Name 1** field, specify the variable name.\n\n6. In the **Value 1** field, specify the variable value.\n\n7. To add another variable, click **Add variable**.\n\n8. Click **Next**.\n\n9. After defining your workflow, to deploy it, click **Deploy**.\n\n### gcloud\n\nTo set an environment variable, use the `--set-env-vars` flag: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --set-env-vars KEY1=VALUE1\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eWORKFLOW_NAME\u003c/var\u003e: the ID of your workflow.\n- \u003cvar translate=\"no\"\u003eKEY1=VALUE1\u003c/var\u003e: the environment variable name and its value; for example, `MONTH=January`.\n\n### Terraform\n\nTo create a workflow, use the\n[`google_workflows_workflow` resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/workflows_workflow)\nand modify your `main.tf` file as shown in the sample. For more information,\nsee\n[Create a workflow by using Terraform](/workflows/docs/create-workflow-terraform).\n\nUse the `user_env_vars` argument to associate an environment variable with\nthe workflow revision.\n\nTo learn how to apply or remove a Terraform configuration, see\n[Basic Terraform commands](/docs/terraform/basic-commands).\n\n### Set multiple environment variables\n\nTo set multiple environment variables, use a comma-separated list: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --set-env-vars KEY1=VALUE1,KEY2=VALUE2\n```\n\n### Escape comma characters\n\nSince the comma character (`,`) is used to delimit environment variables, if\nyour variable values include commas, you must specify a different delimiter\ncharacter, for example, `@`: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --set-env-vars ^@^KEY1=VALUE1,VALUE2,VALUE3@KEY2=VALUE2\n```\n\n### Store variables in a file\n\nTo store your variables in a file (for example, under source control), use a\nYAML file and the `--env-vars-file` flag: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --env-vars-file FILE_PATH\n```\n\nReplace \u003cvar translate=\"no\"\u003eFILE_PATH\u003c/var\u003e with the path to a local YAML file\nthat lists the definitions for your environment variables. Note that the\nvariable names and values must be strings. All existing environment variables\nare removed by Workflows before the new environment variables\nare added.\n\nFor example, the contents of the YAML file might be the following: \n\n```yaml\nKEY1: \"value1\"\nKEY2: \"value2\"\n```\n\nFor more information about the `deploy` command, see\n[`gcloud workflows deploy`](/sdk/gcloud/reference/workflows/deploy).\n\nAccess environment variables\n----------------------------\n\nTo access an environment variable, make a call to the\n[`sys.get_env()`](/workflows/docs/reference/stdlib/sys/get_env) function in an\nexpression, passing the name of the environment variable as a parameter. The\nname of the environment variable must be passed as a string.\n\nFor example, the following workflow assigns the value of the environment\nvariable `KEY1` to a workflow variable called `keyValue`, and then outputs that\nvalue: \n\n```yaml\nmain:\n steps:\n - init:\n assign:\n - keyValue: ${sys.get_env(\"KEY1\")}\n - returnResult:\n return: ${keyValue}\n```\n\nUpdate environment variables\n----------------------------\n\nYou can update user-defined environment variables for existing workflows. This\nis a non-destructive approach that changes or adds environment variables, but\ndoesn't delete variables. \n\n### Console\n\n1. In the Google Cloud console, go to the **Workflows**\n page.\n\n\n [Go to Workflows](https://console.cloud.google.com/workflows)\n\n \u003cbr /\u003e\n\n2. Click the name of the workflow you want to update.\n\n The **Workflow details** page appears.\n3. To edit an existing environment variable, do either of the following:\n\n - Click the **Details** tab.\n\n 1. Beside **Environment variables** , click the edit icon.\n 2. Make your changes.\n 3. To deploy the updated workflow, click **Save**.\n - Click\n edit\n **Edit**.\n\n 1. In the **Environment variables (optional)** section, make your changes.\n 2. To deploy the updated workflow, click **Next** and then **Deploy**.\n\n### gcloud\n\nTo update a variable, use the `--update-env-vars` flag: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --update-env-vars KEY1=VALUE1\n```\n\nTo update multiple environment variables, use a comma-separated list: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --update-env-vars KEY1=VALUE1,KEY2=VALUE2\n```\n\nDelete environment variables\n----------------------------\n\nYou can delete user-defined environment variables for existing workflows. \n\n### Console\n\n1. In the Google Cloud console, go to the **Workflows**\n page.\n\n\n [Go to Workflows](https://console.cloud.google.com/workflows)\n\n \u003cbr /\u003e\n\n2. Click the name of the workflow you want to update.\n\n The **Workflow details** page appears.\n3. To delete an existing environment variable, do either of the following:\n\n - Click the **Details** tab.\n\n 1. Click the appropriate edit.\n 2. Beside the environment variable that you want to delete, click the delete icon.\n 3. To deploy the updated workflow, click **Save**.\n - Click\n edit\n **Edit**.\n\n 1. Beside the environment variable that you want to delete, click the delete icon.\n 2. To deploy the updated workflow, click **Next** and then **Deploy**.\n\n### gcloud\n\nIf you want to selectively remove environment variables, use the\n`--remove-env-vars` flag: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --remove-env-vars KEY1,KEY2\n```\n\nAlternatively, you can clear all previously set environment variables with the\n`--clear-env-vars` flag: \n\n```bash\ngcloud workflows deploy WORKFLOW_NAME \\\n --clear-env-vars\n```\n\nBest practices\n--------------\n\nAs a best practice, we recommend that you don't depend on or modify any\nenvironment variables that you haven't set explicitly. If you modify environment\nvariables other than those that you have set explicitly, it might lead to\nunintended consequences.\n\n### Managing secrets\n\nEnvironment variables can be used to configure workflows, but are not\nrecommended as a way to store and consume secrets such as database credentials\nor API keys. These sensitive values should be stored separately from both your\nsource code and environment variables, and not inadvertently sent to logs.\n\nFor storing secrets, we recommend that you review the best practices for\n[secret management](/secret-manager/docs/best-practices), and follow the instructions to\n[use Secret Manager with Workflows](/workflows/docs/use-secret-manager).\n\n### Naming conventions\n\nAs a general rule, we recommend that environment variable keys consist solely of\nuppercase letters, digits, and underscores (`_`), and that they don't begin with\na digit. Consider prefixing your user-defined environment variables with a\nunique key to avoid conflicts with other variables.\n\n### Size limits\n\nA maximum of 20 user-defined environment variables can be defined. Each\ndefinition string (`KEY=value`) is limited to 4 KiB.\n\nWhat's next\n-----------\n\n- [Execute a workflow](/workflows/docs/executing-workflow).\n- [Pass runtime arguments in an execution request](/workflows/docs/passing-runtime-arguments)."]]