GitLab CI/CD allows users to run automated pipelines to build, test, and deploy applications. One of the key features of GitLab CI/CD is services, which enable running additional containers that support the main job execution. Services are commonly used to set up databases, caching layers, or other dependent services needed for tests and builds.
This blog post explains how GitLab services work with an example snippet and demonstrates practical use cases.
What Are GitLab Services?
In GitLab CI/CD, services are additional Docker containers that run alongside the primary job container. These containers provide dependencies such as databases, configuration servers, or message queues.
Services are especially useful when:
- Running integration tests that require external services.
- Setting up dependencies like databases, config servers, or caching layers.
- Ensuring test environments closely resemble production setups.
Example: Using a Spring Cloud Config Server as a Service
Let’s break down the example GitLab CI/CD configuration snippet you provided:
.config-vars:
services:
- name: hyness/spring-cloud-config-server:4.1-jre17
alias: localhost
before_script:
- set -o allexport
- source .env
- set +o allexport
- echo "Environment variables successfully set"
- echo "Waiting for config server to be ready..."
- timeout 30 bash -c 'until curl -s "http://localhost:8888/actuator/health" | grep UP; do sleep 5; done' || echo "Service not reachable"
- export SPRING_CLOUD_CONFIG_URI=http://localhost:8888/
Defining Services
services:
- name: hyness/spring-cloud-config-server:4.1-jre17
alias: localhost
- Here, the
hyness/spring-cloud-config-server:4.1-jre17
image is used as a service. - The alias
localhost
allows referencing this service usinghttp://localhost:8888/
.
- Setting Environment Variables
before_script:
- set -o allexport
- source .env
- set +o allexport
This snippet loads environment variables from an .env
file, making them available in the job.
2. Waiting for the Service to Start
- echo "Waiting for config server to be ready..."
- timeout 30 bash -c 'until curl -s "http://localhost:8888/actuator/health" | grep UP; do sleep 5; done' || echo "Service not reachable"
The script continuously checks whether the config server is running. If the server is UP
, the job continues; otherwise, it prints a failure message after 30 seconds.
3. Exporting the Config Server URL
- export SPRING_CLOUD_CONFIG_URI=http://localhost:8888/
This ensures that the application can access the configuration server by setting the appropriate environment variable.
4. Running Multiple Services
If your application depends on multiple services, you can define them under services
:
services:
- name: postgres:13
alias: db
- name: redis:latest
alias: cache
This ensures that both PostgreSQL and Redis are available for your pipeline.
5. Using a Custom Docker Network
To improve communication between services, you can define a custom Docker network:
variables:
DOCKER_DRIVER: overlay
services:
- name: mysql:latest
alias: mysql_db
command: ["--default-authentication-plugin=mysql_native_password"]
- name: rabbitmq:3-management
alias: rabbit
variables:
RABBITMQ_DEFAULT_USER: user
RABBITMQ_DEFAULT_PASS: password
6. Running Integration Tests with Services
If you need to run integration tests with services, use needs
to ensure proper sequencing:
stages:
- build
- test
build-job:
stage: build
script:
- echo "Building application..."integration-tests:
stage: test
services:
- name: mongo:latest
alias: mongo_db
script:
- echo "Running tests against MongoDB..."
needs: [build-job]
Benefits of Using Services in GitLab CI/CD
- Isolation: Each service runs in its own container, ensuring a clean test environment.
- Reproducibility: CI/CD pipelines remain consistent since services are always started with predefined configurations.
- Ease of Setup: Services eliminate the need for complex installation scripts by pulling ready-to-use Docker images.
Conclusion
GitLab services are an essential tool in CI/CD pipelines, enabling dependency management with minimal effort. Whether integrating a configuration server, database, or caching layer, services ensure that your CI/CD environment closely mirrors production. By leveraging GitLab services, teams can build more robust and reliable pipelines.