Add a service
This tutorial adds a new service end to end: its image, its CI jobs, its dev and prod deployment, its Traefik route and its docs.
A made-up example
We add a worker service, a Python background task living in worker/ with no web page, and a status page on status.trinity.jostud.com. Neither exists in the project: they are only examples.
1. The Dockerfiles
worker/Dockerfile.dev and worker/Dockerfile.prod, modelled on api/. In prod: multi-stage, non-root user, and a HEALTHCHECK, without which Swarm cannot do a zero-downtime update.
2. The CI jobs
worker/.gitlab-ci.yml, modelled on api/.gitlab-ci.yml:
worker:test:
stage: test
image: python:3.11-slim
script:
- cd worker
- pip install -r requirements.txt
- pytest --junitxml=report.xml
artifacts:
when: always
reports:
junit: worker/report.xml
rules:
- changes:
- worker/**/*
worker:build:
# copy api:build, replacing api with worker
rules:
- if: '$CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "main"'
- when: neverWhy worker:build without changes? Prod deploys every image with the same SHA. See History.
3. The root pipeline
In .gitlab-ci.yml:
include:
- local: api/.gitlab-ci.yml
- local: web/.gitlab-ci.yml
- local: worker/.gitlab-ci.yml4. The Compose files
infra/docker-compose.dev.yml:
worker:
build:
context: ../worker
dockerfile: Dockerfile.dev
restart: unless-stopped
networks:
- internalinfra/docker-compose.prod.yml:
worker:
image: ${CI_REGISTRY_IMAGE}/worker:${CI_COMMIT_SHORT_SHA}
networks:
- internal
deploy:
update_config:
order: start-first
failure_action: rollbackThe worker has no web page: it is only on internal, not on trinity.
5. The deploy-prod check
The job counts the services running the commit's image. With a third service, change 2 to 3:
- test "$(docker service ls --format '{{.Image}}' | grep -c "$CI_COMMIT_SHORT_SHA")" = 3Do not forget this number
Otherwise, deploy-prod fails on every deployment, although everything is fine.
6. The Traefik route and DNS
Only for a service reachable from outside, like the status page. One file per server, for example infra/traefik/dynamic/prod/status.yml:
http:
routers:
prod-status:
rule: "Host(`status.trinity.jostud.com`)"
entryPoints:
- websecure
tls:
certResolver: letsencrypt
service: prod-status
services:
prod-status:
loadBalancer:
servers:
- url: "http://trinity_status:8080"Then:
- add the DNS record
status.trinity.jostud.compointing totrinity-prod's IP; - add the service to the
trinitynetwork in the Compose file; - re-run
ansible-playbook playbook.yml --tags traefik --ask-vault-pass.
7. Secrets
A password or key for the new service: a masked and protected CI/CD variable, referenced as ${MY_VARIABLE} in the Compose file. Never in plain text. See Secrets and access.
8. The docs
In the same merge request, in French and in English.
Final checklist
| If you add… | Also update… |
|---|---|
| a service | root include + both Compose files + the deploy-prod number + Environments + The pipeline |
| a domain | one route per server + DNS + re-run the traefik role + Traefik |
| a secret variable | Settings → CI/CD → Variables + the Compose file + Secrets + Reference |
| an open port | the ufw role + Reference |
| an Ansible role | playbook.yml + Ansible |