Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit gitlab variables accross projects?

How can I let gitlab fill a global variable with from CI/CD secret, and then inherit this global variable in other projects?

templates/commons.yml:

variables:
  TEST_VAR: $FILLED_FROM_SECRETS

project/.gitlab_ci.yml.

include:
  - project: '/templates'
    ref: master
    file:
      - 'commons.yml'

test:
  stage: test
  script:
    - echo $TEST_VAR

Result: the variable is never set. Why? (of course the FILLED_FORM_SECRETS variable is set in the commons project)

like image 469
membersound Avatar asked Jan 26 '26 14:01

membersound


2 Answers

The problem you have is that include: only brings in the contents on the YAML file, not the project level settings or variables.

As possible alternatives, you can:

  • Set the variable in the template directly (not recommended for sensitive values)
  • Set variables set on your own self-hosted runners (note variables cannot be masked this way)
  • Set instance CI/CD variables
  • Set a required CI configuration to forcibly include a template to all projects (that template can include variables you need) (note variables cannot be masked this way)
  • Set group CI/CD variables (where all your projects live under the common group)
  • Retrieve your secrets using the vault integration or as part of your job script
like image 94
sytech Avatar answered Jan 29 '26 12:01

sytech


With the include keyword the included files are merged with the .gitlab-ci.yml and then your .gitlab-ci.yml is executed in the repo where the pipeline is triggered. Therefore, only gobal variables in this repo or inherited variables from any parent groups are known. That's why TEST_VAR is not substituted with the value from the secret as the variable is defined in another repository.

like image 24
danielnelz Avatar answered Jan 29 '26 12:01

danielnelz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!