Circle CI

Pipeline

Pipeline is full set of processes when we trigger work on projects

Workflows

  • Workflows define a list of jobs and their run order.
  • Using workflows, it is possible to run jobs concurrently, sequentially, on a schedule, or with a manual gate using an approval job.
  • Workflows allows to control when they are triggered based on tags, branches, or schedules.
workflow:
  build_test:
    jobs:
      - run_tests
      - build
        requires:
          - run_tests
        filters:
          branches:
            only: main

Jobs

  • Jobs are the building blocks of config file. Jobs are a collection of steps, which run commands and scripts as required. All of the steps in the job are executed in a single unit which consumes a CircleCI container from your plan while it’s running.

  • Each job must declare an executor that is either docker, machine, windows or macos. machine includes a default image if not specified, for docker you must specify an image to use for the primary container, for macos you must specify an Xcode version, and for windows you must use the Windows orb.

Steps

  • Steps are actions that need to be taken to perform your job. Steps are usually a collection of executable commands. For example, the checkout step checks out the source code for a job over SSH. Then, the run step executes the make test command using a non-login shell by default.

Image

  • An image is a packaged system that has the instructions for creating a running container. The Primary Container is defined by the first image listed in .circleci/config.yml file. This is where commands are executed for jobs using the Docker or machine executor. The Docker executor spins up a container with a Docker image. The machine executor spins up a complete Ubuntu virtual machine image. See Choosing an Executor Type document for a comparison table and considerations.

Circle CI Configuration Example

version: 2.1

jobs:
  one:
    docker:
      - image: circleci/ruby:2.4.1
    steps:
      - checkout
      - run: echo "Hello Ruby"
      - run: mkdir -p my_workspace
      - run: echo "Trying out workspaces" > my_workspace/echo-output
      - persist_to_workspace:
            root: my_workspace
            paths:
                - echo-output
      - run: sleep 25
  two:
    docker:
      - image: circleci/ruby:2.4.1
    steps:
      - checkout
      - run: echo "A more familiar Hi"
      - attach_workspace:
          at: my_workspace
      - run: |
          if [[ $(cat my_workspace/echo-output) == "Trying out workspaces" ]]; then
            echo "It worked!";
          else
            echo "Nope!"; exit 1
          fi          
      - run: sleep 15

workflows:
  version: 2
  one_two_wf:
    jobs:
      - one
      - two:
          requires:
            - one

Parametrized Configuration

Parameters let you to reuse parts of your config code, witout repeating yourself. this ensures your config files are shorter, more readable, and easier to maintain.

Parameters work with jobs, commands, executors, as well as orbs.

You can use parameters within a single config file, and across config files.

version: 2.1

jobs:
  build:
    parameters:
      p:
        type: integer
        default: 1
    parallelism: << parameters.p >>
    machine: true
    steps:
    - checkout
  
  workflows:
    workflow:
      jobs:
      - build:
          p: 2

Invoke workflows with logic operators

workflows:
  my-workflow:
    when: << pipeline.git.branch >>

steps:
- when:
    condition:
      and:
        - << parameters.custom_checkout >>
        - equal: [main, << paramters.git.brach >>]
    steps:

Orb

  • A reusable package of YAML configuration that condenses repeated pieces of config into a single line of code.
  • Write configuration once, and use it again across projects.
  • Orb Registry is an open repository of all published orbs.
version: 2.1

jobs:
  test:
    docker:
    - image: cimg/node:<node-version>
      auth:
        username: mydockerhub-user
        password: $DOCKERHUB_PASS
    
    steps:
    - checkout
    - restore_cache:
      keys:
        - node-deps-v1-{{ .Branch }}-{{checksum "package-lock.json"}}
    - run:
      name: install packages
      command: npm ci
    - save_cache:
      key: node-deps-v1-{{ .Branch }}-{{checksum "package-lock.json"}}
      paths:
       - ~/.npm
    - run:
      name: Run Tests
      command: npm run test

workflows:
  test_my_app:
    jobs:
      - test
version: 2.1

orbs:
  node: circleci/node@x.y

workflows:
  test_my_app:
    jobs:
      - node/test:
        version: <node-version>  

Docker Layer Caching

version: 2.1
jobs:
  build:
    docker:
      - image: circleci/node:14.17.3-buster-browsers
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASS
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - run: docker build .

Resources