Intro
CI/CD Fundamentals
.gitlab-ci.yml Basics
GitLab Runners
GitLab Executors
Specific Runners
Artifacts
Documentation Generation
Monitoring
6.2. Storing Artifacts
Step 1: Defining Artifacts in .gitlab-ci.yml
To store artifacts in GitLab CI/CD, you need to define them in the .gitlab-ci.yml
file. This is done within the job configuration by specifying the artifacts keyword.
Explanation of Artifact Configuration
- Stages: You can define different stages of the CI/CD pipeline, such as build, test, and deploy.
- Jobs: Each stage can have one or more jobs that execute specific commands. For instance, a build job can create necessary files, while a test job can run tests and produce reports.
- Artifacts: Inside each job, you specify the artifacts you want to store by using the artifacts keyword. This allows those files to be accessible in subsequent jobs or stages of your pipeline.
- Dependencies: By defining dependencies, you can ensure that a job only runs after certain jobs have completed successfully and can access the artifacts they generated.
Practical Exercise 6.2: Implementing Artifacts in our CI/CD Pipeline
Let’s add artifacts to our CI/CD pipeline for the “Research Publication Data Analysis” project. We will store the test results in an artifact file for later reference.
stages:
- build
- test
- deploy
build_job:
stage: build
- script:
- echo "Building the application..."
- pip install -r requirements.txt
- echo "Build complete"
artifacts:
paths:
- build/
test_job:
stage: test
script:
- echo "Running Calculator tests"
- pytest tests/ > test-results/report.txt
artifacts:
paths:
- test-results/report.txt
deploy_job:
stage: deploy
script:
- echo "Deploying the project"
5. Run the Pipeline:
Commit and push the .gitlab-ci.yml
file to trigger the pipeline in GitLab using:
git add .gitlab-ci.yml
git commit -m "Add CI configuration with artifacts"
git push origin main # Adjust branch name if necessary
6. Check Artifact Storage:
- Navigate to your GitLab project, go to the Pipelines section, and click on the latest pipeline run. Verify that the artifacts are stored correctly by checking the output of your jobs.