GitLab CI/CD: Architecting, Deploying, and Optimizing Pipelines
Architecture Core Concepts
Pipeline with Multiple Dependent Jobs
In this guide, we'll explore how to configure a CI pipeline with three distinct stages—build, test, and deploy—and ensure they execute in the correct order while sharing artifacts.
Initial Pipeline Configuration
Here’s a basic GitLab CI configuration defining three jobs:
workflow:
name: Generate ASCII Artwork
build_job_1:
stage: build
before_script:
- gem install cowsay
- sleep 30s
script:
- cowsay -f dragon "Run for cover, I am a DRAGON....RAWR" >> dragon.txt
test_job_2:
stage: test
script:
- sleep 10s
- grep -i "dragon" dragon.txt
deploy_job_3:
stage: deploy
script:
- cat dragon.txt
- echo "deploying ... ..."
Warning
Relying on fixed sleep
intervals can lead to fragile pipelines. Instead, use proper job dependencies and artifacts to synchronize stages.
Job Breakdown
Job Name | Stage | Purpose |
---|---|---|
build_job_1 | build | Installs cowsay and generates dragon.txt |
test_job_2 | test | Searches for “dragon” in the generated file |
deploy_job_3 | deploy | Displays ASCII art and prints a deploy message |
- build_job_1 installs the
cowsay
gem, pauses for 30 seconds, then writes ASCII art intodragon.txt
. - test_job_2 pauses for 10 seconds before verifying the file contains the keyword “dragon.”
- deploy_job_3 outputs the contents of
dragon.txt
and echoes a deploy message.
Note
By default, GitLab CI jobs run in parallel on separate runners and do not share the same workspace.
Observed Failures
Because test_job_2 and deploy_job_3 often start before build_job_1 finishes, they encounter missing files:
$ sleep 10s
grep: dragon.txt: No such file or directory
ERROR: Job failed: exit code 1
$ cat dragon.txt
cat: dragon.txt: No such file or directory
ERROR: Job failed: exit code 1
Meanwhile, build_job_1 succeeds:
$ gem install cowsay
Successfully installed cowsay-3.0.3
1 gem installed
$ sleep 30s
$ cowsay -f dragon "Run for cover, I am a DRAGON....RAWR!" > dragon.txt
Why Jobs Fail
- Each job runs on a separate runner with its own workspace.
- There’s no file sharing by default.
- The execution order is not guaranteed without explicit dependencies.
Next Steps
In the next section, we’ll introduce the needs
keyword and artifacts settings to:
- Ensure build_job_1 completes before test_job_2.
- Share
dragon.txt
as an artifact for downstream jobs. - Execute deploy_job_3 only after the test stage passes.
References
Watch Video
Watch video content