-
由 Jessie Young 创作于
* Make these API docs more consistent with other endpoint docs: * Use `gitlab.example.com` instead of `example.gitlab.com` * Use `<your_access_token>` instead of `$YOUR_ACCESS_TOKEN` * Other small updates related to custom roles
由 Jessie Young 创作于* Make these API docs more consistent with other endpoint docs: * Use `gitlab.example.com` instead of `example.gitlab.com` * Use `<your_access_token>` instead of `$YOUR_ACCESS_TOKEN` * Other small updates related to custom roles
stage: Verify
group: Pipeline Security
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
type: reference
GitLab CI/CD variables (FREE ALL)
CI/CD variables are a type of environment variable. You can use them to:
- Control the behavior of jobs and pipelines.
- Store values you want to re-use.
- Avoid hard-coding values in your
.gitlab-ci.yml
file.
You can override variable values manually for a specific pipeline, or have them prefilled in manual pipelines.
Variable names are limited by the shell the runner uses to execute scripts. Each shell has its own set of reserved variable names.
To ensure consistent behavior, you should always put variable values in single or double quotes.
Variables are internally parsed by the Psych YAML parser,
so quoted and unquoted variables might be parsed differently. For example, VAR1: 012345
is interpreted as an octal value, so the value becomes 5349
, but VAR1: "012345"
is parsed
as a string with a value of 012345
.
For more information about advanced use of GitLab CI/CD, see 7 advanced GitLab CI workflow hacks shared by GitLab engineers.
Predefined CI/CD variables
GitLab CI/CD makes a set of predefined CI/CD variables available for use in pipeline configuration and job scripts. These variables contain information about the job, pipeline, and other values you might need when the pipeline is triggered or running.
You can use predefined CI/CD variables in your .gitlab-ci.yml
without declaring them first.
For example:
job1:
stage: test
script:
- echo "The job's stage is '$CI_JOB_STAGE'"
The script in this example outputs The job's stage is 'test'
.
.gitlab-ci.yml
file
Define a CI/CD variable in the To create a CI/CD variable in the .gitlab-ci.yml
file, define the variable and
value with the variables
keyword.
Variables saved in the .gitlab-ci.yml
file are visible to all users with access to
the repository, and should store only non-sensitive project configuration. For example,
the URL of a database saved in a DATABASE_URL
variable. Sensitive variables containing values
like secrets or keys should be stored in project settings.
You can use variables
in a job or at the top level of the .gitlab-ci.yml
file.
If the variable is defined:
- At the top level, it's globally available and all jobs can use it.
- In a job, only that job can use it.
For example:
variables:
GLOBAL_VAR: "A global variable"
job1:
variables:
JOB_VAR: "A job variable"
script:
- echo "Variables are '$GLOBAL_VAR' and '$JOB_VAR'"
job2:
script:
- echo "Variables are '$GLOBAL_VAR' and '$JOB_VAR'"