GitLab CI/CD - Dynamic Pipelines
In pipeline best practices, dynamic pipelines are generally discouraged. Readability is crucial for any codebase, and once you start using dynamic pipelines it becomes difficult — sometimes impossible — to guarantee readability or maintainability.
That said, there are certain scenarios where dynamic pipelines can actually help you maintain or even improve readability while also improving maintainability. In those cases, using a dynamic pipeline is the right call.
For example, suppose you need to run 100 tests for the same project in CI, where the only difference between each test is the argument passed in — arguments that will evolve as your product grows. Would you really want to repeat the following snippet 100 times?
1 | test-with-arg-100: |
Parent-Child Pipelines
In GitLab CI/CD, a parent-child pipeline means nesting the execution of another pipeline configuration file (the child pipeline) inside a parent pipeline.
Child pipeline types:
- Merge request child pipelines
- Dynamic child pipelines
- Nested child pipelines
For more details, refer to the official documentation.
Example: Deploying Multiple Applications with a Dynamic Child Pipeline
In my Homelab environment, I have an Infrastructure Kubernetes cluster that needs a set of foundational applications deployed, such as:
- external-dns for registering Ingress DNS records to a DNS server
- Experimental apps like HashiCorp Vault and Jenkins
- Monitoring apps like Prometheus
Each application has its own dedicated deployment script, as shown below.
1 | ├── auto |
All deployment scripts follow the naming pattern auto/deploy-*. Based on this pattern, we can generate the child pipeline’s YAML configuration file automatically. The generation script is shown below.
1 |
|
The relevant section of the .gitlab-ci.yml pipeline configuration file looks like this:
1 | stages: |
The pipeline execution graph is shown below: