Aller au contenu
CI/CD & Automatisation medium

Pipelines parent-enfant GitLab CI/CD

10 min de lecture

logo gitlab

Votre pipeline a 50 jobs et devient ingérable ? Les pipelines parent-enfant permettent de découper un pipeline complexe en sous-pipelines indépendants. Le parent orchestre, les enfants exécutent.

Avant de continuer, assurez-vous de maîtriser :

Sans parent-enfantAvec parent-enfant
Un seul .gitlab-ci.yml géantFichiers séparés par composant
Difficile à maintenirResponsabilité claire
Tout s’exécute toujoursExécution conditionnelle par composant
Un échec bloque toutIsolation des problèmes

pipeline parent-enfants

# .gitlab-ci.yml (racine du projet)
stages:
- triggers
trigger_frontend:
stage: triggers
trigger:
include: frontend/.gitlab-ci.yml
strategy: depend
trigger_backend:
stage: triggers
trigger:
include: backend/.gitlab-ci.yml
strategy: depend
frontend/.gitlab-ci.yml
stages:
- build
- test
build:
stage: build
script:
- npm ci
- npm run build
test:
stage: test
script:
- npm test
trigger_job:
trigger:
include: path/to/child.yml

Par défaut, le parent n’attend pas la fin de l’enfant. Avec strategy: depend :

  • Le parent attend la fin de l’enfant
  • Le statut du parent dépend du statut de l’enfant
trigger_job:
trigger:
include: child.yml
strategy: depend # ✅ Attend la fin
StrategyComportement
dependLe parent attend l’enfant et hérite du statut
mirrorLe job trigger miroire le statut du downstream (utile avec auto-cancel)
trigger_job:
trigger:
include: child.yml
strategy: mirror # Miroir strict du statut enfant
trigger_job:
trigger:
include: child.yml
forward:
yaml_variables: true # Variables définies dans le YAML
pipeline_variables: true # Variables passées au pipeline (web, API)
trigger_frontend:
variables:
ENVIRONMENT: "staging"
VERSION: $CI_COMMIT_SHA
trigger:
include: frontend/.gitlab-ci.yml
strategy: depend

L’enfant peut utiliser $ENVIRONMENT et $VERSION.

prepare:
stage: prepare
script:
- echo "VERSION=1.2.3" >> build.env
artifacts:
reports:
dotenv: build.env
trigger_deploy:
stage: deploy
needs: ["prepare"]
variables:
VERSION: $VERSION # Provient de build.env
trigger:
include: deploy.yml
strategy: depend

Pour récupérer des artefacts d’un pipeline amont, GitLab fournit des mécanismes dédiés.

Parent → Child (même projet) : needs:pipeline:job

Section intitulée « Parent → Child (même projet) : needs:pipeline:job »

Parent : produire un artefact + passer l’ID du pipeline au child.

build_artifacts:
stage: build
script:
- echo "artifact from parent" > artifact.txt
artifacts:
paths:
- artifact.txt
trigger_child:
stage: deploy
trigger:
include: path/to/child-pipeline.yml
strategy: depend
variables:
PARENT_PIPELINE_ID: $CI_PIPELINE_ID

Child : récupérer l’artefact depuis le pipeline amont.

test_child:
stage: test
script:
- cat artifact.txt
needs:
- pipeline: $PARENT_PIPELINE_ID
job: build_artifacts

Dans le pipeline aval :

test_downstream:
stage: test
script:
- cat artifact.txt
needs:
- project: my/upstream_project
job: build_artifacts
ref: main
artifacts: true
trigger_other_project:
trigger:
project: group/other-project
branch: main
strategy: depend
deploy_production:
variables:
DEPLOY_ENV: "production"
APP_VERSION: $CI_COMMIT_TAG
trigger:
project: devops/deployment
branch: main
strategy: depend

Structure :

monorepo/
├── .gitlab-ci.yml # Parent
├── frontend/
│ ├── .gitlab-ci.yml # Enfant frontend
│ └── src/
├── backend/
│ ├── .gitlab-ci.yml # Enfant backend
│ └── src/
└── shared/
└── utils/
.gitlab-ci.yml
stages:
- prepare
- build
- deploy
# Déterminer ce qui a changé
changes:
stage: prepare
script:
- |
if git diff --name-only HEAD~1 | grep -q "^frontend/"; then
echo "FRONTEND_CHANGED=true" >> build.env
fi
if git diff --name-only HEAD~1 | grep -q "^backend/"; then
echo "BACKEND_CHANGED=true" >> build.env
fi
artifacts:
reports:
dotenv: build.env
# Déclencher frontend si modifié
trigger_frontend:
stage: build
needs: ["changes"]
trigger:
include: frontend/.gitlab-ci.yml
strategy: depend
rules:
- if: $FRONTEND_CHANGED == "true"
# Déclencher backend si modifié
trigger_backend:
stage: build
needs: ["changes"]
trigger:
include: backend/.gitlab-ci.yml
strategy: depend
rules:
- if: $BACKEND_CHANGED == "true"
frontend/.gitlab-ci.yml
stages:
- install
- build
- test
install:
stage: install
script: npm ci
cache:
key: frontend-deps
paths:
- .npm/
variables:
npm_config_cache: '$CI_PROJECT_DIR/.npm'
build:
stage: build
script: npm run build
artifacts:
paths:
- dist/
test:
stage: test
script: npm test
LimiteValeur
Profondeur child pipelines (parent → child → grandchild)2 niveaux max
Taille de la hiérarchie downstream1000 pipelines par défaut (paramétrable)
Fichiers include par child pipeline3 fichiers max
  1. Toujours strategy: depend sauf si vous voulez explicitement ne pas attendre

  2. Un fichier CI par composant : frontend/.gitlab-ci.yml, backend/.gitlab-ci.yml

  3. Conditions sur les triggers : ne déclenchez que ce qui a changé

  4. Variables explicites : documentez ce que vous passez aux enfants

  5. Nommez clairement : trigger_frontend, trigger_backend, pas job1, job2

ErreurCauseSolution
Parent passe en success mais enfant échoueManque strategy: dependAjouter strategy: depend ou mirror
Variable non trouvée dans l’enfantVariable non passéeUtiliser variables: ou forward:
maximum depth exceededPlus de 2 niveaux d’enfantsRestructurer le pipeline
Fichier enfant non trouvéMauvais cheminVérifier le chemin relatif
Artefacts non disponibles dans l’enfantneeds classique ne traverse pas les pipelinesUtiliser needs:pipeline:job
needs:project échoueJob token non autoriséConfigurer allowlist côté projet amont
  1. trigger: include crée un pipeline enfant à partir d’un fichier local
  2. strategy: depend fait attendre le parent ; mirror pour un reflet strict
  3. variables: passe des variables à l’enfant
  4. Artefacts : utilisez needs:pipeline:job (parent→child) ou needs:project (multi-projet)
  5. trigger: project déclenche un pipeline dans un autre projet
  6. Limite : 2 niveaux de profondeur, 1000 pipelines downstream max
  7. $CI_PIPELINE_SOURCE vaut parent_pipeline dans un child pipeline