GitLab CI/CD: Architecting, Deploying, and Optimizing Pipelines

Architecture Core Concepts

Artifacts Storing Job Data

In this article, we’ll explore how artifacts capture and persist files produced during job execution in your GitLab CI/CD pipelines. You’ll learn how to configure artifacts in .gitlab-ci.yml, pass them between stages, and view them in the UI.

What Are Artifacts?

In GitLab CI/CD, artifacts are archives of files and directories generated by a job. They are:

  • Uploaded to GitLab when a job finishes.
  • Automatically downloaded by downstream jobs in the same pipeline.
  • Retained according to the expire_in policy you define.

Note

Artifacts are uploaded only if the job’s status matches the when setting (e.g., on_success or always). Use the dependencies or needs keywords to control which upstream artifacts a job downloads.

The image shows a GitLab documentation page about CI/CD YAML syntax, specifically focusing on job artifacts and their configuration. The page includes navigation links on the left and a list of keywords on the right.

Configuring Artifacts

Under the artifacts section of a job, you can define several options. The table below summarizes the most common:

OptionDescriptionExample
pathsList of files or directories to archivepaths: [binaries/, .config]
expire_inDuration to keep artifacts (e.g., 3 days, 1 week, never)expire_in: 3 days
expose_asLabel to display artifacts in the Merge Requestexpose_as: 'Test Report'
nameCustom name for the artifact archivename: "job1-artifacts-{{CI_JOB_ID}}"
whenUpload condition (on_success, on_failure, always)when: on_failure

Example: Defining Artifacts

job:
  script:
    - make build
  artifacts:
    paths:
      - binaries/
      - .config
    expire_in: 1 week
    when: on_success
    name: "build-artifacts-{{CI_JOB_ID}}"

The image shows a GitLab documentation page about the `artifacts:expire_in` keyword, explaining how to specify the duration for storing job artifacts before they expire. It includes examples of valid time values and additional information on usage.

End-to-End Pipeline Example

Below is a simple three-stage pipeline where the build_job creates a file, then passes it to test_job and deploy_job.

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....RAWR" > dragon.txt
  artifacts:
    name: "Dragon Text File"
    paths:
      - dragon.txt
    when: on_success
    expire_in: 3 days

test_job:
  stage: test
  needs: ["build_job"]
  script:
    - sleep 10s
    - grep -i "dragon" dragon.txt

deploy_job:
  stage: deploy
  needs: ["build_job"]
  script:
    - cat dragon.txt
    - echo "deploying …"

With this setup:

  1. build_job generates dragon.txt and uploads it.
  2. test_job and deploy_job automatically download the artifact.
  3. Artifacts expire after three days.

Pipeline Execution and Logs

build_job

$ gem install cowsay
Successfully installed cowsay-3.0.0
$ sleep 30s
$ cowsay -f dragon "Run for cover, I am a DRAGON....RAWR" > dragon.txt
Uploading artifacts for successful job
Finding dragon.txt based on provided path
Creating archive (zip, tar, etc.)
POST /api/v4/projects/:id/jobs/:job_id/artifacts
201 Created

test_job

Downloading artifacts for build_job (ID=6027836005)...
Downloading artifacts from coordinator… ok
host=storage.googleapis.com id=6027836005 responseStatus=200 ok
$ sleep 10s
$ grep -i "dragon" dragon.txt
Run for cover, I am a DRAGON....RAWR

deploy_job

Downloading artifacts for build_job (ID=6027836005)...
Downloading artifacts from coordinator… ok
host=storage.googleapis.com id=6027836005 responseStatus=200 ok
$ cat dragon.txt
Run for cover, I am a DRAGON....RAWR
$ echo "deploying …"
deploying …

Viewing Artifacts in GitLab UI

In the Pipeline view, jobs that produce artifacts display an Artifacts badge. Clicking Browse or Download lets you inspect and retrieve the files.

The image shows a GitLab interface displaying a list of artifacts from different jobs, including their sizes and creation times. The sidebar includes navigation options like "Manage," "Plan," "Code," and "Build."

Artifacts also appear under the Artifacts tab on each job’s detail page, alongside logs and metadata.


References

Watch Video

Watch video content

Previous
Using stage vs stages Keyword