Configuration

Configuration

Configuration involves:

  • platform used to execute task
  • third party
  • environment variables

Execution environment

CircleCI relies on CircleCI’s Docker images (documentation) which is mainly used as follow:

jobs:
  build-job:
    docker: # executor type
      - image: buildpack-deps:trusty # primary container will run Ubuntu Trusty

Executors

Executors require version 2.1

When the same environment is required multiple times or for code organization purpose, you can configure the execution environment under executors:

version: 2.1

executors:
  # Define environment like you did in jobs
  app-builder:
    docker:
      - image: circleci/ruby:2.5.1-node-browsers
      - image: circleci/postgres:9.6.5-alpine-ram
    working_directory: ~/repo
  aws-deployer:
    docker:
      - image: circleci/python:3.7.2-stretch-node-browsers-legacy

jobs:
  build:
    executor: app-builder
    steps: # steps
  deploy-dev:
    executor: aws-deployer
    steps: # steps
  deploy-prod:
    executor: aws-deployer
    steps: # steps

Orbs

Orbs require version 2.1

CircleCI Orbs are presets used as third party library. It offers preconfigured commands. For example, the Slack Orb is used as follow:

version: 2.1

orbs:
  slack: circleci/[email protected]

jobs:
  # This job only push a message on Slack to request an approval
  before_approval:
    docker:
      - image: buildpack-deps:trusty # Ubuntu Trusty
    steps:
      - slack/notify:
          message: "A deploy is on hold for a manual approval: https://circleci.com/workflow-run/$CIRCLE_WORKFLOW_ID"
          mentions: "{validator users}"
          color: "#e5bd0b"
          # Optional: Enter a specific webhook here or the default will use $SLACK_WEBHOOK
          # webhook: 'webhook'

Commands

Commands require version 2.1

If a command pattern is repeated, it can be defined under commands with parameters

version: 2.1

commands:
  deploy-aws-eb:
    description: "Deploy to AWS Elastic Beanstalk"
    parameters:
      env:
        type: string
    steps:
      - run:
          # Environment is assumed to by Python
          name: Install EB CLI
          command: sudo pip install awsebcli --upgrade
      - run:
          name: Create AWS credentials manually
          command: |
            mkdir ~/.aws
            touch ~/.aws/config
            chmod 600 ~/.aws/config
            echo "[profile eb-cli]" > ~/.aws/config
            echo "aws_access_key_id=$AWS_ACCESS_KEY_ID" >> ~/.aws/config
            echo "aws_secret_access_key=$AWS_SECRET_ACCESS_KEY" >> ~/.aws/config
      - run:
          name: Deploy to AWS EB (Prod)
          command: eb deploy << parameters.env >> --profile eb-cli --label << parameters.env >>-$CIRCLE_BUILD_NUM

jobs:
  # Deploy to dev
  deploy-dev:
    executor: aws-deployer
    steps:
      - checkout
      - deploy-aws-eb:
          env: learnzone-aws-dev

  # Deploy to prod
  deploy-prod:
    executor: aws-deployer
    steps:
      - checkout
      - deploy-aws-eb:
          env: learnzone-aws