GitLab CI Artifact

GitLab CI Artifacts

In this article, we will explain the Gitlab CI Artifacts. What we can do with it and when we can use it. Also, we will go through some critical configurations, like path, size limits, and how to scale the storage.

What are Gitlab CI Artifacts?

Artifacts are files generated through a process that we use Job or Pipeline. Of course, it’s not all Jobs that create files, but if your projects generate files, we can use the GitLab Artifacts to manage the files for us. 

GitLab will store that artifact, and we can download it using the UI or calling the API.

The Artifact Keyword

We need to use the keyword artifacts on our gitlab-ci.yml file to use the Gitlab CI Artifacts, specifying which files we would like to store as job artifacts.

What are Job Artifacts?

Job artifacts are a list of directories and files linked to the Job regardless of whether the Job succeeds or fails.

At the end of the Job, the GitLab stores the artifact, and the file will be available for us to download using the UI, only if the artifact size is smaller than the maximum size that we configure to allow it.

Gitlab CI Artifact Example

pre_build:
  stage: Pre Build
  image: 123456789011.dkr.ecr.us-east-1.amazonaws.com/bitslovers/blog:latest
  tags:
    - fargate
  only:
    - master
  script:
    - echo "export BRANCH=\"$(echo $BRANCH)\"" >> variables
    - echo "export RELEASE_VERSION=\"$(echo $RELEASE_VERSION)\"" >> variables
    - echo "export DB_HOST=\"$(echo $DB_HOST)\"" >> variables
    - echo "export DNS=\"$(echo $DNS)\"" >> variables
  artifacts:
    paths:
      - vars_file
  retry:
    max: 2
    when:
      - runner_system_failure

Gitlab Artifacts Between Stages

By default, jobs in later stages automatically download all the artifacts created by jobs in earlier stages. However, you can control artifact download behavior in jobs with dependencies.

build_app:
  stage: build_app
  script: make build:app
  artifacts:
    paths:
      - bin/
test:
  stage: test_app
  script: make test:app
  dependencies:
    - build_app

When using the needs keyword, jobs can only download artifacts from the positions defined in the needs configuration.

Job artifacts are collected for successful jobs by default and restored after caches.

Where are artifacts stored in GitLab?

The default location that Gitlab uses to store the files are /var/opt/gitlab/gitlab-rails/shared/artifacts.

If you change this location, you need to restart Gitlab.

GitLab Artifact Expiration

Sometimes we don’t want to fill our server with many files. For example, managing files for a big company with many projects and developers could be a big problem. Occasionally, we know that we should not keep some files for a long time on the server, and we could tell GitLab when this file expires or mark to delete.

To delete files after a specific creation period, we can use the expire_in keyword to define how long GitLab holds the artifacts. We can also use the Gitlab UI to store artifacts from expiring. If expire_in is not specified, the files are deleted after 30 days.

GitLab Artifact Path

We can use multiple paths and wildcards if various files are on the same path.

build-job:
  script:
    - mvn package -U
  artifacts:
    paths:
      - target/*.war

GitLab Artifacts API

We can use the GitLab API to download the artifacts, giving us more flexibility to automate our process according to our necessity.

For example: Download a single artifact file for a job of the latest successful pipeline for the specific branch name from inside the Job’s artifacts archive. The file is pulled from the archive and streamed to the client.

One thing that it’s worth remembering is that in GitLab 13.5 and later, artifacts for parent and child pipelines work like a “search-path” in hierarchical ordering from parent to child. So, for example, if both parent and child pipelines include a job with the same name, the artifact from the parent pipeline is downloaded.

I tested this command below using GitLab 13.5.

GET /projects/:id/jobs/artifacts/:ref_name/raw/*artifact_path?job=name

Example of GitLab API call to download Artifacts:

curl --header "PRIVATE-TOKEN: <access_token>"  --location "https://gitlab.www.bitslovers.com/api/v4/projects/55/jobs/artifacts/master/raw/path/to/file/post.pdf?job=pdf"

GitLab Artifact Size Limit

The default size limit for the artifact on Gitlab is 100 MB. However, we can easily change it on Admin Area:

How to change the artifact limit on GitLab:

For example

https://gitlab.www.bitslovers.com/admin/application_settings/ci_cd

Or

Admin -> Settings -> CI/CD

Gitlab Artifact Limit
Gitlab Artifact Limit

Gitlab Increase Artifact Size for Project and Group Level.

Also, we can change the artifact limit, in different levels, for example, by Project or Group. Following the exact location: Settings -> CI/CD


Boost Your GitLab Skills

Deploy AWS Elastic Beanstalk using GitLab.

How to execute Cloud Formation on Gitlab.

Setup Gitlab Runner with AWS ECR – Authenticate into Private Repository.

How to use Gitlab CI Variables.

Leave a Comment

Your email address will not be published. Required fields are marked *

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.