GitLab
RabbitMQ
CI/CD
Configuration
YAML

How to use Rabbit inside a gitlab-ci.yml file?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When it comes to Continuous Integration/Continuous Deployment (CI/CD) pipelines in GitLab, the gitlab-ci.yml file is central. This YAML file defines the configurations for your CI/CD jobs running on GitLab CI. In this detailed guide, we'll discuss how to use RabbitMQ, a robust messaging broker, inside a gitlab-ci.yml file, providing an example and explanations of the process.

Understanding RabbitMQ:

RabbitMQ is an open-source message broker that enables applications to communicate with each other and scale applications effectively. It supports multiple messaging protocols, message queuing, delivery acknowledgment, and flexible routing to queues. Using RabbitMQ in a GitLab CI environment can be particularly useful for integration tests where applications need to interact with RabbitMQ or to ensure services communicate properly before deployment.

Integrating RabbitMQ in GitLab CI:

To use RabbitMQ within GitLab CI, you generally have three options:

  1. Use RabbitMQ Docker image: The simplest approach is to use the official RabbitMQ Docker image from Docker Hub.
  2. Include a RabbitMQ service: GitLab CI allows you to define service containers that run alongside the job.
  3. Install RabbitMQ manually: This method involves manually installing RabbitMQ on the runner. It is more complex and typically not recommended for most use cases.

1. Using RabbitMQ Docker Image:

The following is an example of how to integrate the RabbitMQ Docker image in a gitlab-ci.yml file for running integration tests:

yaml
1integration_test:
2  stage: test
3  image: python:3.8
4  services:
5    - name: rabbitmq:latest
6      alias: rabbitmq
7  script:
8    - apt-get update && apt-get install -y amqp-tools
9    - amqp-declare-queue --url=amqp://guest:guest@rabbitmq/%2F --queue=test_queue
10    - python -m unittest discover tests
11  tags:
12    - docker

Explanation:

  • image: python:3.8: This specifies the Docker image to use for the job. Here, we are using Python 3.8.
  • services: This section defines Docker services that should be available during the job. rabbitmq:latest pulls the latest official RabbitMQ image, and alias: rabbitmq allows us to refer to this service as rabbitmq.
  • script: These commands are executed as part of the job. It includes installing amqp-tools to interact with RabbitMQ, declaring a queue, and running Python unit tests.

2. Including RabbitMQ as a Service:

If you want RabbitMQ to persist across multiple jobs or stages, it can be useful to define it at the top level of your .gitlab-ci.yml to make it available to multiple jobs.

yaml
1services:
2  - rabbitmq:latest
3
4stages:
5  - test
6
7unit_test:
8  stage: test
9  script:
10    - echo "Run unit tests"
11
12integration_test:
13  stage: test
14  script:
15    - echo "Run integration tests with RabbitMQ"

3. Manually Installing RabbitMQ:

This method is less common but might be necessary for customized environments that require specific RabbitMQ configurations not supported by the Docker image.

yaml
1before_script:
2  - apt-get update && apt-get install -y rabbitmq-server
3  - rabbitmq-plugins enable rabbitmq_management
4  - systemctl start rabbitmq-server
5  - rabbitmqctl add_user test test
6  - rabbitmqctl set_permissions -p / test ".*" ".*" ".*"
7
8job:
9  script:
10    - echo "Job that uses RabbitMQ"

When to Use Each Method:

MethodUse Case
RabbitMQ Docker ImageFor default RabbitMQ setups with minimal to no customization.
Include as a Top-Level ServiceWhen RabbitMQ needs to be accessed by multiple jobs/stages.
Manual InstallationFor customized RabbitMQ setups on self-hosted runners.

Conclusion:

Incorporating RabbitMQ into your GitLab CI pipeline can significantly improve the robustness of your testing and deployment processes by enabling services to communicate in a decoupled manner. Whether you're handling microservices, APIs, or simply need to ensure application components work together flawlessly, RabbitMQ offers a proven solution that integrates seamlessly with GitLab's CI/CD workflow. By choosing the appropriate integration method based on your project needs, you can ensure consistent and reliable message passing within your CI pipelines.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.