Override predefined variable in extended job

Hi,

I have a gitlab-ci root in my repo that include locally another CI in subfolder repo.
In this one, I have extended the job created in gitlab-ci root with predefined variable, cf below:

.gitlab-ci.yml in root repo:

---

image:
  name: gcr.io/kaniko-project/executor:debug
  entrypoint: [""]

variables:
  CI_DEBUG_TRACE: "false"

stages:
  - build
  - push
  # - run

.build-job:
  stage: build
  before_script:
    - docker info
  script:
    - env
    - echo "Building Dockerfile $CI_REGISTRY/$STACK:$VERSION on branch $CI_COMMIT_BRANCH"
    - VERSION=$(cat $CI_PROJECT_DIR/VERSION | cut -d " " -f1)
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor -cache=true --cache-ttl=6h --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --build-arg --destination $CI_REGISTRY/$STACK:$VERSION

include:
  - local: '/administration/agent/agent.gitlab-ci.yml'

agent.gitlab-ci.yml in /administration/agent repo:

---

build-job-agent:
  variables:
    STACK: "agent"
  extends: .build-job

I understood with include that the job running is specified in the agent.gitlab-ci.
CI_BUILD_NAME=build-job-agent in my pipeline
The problem is that all predefined env var are setup like the job was executed from the root gitlab-ci.
For example, CI_PROJECT_DIR has the value of the path root gitlab-ci whereas I would it has the value of the path of agent.gitlab-ci.

I didnt find anyhting about this in documentation or gitlab issues.

Do you have an idea about a solution to override predefined value on extended jobs ?

Thanks

Hi @bdoublet91

There is only 1 project root. Including files from sub-directory doesn’t change your working directory.
Override the variable on the shell:

.build-job:
  ...
  script:
    - env
    - export CI_PROJECT_DIR="$CI_PROJECT_DIR/$STACK"
  ...

build-job-agent:
  variables:
    STACK: "agent"
  extends: .build-job

Ok it what I did but I thought there was a better solution with predefined env var.

Logically, If I use extend keyword in sub gitlab file, it that I want to execute pipeline from subfolder and not from root folder so I don’t really understand why gitlab didn’t override predefined env var with sub gitlab-ci file.

Thanks for your solution.

As I said, project root is only 1. It doesn’t override CI_PROJECT_DIR, because common practice is to have all GitLab CI files in one directory named for example.gitlab. CI files are usually not stored in the same directories like code. And imho they shouldn’t be.

There will be a better solution where you can use variables inside variables like this: `MY_PROJECT_DIR: “$CI_PROJECT_DIR/$STACK” or similar, but that feature is just coming to GitLab.com