Persisting Data Between Jobs in CircleCI Workflows
In CircleCI, jobs in a workflow are independent of each other and run in separate environments. This means that if you've done some work in one job (such as installing dependencies), you'll lose that work when moving to the next job. To make your pipelines more efficient, you can persist data across jobs using the
In the example configuration, we have a
build job where dependencies are installed, and we want to use those installed dependencies in a subsequent
test job. Here's how it works:
Persisting Data: In the
persist_to_workspaceto persist the
node_modulesdirectory. This will make
node_modulesavailable to subsequent jobs.
- persist_to_workspace: root: . paths: - node_modules
Attaching Workspace: In the
attach_workspaceto retrieve the data persisted from the
buildjob. We specify
/workspaceas the directory where the persisted data should be attached.
- attach_workspace: at: /workspace
Using Persisted Data: Finally, we run
npm testin the
testjob using the previously installed
node_modulesby setting the
This approach is particularly useful for cutting down redundant steps in your workflow, making your CI/CD process more efficient.
For more details, you can refer to the official CircleCI documentation on Workspaces.
version: 2.1 jobs: build: docker: - image: cimg/node:20.5.1 steps: - checkout - run: npm install - persist_to_workspace: root: . paths: - node_modules test: docker: - image: cimg/node:20.5.1 steps: - attach_workspace: at: /workspace - run: command: npm test working_directory: /workspace workflows: build_and_test: jobs: - build - test: requires: - build