Search code examples
gitlabgitlab-cigitlab-ci.yml

How to override a gitlab group or project CD/CI variable in .gitlab-ci.yml?


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.


Solution

  • 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