Dedicated variables for services
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Problem to solve (revised 2025-09-02)
- Currently, environment variables defined in
.gitlab-ci.yml(either at global or job scope) are available to both services and jobs - This creates potential conflicts when the same variable name means different things for a service versus a job
- Users cannot set different values for the same variable name between services and jobs
- This is particularly problematic for common variable names like
PORTthat might be used by multiple services
Problem statement - original
A service can depend on some specific environment variables to set up the environment, for example, it would require a variable named DB to set up the database.
Inside of .gitlab-ci.yml it is already possible to define variables at a global level or at a job scope like the examples below.
global scope
services:
- postgres:latest
variables:
DB: nice_marmot
USER: runner
PASSWORD: ""
job_name:
...
job scope
job_name:
services:
- postgres:latest
variables:
POSTGRES_DB: nice_marmot
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""
...
The problem with this is, that it will be available for both the service and the job itself, which might not be the wanted behavior since the environment variable means different things for the service and the job, which leads into a conflict where the user can't set different values to the same key for the service and the job.
Proposal
Allow the user to specify variables scoped specifically for the service.
services:
- name: my-postgres:9.4
alias: db-postgres
entrypoint: ["/usr/local/bin/db-postgres"]
command: ["start"]
variables:
VARIABLE_FOR_SERVIVE: ONLY_AVAILABLE_TO_SERVICE
Orginal Request
We can define services and also variables.
The variables are environment variables for the main job.
A service may need also need a custom configuration using environment variables.
For example, Elasticsearch should have an environment variable discovery.type=single-node when run in a CI pipeline as a single node.
We can already configure many things for a service. It would be great if we could configure variables, too.