Environment variables are a common way to customize a pipeline and can be used at any stage. This topic describes the types of environment variables—built-in, custom, and common variable groups—and explains how to use them.
Sources of environment variables
Built-in variables
Alibaba Cloud DevOps pipelines provide built-in variables related to basic information and code sources. You can use these variables to customize your workflow.
|
Module |
Parameter |
Description |
|
Basic information |
PIPELINE_ID |
The ID of the pipeline. |
|
BUILD_NUMBER |
The run number of the pipeline, which starts at 1 and increments with each run. |
|
|
PIPELINE_NAME |
The name of the pipeline. Example: |
|
|
BUILD_REMARK |
The notes for the pipeline run. |
|
|
BUILD_EXECUTOR |
The user who triggered the pipeline. Example: |
|
|
BUILD_MESSAGE |
The trigger information for the pipeline. Example: |
|
|
PROJECT_DIR |
The working directory for running commands. Example: |
|
|
DATETIME |
The current time. Example: |
|
|
TIMESTAMP |
The current timestamp. Example: |
|
|
Code source (single) |
CI_SOURCE_NAME |
The name of the code source. |
|
CI_COMMIT_REF_NAME |
The branch or tag name of the code source, selected during the pipeline run. Example: |
|
|
CI_COMMIT_TITLE |
The message of the last commit. |
|
|
CI_COMMIT_SHA |
The full commit ID of the last commit. Example: |
|
|
CI_COMMIT_ID |
The 8-character short commit ID of the last commit (for Git). The revision number of the last commit (for SVN). |
|
|
Code sources (multiple) |
CI_SOURCE_NAME_n |
The name of the nth code source. |
|
CI_COMMIT_REF_NAME_n |
The branch or tag name of the nth code source, selected during the pipeline run. Example: |
|
|
CI_COMMIT_TITLE_n |
The message of the last commit for the nth code source. |
|
|
CI_COMMIT_SHA_n |
The full commit ID of the last commit for the nth code source. Example: |
|
|
CI_COMMIT_ID_n |
The 8-character short commit ID of the last commit for the nth code source. |
|
|
Artifact source |
CI_SOURCE_NAME |
The name of the artifact source. |
|
CI_SOURCE_URL |
The URL of the artifact source. Example: |
|
|
CI_VERSION_NAME |
The version name of the artifact source, selected during the pipeline run. Example: |
To access information for a specific code or artifact source when multiple exist, append a numeric suffix to the variable name.
For example, a pipeline can have a Git code source (source 1), an artifact source (source 2), and an SVN code source (source 3). After the pipeline runs, you can view the actual values of all variables in the run details. Numeric suffixes distinguish variables for multiple sources, such as CI_COMMIT_REF_NAME_1 and CI_COMMIT_REF_NAME_2.
The value of the CI_COMMIT_REF_NAME variable depends on the specific branch or tag you select when you trigger the pipeline. If you manually trigger the pipeline and select the master branch, the value of CI_COMMIT_REF_NAME is master. If you select the V1.0 tag, its value is V1.0. This runtime selection lets you run the pipeline on different branches or tags, and the variable's value changes accordingly.
When you trigger a pipeline run, you can select a specific branch or tag in the run dialog box. The selected value becomes the runtime value of the CI_COMMIT_REF_NAME variable.
Pay attention to the following limitations and special scenarios when you use built-in variables:
-
Multi-source variable ambiguity — When a pipeline has multiple code sources, do not use unindexed variables such as
CI_COMMIT_REF_NAMEdirectly. Although the system defaults to the first source, the result is unpredictable. Always use indexed variables with numeric suffixes, such asCI_COMMIT_REF_NAME_1orCI_COMMIT_REF_NAME_2, to reference a specific code source. -
Git variable prerequisites — Git-related built-in variables such as
CI_COMMIT_TITLE,CI_COMMIT_SHA, andCI_COMMIT_IDare populated only after a code checkout step runs in the pipeline. If no code is checked out, these variables are empty. -
Variable processing limitations — Alibaba Cloud DevOps Flow does not support direct string operations such as substring extraction or regular expression matching on built-in variables (for example,
CI_COMMIT_ID). To process a built-in variable value, assign it to a custom variable in a shell script and then perform the operations on the custom variable:# Assign a built-in variable to a custom variable for processing MY_SHORT_SHA=$(echo $CI_COMMIT_SHA | cut -c1-8) echo "MY_SHORT_SHA=$MY_SHORT_SHA" >> "$FLOW_ENV" -
CI_WORKSPACE scope — The
CI_WORKSPACEvariable is valid only within the current build environment. In host deployment (ECS) scenarios, this variable may be empty or point to an unexpected path. Do not rely onCI_WORKSPACEto locate files on the ECS instance. Use absolute paths or configure a custom variable with the target directory path instead. -
Missing built-in variables — Built-in variables do not include the current repository project name or the full Git URL. To obtain this information, configure a custom variable manually or construct the URL by combining the Codeup domain with the repository path.
Custom variables
In addition to built-in variables, Alibaba Cloud DevOps Flow supports custom variables. Each custom variable is scoped to the pipeline where it is defined. To create a custom variable, select a pipeline, click Edit, and then go to the Variables and Cache tab. Alibaba Cloud DevOps Flow supports string and enumeration variable types.
This tab contains configuration areas for string variables and runtime-selected variables.
String variables
-
On the Variables and Cache tab, in the String Variables section, click New Variable.
-
Enter a Variable Name and a Default Value. You can also configure private mode and runtime settings.
-
Variable Name: The name cannot contain hyphens (-).
-
private mode: When enabled, this mode hides the value, preventing it from appearing in execution logs. Use this for sensitive information like usernames and passwords.
-
runtime settings: Controls whether the variable's value is required at runtime. When enabled, you must provide a value each time you run the pipeline.
-
-
You can add more variables or delete existing ones.
-
Click Add and then save the pipeline. You can now use the variable as described in Use environment variables.
Runtime-selected variables
-
On the Variables and Cache tab, in the Runtime-Selected Variables section, click New Variable.
-
Enter a Variable Name and define the Options.
-
Click Add Option to add multiple choices for the variable's value.
-
Enable Default Value for an option to make it the default selection.
-
-
Click Add and then save the pipeline. You can now use the variable as described in Use environment variables.
-
When you run the pipeline, you will be prompted to select a value for this variable.
Best practices for custom variables
-
Avoid naming conflicts — Custom variable names must not conflict with system reserved words or built-in parameters such as
image,tag, orversion. Conflicts may cause unexpected errors in image build or deployment steps. Variable names must start with a letter, contain only letters, digits, and underscores, and be 1 to 64 characters in length. -
Handle special characters in variable values — When a variable value such as a password contains special characters like
@, directly concatenating the value in a URL causes format errors. URL-encode the special characters (for example, replace@with%40), or use private mode variables to let the system handle authentication automatically. -
Dynamic code source permissions — The pipeline does not automatically authenticate with the executor's identity when you access a dynamically configured code repository. To access a private repository, configure the Git username and access token as private variables, or add the repository as a pipeline source to inherit access permissions.
-
Escape variable references in YAML files — In scenarios such as
kubectl apply, the${}syntax in YAML files may be incorrectly interpreted as pipeline variable references. To prevent this, enable the Variable Validation option to disable automatic variable parsing, or use the escape syntax$${}to preserve the original string. For example, write$${2}instead of${2}.
Common variable groups
A common variable group is a collection of environment variables that your organization manages centrally. You can associate a pipeline with a group to use its variables.
-
On the Variables and Cache tab, in the Common Variable Group section, click Associate Variable Group. Select a variable group from the drop-down list and click OK to link it to the pipeline.
-
In this section, you can view details of the associated variable groups or unlink them. After saving the pipeline, you can use the variables as described in Use environment variables.
Use environment variables
After you define variables, you can reference them anywhere in the pipeline by using the ${XXX} syntax. The system resolves variables in the following order of precedence:
-
Step output variables > Pipeline runtime input variables > Pipeline variables > Common variable group variables.
-
If you associate a pipeline with multiple common variable groups that contain a variable with the same name, the value from the most recently associated group takes precedence.
The following sections provide examples of common use cases for environment variables, including command execution, host deployments, image build arguments, configuration files, and for passing data.
Use variables in commands
You can use a variable to change a parameter in a configuration file. For example, to change the value of the key parameter in the a.conf file from 123 to the value of an environment variable named abc.
In a shell script step, use the sed command to replace the parameter value with the environment variable's value.
$ cat a.conf
key=123
$ sed -i "s/key=123/key=${abc}/g" a.conf
$ cat a.conf
key=abc_value
In the pipeline Variables and Cache, set the default value of the variable abc to abc_value.
Variables in host deployment
You can reference environment variables directly in your deployment script by using the ${XXX} syntax to control the host deployment logic. After the pipeline runs, you can check the logs of the host deployment step to verify that the system passed the variable's value correctly.
Variables as image build arguments
If you need to use pipeline environment variables as arguments during an image build, configure them as follows.
-
In the image build step, use a specified container environment. In the build arguments, add a custom argument and assign the environment variable to it by using the
${XXX}format. The system passes the argument to the build command by using the--build-argflag.
In the Build Parameters of the image build step, add a custom argument such as abc=${abc}. This argument is passed to the Docker build command as a --build-arg.
-
In your Dockerfile, reference the argument by using
ARG argName.
FROM <base-image>
ARG argName
RUN echo $argName
Use variables in configuration files
If you need to modify a parameter in a configuration file by using an environment variable, follow these steps. For example, to replace the username parameter in the a.conf file with a pipeline variable:
-
In the a.conf file, change the line to
username = ${abc}. -
In the pipeline's environment variables, configure the
abcvariable and set its default value tomy_name_is_hanmeimei. -
Add a job to the pipeline. From the Utilities section, add the Replace EnvVar in File step. Enter the path to the configuration file in the Source File Path field. The Target File Path is optional. If you specify a target path, the step creates a new file. Otherwise, it modifies the original file.
In this example, the Source File Path is set to a.conf, and the Target File Path is set to b.conf.
-
Check the result. The step replaces the
usernameplaceholder ina.confwith the variable's value in the newb.conffile, which now containsmy_name_is_hanmeimei. Note: Steps share a workspace only when they are in the same job.
Passing variables
The environment variables defined on the pipeline configuration page are static. However, you may need to generate an environment variable from the output of a running step and pass it to subsequent steps or jobs. You can do this in two ways:
-
Pass variables within a job: Step 1 generates a custom environment variable, and Step 2 uses it.
-
Pass variables between jobs: Job 1 generates an environment variable, and Job 2 uses it.
Important: You must use different syntax for different build environments. The syntax for injecting environment variables is: in a default environment, use echo 'USER_abc=123' > .env (the variable name must start with USER_); in a specified container environment or default VM environment, use echo "yaojia_Test=myParam" >> "$FLOW_ENV".
In Job 1, you can add a step by navigating to Add Step > Utilities > Set Variables to promote the environment variable to the pipeline level.
Default environment
Pass variables within a job
In this scenario, you share environment variables within a single job. For example, Step 1 generates the variable USER_abc=123, and Step 2 references it by using ${USER_abc}.
In a preceding step, you can inject the environment variable by running echo 'USER_abc=123' > .env to write the variable to the .env file. Note: Environment variables in the .env file must have names that start with USER_.
# Step 1: Inject the environment variable
echo 'USER_abc=123' > .env
# Step 2: Reference the environment variable
echo $USER_abc
123
# Step 1: Write the environment variable to .env
echo 'USER_abc=123' > .env
# Step 2: Read the environment variable
echo $USER_abc
123
Pass variables between jobs
In this scenario, you share environment variables between multiple jobs in a pipeline. For example, Job 1 generates the variable USER_abc=123, and Job 2 references it by using ${USER_abc}.
-
In a step in Job 1, inject the environment variable by writing it to the
.envfile. Note: The variable name must start withUSER_. -
In Job 1, add another step by choosing to promote the environment variable to the pipeline level.
-
In Job 2, you can then use ${USER_abc} to reference the variable.
After Job 1 injects and promotes the variable, Job 2 can reference it by using ${USER_abc}.
# Execution log of Job 2
echo $USER_abc
123
# [Success]
Usage notes for the default environment
-
Use append mode to prevent overwriting — When you write variables to the
.envfile, use the append operator (>>). Do not use the overwrite operator (>), because it replaces all previously written variables.# Correct: append mode preserves existing variables echo 'USER_abc=123' >> .env echo 'USER_xyz=456' >> .env # Incorrect: overwrite mode — USER_abc is lost echo 'USER_abc=123' > .env echo 'USER_xyz=456' > .env -
Same-step read limitation — After you write a variable to the
.envfile by usingecho, you cannot immediately read the variable in the same step by referencing$KEY. Variables are loaded into the runtime context only after the current step finishes. To verify a variable value, read it in a subsequent step. -
Kubernetes deployment workaround — If a variable defined in a shell script step is unavailable or returns a default value in a Kubernetes deployment step, add a Set Variables step (navigate to ) after the shell step. In this step, explicitly assign the variable to a new variable name that the Kubernetes step can reference.
-
Extract a version number dynamically — To extract a version number from a build artifact such as
pom.xmland pass it to downstream steps:# Extract the version from pom.xml VERSION=$(grep -m1 '<version>' pom.xml | sed 's/.*<version>\(.*\)<\/version>.*/\1/') echo "USER_VERSION=$VERSION" >> .env
Container or VM environment
Pass variables within a job
In this scenario, you share environment variables within a single job. For example, Step 1 generates the variable yaojia_Test=myParam, and Step 2 references it by using ${yaojia_Test}.
In a preceding step, you can inject the environment variable by running echo "yaojia_Test=myParam" >> "$FLOW_ENV" to append the variable to the $FLOW_ENV file.
# Execution log of Step 2
echo $yaojia_Test
myParam
# [Success]
Pass variables between jobs
In this scenario, you share environment variables between multiple jobs in a pipeline. For example, Job 1 generates the variable yaojia_Test=myParam, and Job 2 references it by using ${yaojia_Test}.
-
In a step in Job 1, inject the environment variable by appending it to the
$FLOW_ENVfile. -
In Job 1, add another step by choosing to promote the environment variable to the pipeline level.
-
In Job 2, you can then use ${yaojia_Test} to reference the variable.
After Job 1 injects and promotes the variable, Job 2 can reference it by using ${yaojia_Test}.
# Execution log of Job 2
echo $yaojia_Test
myParam
# [Success]
Usage notes for the container or VM environment
-
Use append mode to prevent overwriting — When you write variables to
$FLOW_ENV, use the append operator (>>). Do not use the overwrite operator (>), because it replaces all previously written variables.# Correct: append mode preserves existing variables echo "VAR_A=value1" >> "$FLOW_ENV" echo "VAR_B=value2" >> "$FLOW_ENV" # Incorrect: overwrite mode — VAR_A is lost echo "VAR_A=value1" > "$FLOW_ENV" echo "VAR_B=value2" > "$FLOW_ENV" -
Same-step read limitation — After you write a variable to
$FLOW_ENVby usingecho, you cannot immediately read the variable in the same step by referencing$KEY. Variables are loaded into the runtime context only after the current step finishes. To verify a variable value, read it in a subsequent step. -
Kubernetes deployment workaround — If a variable defined in a shell script step is unavailable or returns a default value in a Kubernetes deployment step, add a Set Variables step (navigate to ) after the shell step. In this step, explicitly assign the variable to a new variable name that the Kubernetes step can reference.
-
Extract a version number dynamically — To extract a version number from a build artifact such as
pom.xmland pass it to downstream Docker image build steps:# Extract the version from pom.xml VERSION=$(grep -m1 '<version>' pom.xml | sed 's/.*<version>\(.*\)<\/version>.*/\1/') echo "IMAGE_VERSION=$VERSION" >> "$FLOW_ENV" -
Troubleshoot variable setting issues — If variables do not take effect or cause errors in a container environment, verify the following:
-
You have added a Set Variables step to promote the variable to the pipeline level.
-
The syntax matches the target environment. Use
echo "KEY=VALUE" >> "$FLOW_ENV"for the container or VM environment. Do not useecho 'USER_KEY=VALUE' > .env, which applies only to the default environment.
-
FAQ
How do I use environment variables for multi-environment deployments or to dynamically select build targets?
-
Multi-environment deployment — Create separate pipelines for different stages such as development, testing, and production. In each pipeline, configure custom variables with values specific to the target environment, such as environment identifiers or configuration file paths. This approach enables differentiated deployments without modifying your build scripts.
-
Dynamic build target selection — Alibaba Cloud DevOps Flow does not support selecting specific build targets (such as a particular JAR file or service module) from the pipeline UI. As a workaround, define a runtime-selected variable to specify the target service directory path. Reference this variable in your build commands to dynamically select the build target, and configure the artifact upload path to include only the directory that corresponds to the selected variable value.