Skip to content

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:

yaml
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: never

Why worker:build without changes? Prod deploys every image with the same SHA. See History.

3. The root pipeline ​

In .gitlab-ci.yml:

yaml
include:
  - local: api/.gitlab-ci.yml
  - local: web/.gitlab-ci.yml
  - local: worker/.gitlab-ci.yml

4. The Compose files ​

infra/docker-compose.dev.yml:

yaml
  worker:
    build:
      context: ../worker
      dockerfile: Dockerfile.dev
    restart: unless-stopped
    networks:
      - internal

infra/docker-compose.prod.yml:

yaml
  worker:
    image: ${CI_REGISTRY_IMAGE}/worker:${CI_COMMIT_SHORT_SHA}
    networks:
      - internal
    deploy:
      update_config:
        order: start-first
        failure_action: rollback

The 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:

yaml
- test "$(docker service ls --format '{{.Image}}' | grep -c "$CI_COMMIT_SHORT_SHA")" = 3

Do 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:

yaml
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:

  1. add the DNS record status.trinity.jostud.com pointing to trinity-prod's IP;
  2. add the service to the trinity network in the Compose file;
  3. 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 serviceroot include + both Compose files + the deploy-prod number + Environments + The pipeline
a domainone route per server + DNS + re-run the traefik role + Traefik
a secret variableSettings → CI/CD → Variables + the Compose file + Secrets + Reference
an open portthe ufw role + Reference
an Ansible roleplaybook.yml + Ansible