cheat-sheets

CircleCI

CircleCI is a hosted CI/CD service (with a self-hosted runner option) configured by .circleci/config.yml. This sheet covers the version 2.1 config syntax and the circleci CLI. The CLI section was checked against circleci 1.14.0, a rewrite of the older CLI; the config YAML comes from the CircleCI docs and was not validated here because circleci config validate needs to reach the CircleCI API (unverified). For concepts shared with other systems see CI/CD Overview.

Documentation: CircleCI docs, CLI reference Project: CircleCI-Public/circleci-cli


Minimal Config #

yaml
version: 2.1

executors:
  node:
    docker:
      - image: cimg/node:22.11
    resource_class: medium

commands:
  install-deps:
    steps:
      - restore_cache:
          keys:
            - deps-v1-{{ checksum "package-lock.json" }}
      - run: npm ci
      - save_cache:
          key: deps-v1-{{ checksum "package-lock.json" }}
          paths:
            - ~/.npm

jobs:
  test:
    executor: node
    steps:
      - checkout
      - install-deps
      - run:
          name: Run tests
          command: npm test
      - store_test_results:
          path: test-results
      - persist_to_workspace:
          root: .
          paths: [dist]
  deploy:
    executor: node
    steps:
      - attach_workspace:
          at: .
      - run: ./deploy.sh

workflows:
  main:
    jobs:
      - test
      - hold:
          type: approval
          requires: [test]
          filters:
            branches:
              only: main
      - deploy:
          requires: [hold]
          context: production
          filters:
            branches:
              only: main
  nightly:
    triggers:
      - schedule:
          cron: "0 3 * * *"
          filters:
            branches:
              only: main
    jobs:
      - test

Config Keys #

KEYPURPOSE
version: 2.1Required for orbs, commands, executors and parameters
orbsImport shared packages of jobs, commands and executors
executorsReusable environments: docker, machine (a VM) or macos, plus resource_class
commandsReusable step lists with parameters
jobsNamed collections of steps run in one executor
workflowsOrder and filter jobs: requires, filters, context, matrix, triggers
parametersPipeline parameters, set at trigger time
parallelism (job)Run N copies of a job, usually to split tests
working_directory, environment, shell (job)Job level defaults

Steps #

STEPDESCRIPTION
checkoutClone the repository at the pipeline’s commit
runRun a command (name, command, environment, when: always|on_fail, no_output_timeout)
save_cache / restore_cacheCache paths between runs, keyed on {{ checksum "FILE" }}
persist_to_workspace / attach_workspacePass files between jobs in one workflow
store_artifactsKeep files for download
store_test_resultsUpload JUnit style results for the test insights UI
when / unlessConditional steps using a parameter or pipeline value
setup_remote_dockerRemote Docker engine for building images (see CI Docker Builds)

Environment Variables #

VARIABLEMEANING
CIRCLE_SHA1Commit being built
CIRCLE_BRANCHBranch name
CIRCLE_TAGTag name when built from a tag
CIRCLE_BUILD_NUMJob build number
CIRCLE_PROJECT_REPONAMERepository name
CIRCLE_WORKING_DIRECTORYJob working directory

Project variables and contexts are set in the web app or with circleci envvar and circleci context (below). Contexts hold secrets shared across projects and are referenced from a workflow job with context:.

circleci CLI #

Install by downloading the release archive (it contains a single circleci binary) or the .deb or .rpm. Global flags: -c/--config, --debug, --no-color, -q/--quiet. Help for any command: circleci help <COMMAND>.

COMMANDDESCRIPTION
circleci auth loginLog in through the browser (auth me shows the account, auth logout clears credentials)
circleci config validate [PATH]Validate a config (default .circleci/config.yml; - reads stdin; --org gh/ORG for private orbs; --json)
circleci config process PATHPrint the fully expanded config (orbs inlined, matrices and parameters resolved; --pipeline-parameters 'key: value')
circleci config pack DIRMerge a split config directory into one YAML document on stdout
circleci config generate [PATH]Write a starter .circleci/config.yml from a scan of the repo (will not overwrite an existing file)
circleci run listRecent runs for the project (run get, run watch, run trigger, run cancel, run approve)
circleci workflow list / workflow rerun / workflow cancelInspect and control workflows (IDs appear in circleci run get)
circleci job get / job output / job artifactJob details, step output and artifacts
circleci preflight runPush the working tree (including uncommitted changes) to a temporary cci/preflight/ branch and watch the run; circleci preflight cleanup deletes those branches
circleci envvar list / set NAME VALUE / delete NAMEProject environment variables (--project gh/ORG/REPO; listed values are masked)
circleci context list / create / get / deleteContexts (context secret manages the variables in one)
circleci orb list / validate FILE / process FILE / publishWork with orbs
circleci runner resource-class / token / instance / configSelf-hosted runner management
circleci project follow / project linkFollow a project, or bind this checkout to one
circleci completionInstall or print shell completions

Version 1.14.0 has no circleci local execute; the older CLI used it to run a job locally. Use circleci preflight run (which runs in CircleCI itself) or run the job’s image locally instead.

Troubleshooting #

SYMPTOMFIX
config validate cannot reach the APIIt compiles the config on CircleCI’s servers, so it needs network access
Private orb not foundAdd --org gh/ORG and log in (circleci auth login)
Workflow does not startConfig error, or branch and tag filters exclude it; tag filters need the job and every job it requires to allow the tag
Cache never restoresThe key changes every run, or the checksum file differs on the branch
Deploy runs on every branchAdd filters: branches: only: main to the workflow job
Secret empty in a fork PRSecrets are withheld from forks by default

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.