4

I have a project in Gitlab having defined variables in the Gitlab UI in the project group folder. On a new branch of one of my git repositories I need to override some of these variables.

variables:
  GLOBALLY_DEFINED_VARIABLE: "branch_specific_value"

But in a concrete build the value is still the original one from the global variable. How do I get the value inside the .gitlab_ci.yml file to win over the project value?

I know, the docs (https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence) say that variable in the yml have lower precedence, but this does not make any sense (the yml is inside a project, so it normally must have higher precedence than the project). Is there any way to make my yml file win? Note: I do not want to rule out all variables! Most of them are still needed, I just need to overwrite some.

My use case is this: the new branch prepares the code base to be able to run on a new webserver. So this branch obviously needs to have variables storing paths and similar of the new server. But of course the main branches of all repositories should still build to the current (old) live server until the migration on the main-new-server branch is finished and tested. So on this preparation branch I must be able to override the old-server settings. The old settings are stored in project group variables because they are the same for many repositories, but now need to change in a single branch of a single repository.

foobar
  • 43
  • 1
  • 5
  • Maybe you can set [environment scope](https://docs.gitlab.com/ee/ci/environments/index.html) when you are creating project-level variables. Then in your jobs, you can set environment to `test` or `production` or `staging` and the values for these envs will differ based on what you have set. – AlirezaAsadi Apr 26 '22 at 12:25

2 Answers2

4

You could set the variable value from within your script or before_script:

variables:
  LOCALLY_DEFINED_VARIABLE: "branch_specific_value"
before_script:
  - GLOBALLY_DEFINED_VARIABLE=$LOCALLY_DEFINED_VARIABLE
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • Thanks. :) I still have to adjust the build scripts, but at leats in an easy and straightforward way. When asking the question I was expecting that Gitlab does something like this implicitly (set all variables to the latest value given in the execution tree: set to group value, then override with project, then override with YAML etc.). – foobar Apr 27 '22 at 06:03
1

Adding to Glen's solution, in case you have a lot of variables that needed to be overridden, I suggest a more automated solution:

  variables:
    LOCAL_WEB_HOST: localhost
    LOCAL_DB_HOST: db
    LOCAL_USERNAME: test

  before_script:
    - export LOCAL_ENV_VARS=$(env | grep LOCAL_)
    - export LOCAL_ENV_VARS=$(echo "$LOCAL_ENV_VARS" | perl -pe 's/LOCAL_//g')
    - export $(echo "$LOCAL_ENV_VARS" | xargs)

So vars from GitLab CI that I want to override are just prefixed with LOCAL_ and then the prefix is removed programmatically in the brefore_script

Ahmed Mohamedeen
  • 328
  • 3
  • 11