Search code examples
dockerservicegitlabyamlgitlab-ci

Gitlab-ci - Services


I have a service and some stages in my yml. I only need to use the service for a specific stage. Unfortunately, however, the service loads the image for each stage, lengthening the execution times. Is there a way to prevent a service from being performed or used by every stage inside the ci?

Thank you!


Solution

  • It's hard to say for sure without your .gitlab-ci.yml file, but it sounds like you defined the service(s) at the "default" level (that applies to every job in the pipeline) instead of the job level (that applies just to that job). Take a look at the examples in the docs (https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#define-image-and-services-from-gitlab-ciyml):

    Services for entire pipeline

    default:
      image: ruby:2.6
    
      services:
        - postgres:11.7
    
      before_script:
        - bundle install
    
    test:
      script:
        - bundle exec rake spec
    

    Here, the postgres:11.7 service will be run for each and every job in a pipeline because it's defined outside of any jobs, in the "default" configuration. If only setting one keyword, it might also look like this:

    
    services:
      - postgres:11.7
    
    test:
      script:
        - bundle exec rake spec
    

    Service for a specific job

    test:2.6:
      image: ruby:2.6
      services:
        - postgres:11.7
      script:
        - bundle exec rake spec
    
    test:2.7:
      image: ruby:2.7
      services:
        - postgres:12.2
      script:
        - bundle exec rake spec
    

    Here, each job has its own services and image configuration, so the service will only be used for that job, and once it finishes, the service will be terminated. This is useful if say you wanted to test your code against multiple versions of a database, etc.