Feedback

Chat Icon

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Managing Artifacts in GitLab CI/CD: Data Sharing Between Jobs
45%

Managing Artifacts Dependencies: Selective Artifacts Using "dependencies"

The keyword artifacts is used to pass artifacts between jobs. However, this is the default behavior of GitLab CI/CD: all artifacts generated by a job are automatically passed to the next jobs in the same pipeline.

Example:

In a pipeline where we have three jobs: job-1, job-2, and job-3:

  • If job-1 generates an artifact called file1.txt.
  • If job-2 generates an artifact called file2.txt.
  • As a result, job-3 will automatically fetch the artifacts generated by both jobs (job-1 and job-2).

You can run the following command to test how it works:

cat <$HOME/todo/app/.gitlab-ci.yml && \
cd $HOME/todo/app && \
git add . && \
git commit -m "Artifacts example" && \
git push origin main
stages:
  - stage-1
  - stage-2
  - stage-3

job-1:
  stage: stage-1
  script:
    - touch file1.txt
  artifacts:
    paths:
      - file1.txt

job-2:
  stage: stage-2
  script:
    - touch file2.txt
  artifacts:
    paths:
      - file2.txt

job-3:
  stage: stage-3
  script:
    - ls -l | grep "file1.txt" || echo "file1.txt not found"
    - ls -l | grep "file2.txt" || echo "file2.txt not found"
EOF

After the pipeline is triggered, you will see this output in the job-3 job:

[...]

Downloading artifacts for job-1 (8452994181)...

[...]

Downloading artifacts for job-2 (8452994182)...

[...]

$ ls -l | grep "file1.txt" || echo "file1.txt not found"
-rw-r--r-- 1 root root     0 Oct 22 17:07 file1.txt

$ ls -l | grep "file2.txt" || echo "file2.txt not found"

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Enroll now to unlock all content and receive all future updates for free.