Working with CircleCI Artifacts - CircleCI Tutorial
Introduction
CircleCI artifacts provide a convenient way to store and share files generated during your CI/CD pipelines. They allow you to persist important build artifacts, such as logs, test reports, code coverage reports, and compiled binaries. This tutorial will guide you through the process of working with CircleCI artifacts and leveraging their power to store and access files within your CI/CD workflows.
Example
Let's consider an example where we store test reports as artifacts in CircleCI:
version: 2.1
jobs:
test:
docker:
- image: circleci/node:12
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package-lock.json" }}
- run: npm install
- save_cache:
paths:
- ./node_modules
key: v1-dependencies-{{ checksum "package-lock.json" }}
- run:
name: Run Tests
command: npm test
- store_artifacts:
path: ./test-reports
Working with CircleCI Artifacts
To work with CircleCI artifacts, follow these steps:
1. Define the files to be stored
Determine which files or directories you want to persist as artifacts. This can include test reports, code coverage reports, build logs, compiled binaries, or any other relevant files.
2. Configure artifact storage
In your CircleCI configuration file (e.g., .circleci/config.yml
), use the store_artifacts
key to specify the files or directories you want to store as artifacts. You can use wildcards to match multiple files or directories.
For example, to store all test reports located in the ./test-reports
directory, you can add the following step to your configuration:
- store_artifacts:
path: ./test-reports
Common Mistakes
- Not specifying the correct path to the files or directories to be stored
- Storing unnecessary or irrelevant files as artifacts
- Forgetting to download or access the stored artifacts in subsequent steps or jobs
Frequently Asked Questions (FAQs)
-
How can I download or access artifacts in subsequent steps or jobs?
You can download or access the stored artifacts using the CircleCI API or the CircleCI CLI. Artifacts can be retrieved based on the build number, job name, and path specified during storage. Refer to the CircleCI documentation for more information on accessing artifacts.
-
Can I store artifacts from multiple jobs in a single pipeline?
Yes, you can store artifacts from multiple jobs within a single pipeline. Each job can store its own set of artifacts, allowing you to organize and manage them efficiently.
-
Can I control the retention period of artifacts?
Yes, CircleCI allows you to configure the retention policy for artifacts. By default, artifacts are retained for 30 days, but you can customize this period to match your requirements.
Summary
In this tutorial, you learned how to work with CircleCI artifacts to store and access build artifacts within your CI/CD pipelines. By defining the files to be stored and configuring artifact storage, you can persist important files such as logs, test reports, and binaries. Use CircleCI APIs or CLI to download or access these artifacts in subsequent steps or jobs. Avoid common mistakes, store only relevant files as artifacts, and manage the retention period of artifacts effectively. Refer to the CircleCI documentation for further details and advanced configuration options.