Gitlab CI/CD: default value for environment variables

Viewed 5175

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

1 Answers

project variable can overridden Variables defined in jobs in the .gitlab-ci.yml.

for example:

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

I am setting VAR2 variable is CI/CD variable: var 2 setting 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

Related