Dependent Jobs and Artifacts
Overview
In GitLab CI/CD, multiple dependent jobs can be organized using stage
and stages
. By defining different stages, you can control the order in which jobs run and ensure that some jobs depend on the successful completion of previous ones.
For more information, please see GitLab CI/CD YAML Syntax Reference
Pre-requisites
Modify the pipeline
Select the project > Build > Pipeline Editor. Paste the code below, add a commit message, and commit changes.
workflow:
name: Generate ASCII Framework
stages:
- build
- test
- deploy
build_job:
stage: build
before_script:
- gem install cowsay
- sleep 30s
script:
- >
cowsay -f dragon "Run for cover!
I am a DRAGON!" >> dragon.txt
test_job:
stage: test
script:
- |
sleep 10s
grep -i "dragon" dragon.txt
deploy_job:
stage: deploy
script:
- cat dragon.txt
- echo "Deploying..."
Go to Pipelines and click the Job code/number. We can see that the second job failed, which then stops the entire pipeline.
Opening the test job, we can see that it failed because it cannot access the text file that was created by the first job. To fix this, we need to use artifacts.
Artifacts
Artifacts are files generated by jobs that can be stored and shared between pipeline stages. They allow you to pass outputs, like build files or test reports, from one stage to another.
Go back to the .gitlab-ci.yml
file and add the artifacts on the build job.
workflow:
name: Generate ASCII Framework
stages:
- build
- test
- deploy
build_job:
stage: build
before_script:
- gem install cowsay
- sleep 30s
script:
- >
cowsay -f dragon "Run for cover!
I am a DRAGON!" >> dragon.txt
artifacts:
name: artifact-text-file
paths:
- dragon.txt
untracked: false
when: on_success
expire_in: 3 days
test_job:
stage: test
script:
- |
sleep 10s
grep -i "dragon" dragon.txt
deploy_job:
stage: deploy
script:
- cat dragon.txt
- echo "Deploying..."
The pipeline should now succeed.
To access the generated artifacts, go to Build > Artifacts and click the folder icon at the right side.
To download, click Download artifacts archive.