Junior
What is the 'services' entity in GitLab CI?
sobes.tech AI
Answer from AI
The services entity in GitLab CI is a way to define additional containers that will be run during the execution of a job. These containers are typically used for third-party services needed for the main job container, such as databases, message brokers, or caches.
Main points:
- Job dependencies: Services provide dependencies necessary for executing steps in the job's
script. - Isolated environment: Each service runs in its own container, isolated from other services and the main job container.
- Access by name: GitLab Runner automatically links the job container with the service containers. The job container can access services by their aliases (or by image name if no alias is specified).
- Restrictions: The number and resources of services can be limited by GitLab Runner settings.
Example usage:
# .gitlab-ci.yml
test_job:
image: python:3.9
services:
- name: postgres:latest # Service image name
alias: db # Alias for access from the main container
script:
# Example of connecting to a database from the main container
- pip install psycopg2-binary
- python -c "import psycopg2; conn = psycopg2.connect(host='db', user='postgres', password='mysecretpassword', database='mydatabase'); print('Successfully connected to DB!')" # Access via alias 'db'
In this example, the test_job uses a Python image. It requires a PostgreSQL database to run. The services section specifies that GitLab Runner should launch a container with the postgres:latest image and make it available in the main job container under the alias db. The job script then connects to the database using this alias db.