Bruno Massa
Bruno Massa

Reputation: 105

Gitlab CI/CD: default value for environment variables

I would like to write a pipeline and include some default values that could be overridden by the user in the project CI/CD settings.

Could I do something like the .gitlab-ci.yml bellow? It's not clear in the documentation...

variables:
  VAR: $VAR || "default"

Or the user set tis value in the settings, or use the default value.

regards

Upvotes: 6

Views: 20623

Answers (1)

Mouson Chen
Mouson Chen

Reputation: 1224

Variables defined in .gitlab-ci.yml are overwritten by variables defined on the project level:

try-job:
  image: ubuntu:20.04
  variables:
    VAR1: 'var 1 set in gitlab-ci.yml'
    VAR2: 'var 2 set in gitlab-ci.yml'
  script:
    - echo $VAR1;
    - echo $VAR2;

When setting VAR2 variable manually during Pipeline trigger to var 2 set in project cicd variable

enter image description here

https://gitlab.com/mouson-gitlab-playground/gitlab-ci-parallel-test01/-/jobs/1821982396


The order of precedence for variables is (from highest to lowest):

  1. Trigger variables, scheduled pipeline variables, and manual pipeline run variables.
  2. Project variables.
  3. Group variables.
  4. Instance variables.
  5. Inherited variables.
  6. Variables defined in jobs in the .gitlab-ci.yml file.
  7. Variables defined outside of jobs (globally) in the .gitlab-ci.yml file.
  8. Deployment variables.
  9. Predefined variables.

ref: https://docs.gitlab.com/ee/ci/variables/index.html#cicd-variable-precedence

Upvotes: 9

Related Questions