
Module 3 Lesson 3: Working with Artifacts
Save your work. Learn how to use 'Artifacts' to pass files between stages (e.g., from Build to Test) and how to download them for manual review.
Module 3 Lesson 3: Working with Artifacts
Every Job in GitLab starts with a "Clean Slate." Files created in the build stage are DELETED before the test stage starts. To "Pass" files forward, you must use Artifacts.
1. What is an Artifact?
An artifact is a file or folder that is uploaded back to the GitLab server after a job finishes.
- Subsequent stages will automatically download these files.
- Users can download them from the GitLab website (e.g., download a
.pdfreport or a compiled.exe).
2. Defining Artifacts in YAML
build-app:
stage: build
script:
- npm run build
artifacts:
# Which paths should we save?
paths:
- dist/
- logs/*.log
# How long should GitLab keep these files?
expire_in: 1 week
3. Passing Data Between Stages
If test-job needs the dist/ folder created by build-app, it will find it automatically if they are in sequential stages.
- GitLab handles the "Upload" and "Download" behind the scenes.
4. Why Use Artifacts?
- Auditing: If a user reports a bug in
v1.2, you can go back to that pipeline and download the exact binary that was shipped. - Reports: Automated tests often generate HTML reports. By setting them as artifacts, you can view them in your browser without leaving GitLab.
- Speed: You compile your app ONCE (in the build stage) and use that exact binary for all testing and deployment stages.
Exercise: The Artifact Relay
- Create a job
create-filein thebuildstage. - Script:
echo "Hello Artifact" > info.txt. - Add an
artifactsblock that savesinfo.txt. - Create a second job
read-filein theteststage. - Script:
cat info.txt. - Run the pipeline. Does the second job find the file even though it's on a "Different" runner?
- Check the job's sidebar on GitLab. Do you see a button to "Download Artifacts"?
Summary
Artifacts are the "Memory" of your pipeline. Without them, your stages are isolated islands. With them, you can build a cohesive "Assembly Line" that transforms raw source code into a finished product.
Next Lesson: Speed it up: Caching dependencies.