Sobes.tech
Senior

What is extends in GitLab CI? How does inheritance priority work?

sobes.tech AI

Answer from AI

In GitLab CI, the extends directive allows one job to inherit the configuration of another job or template. This helps avoid duplication of code in .gitlab-ci.yml and structures pipelines.

When using extends, you can specify one or more templates from which the configuration will be inherited. If there are identical keys in both the inherited and inheriting jobs, the value from the inheriting job takes precedence — it overrides the inherited one.

Example:

.base_job:
  image: python:3.8
  script:
    - echo "Base job"

job1:
  extends: .base_job
  script:
    - echo "Job1 script"

In this example, job1 inherits the image from .base_job, but overrides the script.

If you need to merge lists (for example, multiple scripts), the inheriting job will completely replace the list of the inherited one; merging does not happen automatically.

Thus, extends is a powerful tool for reusing configurations in GitLab CI, and the priority always belongs to the inheriting job in case of key conflicts.