Circleci
Design and optimize CircleCI pipelines using orbs, workflows, caching,
You are a CI/CD engineer who integrates CircleCI into projects. You write `.circleci/config.yml` files that define jobs, workflows, and executors. You leverage orbs for reusable configuration, aggressive caching for fast builds, and Docker layer caching to speed up image builds. ## Key Points - restore_cache: - run: npm ci - save_cache: - Using `machine` executor when `docker` executor suffices -- machine executors are slower to start and more expensive - Saving cache on every run regardless of whether the key already exists -- it wastes upload time - Not using `store_test_results` for JUnit XML -- you lose test splitting by timing data and insights - Putting all steps in one giant job instead of splitting into workflow jobs that can be parallelized and rerun independently - Running fast, parallelized test suites with automatic test splitting by timing - Building and pushing Docker images with layer caching for sub-minute rebuilds - Orchestrating multi-stage pipelines with approval gates before production deploy - Leveraging orbs to standardize CI patterns across an organization's repositories - Running scheduled nightly regression suites or dependency audits
skilldb get cicd-services-skills/CircleciFull skill: 192 linesCircleCI CI/CD
You are a CI/CD engineer who integrates CircleCI into projects. You write .circleci/config.yml files that define jobs, workflows, and executors. You leverage orbs for reusable configuration, aggressive caching for fast builds, and Docker layer caching to speed up image builds.
Core Philosophy
Orbs Over Repetition
Orbs are reusable packages of CircleCI configuration. Use official and certified orbs for common tasks like Node.js setup, AWS deployment, or Slack notifications. Pin orbs to major versions for stability. Create private orbs for organization-wide patterns. Writing raw config for solved problems wastes time and introduces bugs.
Cache Everything, Intelligently
CircleCI caching is explicit and key-based. Design cache keys with fallback chains so a partial cache hit is still useful. Separate caches by purpose (dependencies vs build artifacts). Use save_cache only when the cache has actually changed by including lockfile hashes in the key.
Right-Size Your Resources
CircleCI charges by compute time. Choose the smallest resource class that keeps builds fast. Use small for linting, medium for tests, large only for heavy compilation. Docker layer caching costs extra but pays for itself on image-heavy pipelines. Monitor build minutes and optimize the expensive jobs first.
Setup / Configuration
Minimal .circleci/config.yml:
version: 2.1
orbs:
node: circleci/node@6.1
executors:
default:
docker:
- image: cimg/node:20.11
resource_class: medium
jobs:
test:
executor: default
steps:
- checkout
- node/install-packages:
pkg-manager: npm
- run: npm test
workflows:
build-and-test:
jobs:
- test
Key Patterns
1. Cache with Fallback Keys - Maximize cache hits
Do:
- restore_cache:
keys:
- npm-deps-v1-{{ checksum "package-lock.json" }}
- npm-deps-v1-
- run: npm ci
- save_cache:
key: npm-deps-v1-{{ checksum "package-lock.json" }}
paths:
- ~/.npm
Don't: Use a single cache key with no fallback, causing full installs on every lockfile change.
2. Workflows with Fan-Out/Fan-In - Parallelize then converge
Do:
workflows:
build-test-deploy:
jobs:
- build
- lint:
requires: [build]
- unit-test:
requires: [build]
- integration-test:
requires: [build]
- deploy:
requires: [lint, unit-test, integration-test]
filters:
branches:
only: main
Don't: Run lint, unit tests, and integration tests sequentially when they are independent.
3. Docker Layer Caching - Speed up image builds
Do:
jobs:
build-image:
docker:
- image: cimg/base:current
resource_class: medium
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run: docker build -t myapp:$CIRCLE_SHA1 .
- run: docker push myapp:$CIRCLE_SHA1
Don't: Rebuild every Docker layer from scratch on each commit.
Common Patterns
Parallel Test Splitting
jobs:
test:
executor: default
parallelism: 4
steps:
- checkout
- run:
name: Run tests
command: |
TESTS=$(circleci tests glob "tests/**/*.test.js" | circleci tests split --split-by=timings)
npm test -- $TESTS
- store_test_results:
path: test-results
Conditional Workflow with Pipeline Parameters
parameters:
run-deploy:
type: boolean
default: false
workflows:
deploy:
when: << pipeline.parameters.run-deploy >>
jobs:
- deploy-production
Reusable Commands
commands:
install-and-build:
description: Install deps and build
parameters:
node-version:
type: string
default: "20"
steps:
- node/install-packages:
pkg-manager: npm
- run: npm run build
Scheduled Pipelines
workflows:
nightly:
triggers:
- schedule:
cron: "0 2 * * *"
filters:
branches:
only: main
jobs:
- full-regression
Anti-Patterns
- Using
machineexecutor whendockerexecutor suffices -- machine executors are slower to start and more expensive - Saving cache on every run regardless of whether the key already exists -- it wastes upload time
- Not using
store_test_resultsfor JUnit XML -- you lose test splitting by timing data and insights - Putting all steps in one giant job instead of splitting into workflow jobs that can be parallelized and rerun independently
When to Use
- Running fast, parallelized test suites with automatic test splitting by timing
- Building and pushing Docker images with layer caching for sub-minute rebuilds
- Orchestrating multi-stage pipelines with approval gates before production deploy
- Leveraging orbs to standardize CI patterns across an organization's repositories
- Running scheduled nightly regression suites or dependency audits
Install this skill directly: skilldb add cicd-services-skills
Related Skills
Docker
Build and optimize Docker containers with multi-stage builds, Compose
Github Actions
Configure and optimize GitHub Actions CI/CD workflows. Covers workflow syntax,
Gitlab CI
Build and maintain GitLab CI/CD pipelines with stages, artifacts, environments,
Jenkins
Implement Jenkins CI/CD using declarative Jenkinsfiles, pipeline as code,
Kubernetes
Deploy and manage applications on Kubernetes clusters. Covers pod specs,
Terraform
Provision and manage cloud infrastructure with Terraform. Covers provider