Overriding variables defined in GitLab's UI in my gitlab-ci.yml script

Hi Gitlab forum,

Hope you are fine!
I’m here with an issue into my CI/CD script.

Problem to solve

Overriding variables defined in GitLab’s UI in my gitlab-ci.yml script

I have variables defined in Gitlab’s UI and I’ve created a gitlab-ci.yml file that extends another one.
I would like to override in my extended job the value to a defined variable

Steps to reproduce

I’ve defined 2 variables into GitLab’s UI, into CI/CD settings

  • SSH_SERVER_URL: the prod server SSH URL
  • SSH_SERVER_URL_DEMO: the demo server SSH URL

Here is my .gitlab-ci.yml defined in my project with the 2 following jobs:

include:
  - project: devops/skeleton
    ref: main
    file: ci-cd/deploy.yml

deploy_app_demo:
  extends: .deploy_skeleton
  variables:
    SSH_SERVER_URL: $SSH_SERVER_URL_DEMO
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - if: '$CI_COMMIT_BRANCH == "demo"'

deploy_app:
  extends: .deploy_skeleton

As you can see, the 2 jobs, depends on another one, which is defined into another file:

.deploy_skeleton:
  stage: deploy
  variables:
    SSH_SERVER_URL: $SSH_SERVER_URL
    PROJECT_NAME: $PROJECT_NAME
  dependencies:
    - build_app

   ....

My issue is the following one:
In my deploy_app_demo job, I want to override SSH_SERVER_URL with SSH_SERVER_URL_DEMO, defined in Gitlab’s UI setting.

Currently, it does not work. It seems that Gitlab’s UI defined variables take precedence on it (variable value is not overriden in my job logs).

Is there a way to tell to my job to override variables? I’ve tried using reference but not sure about the way to achieve it.

Thanks a lot for your help.

Best

Hi,

I believe that the problem here might be that you’re using extends, and the way how GitLab is merging those variables is described here. I myself don’t have much experience with extends, but you could have a deeper look into documentation and try to avoid this scenario by interpolating your config in a different way.

Alternatively, you could try using old-fashioned override, by doing it in a before_script section (unless you have this also defined in your skeleton job):

deploy_app_demo:
  extends: .deploy_skeleton
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - if: '$CI_COMMIT_BRANCH == "demo"'
  before_script:
    - SSH_SERVER_URL=$SSH_SERVER_URL_DEMO

Hope this helps! :slight_smile: