Skip to content

CI/CD - Default Services: Allow single service per pipeline variable

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

This is related to the Services feature under default section in Gitlab CI yaml file and described here services.

As stated above, the services keyword allows linking docker images to every single job in our pipeline.

It's all good, whenever I have, let's say, one build, one test, and one deploy job in my pipeline, I can link my image with API to my test job and run my tests and get test data from my API.

Let's assume we have multiple test jobs. They still can be run and whenever the test job is started, a new API docker container will be linked to that job.

But the issue arises when you have multiple test jobs that you want to run in parallel to save some time.

If you set up the services keyword in a default section of the YAML file instead of putting it inside every test job, you see that all test jobs are executed in parallel and your API service container is multiplied.

So if you have 5 test jobs, you will get 5 instances of your API image, one of them will be linked to every test job, which is not what I need. I need to run a pipeline with only one API image container and have it linked to every test job.

The issue is not critical, but I can't run my tests, since API contains a list of users with their credentials. Every test job should pick one user, lock it and let's say log in to the app. When every test job starts testing with the same user, it overrides the settings and breaks the test.

Proposal

My proposal would be to add a variable in services in the default section like below

default:
  services:
    - name: registry.gitlab.com/organization/private-api:latest
      alias: backend-api
  variables:
    SINGLETON_INSTANCE: true

that would make the Gitlab runner create only one instance of the service and link it to every single job, which does not override services with the keyword services inside the job.

Please advise if this feature is already available and I am missing out. Also, if it can be done differently or there are best practices for cases like this.

Edited by 🤖 GitLab Bot 🤖