Skip to content

Instantly share code, notes, and snippets.

@abhioncbr
Last active November 29, 2018 01:20
Show Gist options
  • Save abhioncbr/4276fc17911c8fb95502d18286b8da27 to your computer and use it in GitHub Desktop.
Save abhioncbr/4276fc17911c8fb95502d18286b8da27 to your computer and use it in GitHub Desktop.
Manage CI/CD efficiently with CircleCI

How to manage CI/CD efficiently with CircleCI?

Continuous integration & deployment(CI/CD) is one of the well-known standard practice in modern-day software development. In a fiercely competitive world, businesses are relying on much more frequent feature releases to target customers. Recently, I came to know about CircleCI which is one of the excellent tools for achieving CI/CD efficiently. In my experience, the following CircleCI tagline is entirely apt:

Automate your development process quickly, safely, and at scale.

In this post, I will be sharing how to build and deploy Docker images using CircleCI quickly.

Introduction to a CircleCI config file

First, enable CircleCI webhook for your public GitHub repository. CirclecCi expects config.yml in the .circleci sub-folder of the project root directory. The config file should follow the rule specified here. CircleCI config file consists of three basic definitions as follows:

  • version - configuration file version. Commonly used versions are 2 or 2.0
  • workflow - defines the complete workflow of continuous integration and deployment. Workflow can have multiple jobs.
  • jobs - contains the list of steps to be run.

Building and publishing Docker image

  • Define simple workflow using workflows tag for building and deployment of docker image. In workflow definition, filters can be used for specifying branches that are used for running a job. For example:
workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build:
          filters:
            branches:
              only:
                - master
                - develop
  • jobs can be a single step or multiple steps. Steps execute sequentially, for example, build a Docker image and deploy to the docker registry. Below are few basic configurations required for baking docker image
    • setup_remote_docker, define docker environment for building docker image.
    • working directory, the folder where a code copied for running build commands.
    • Environment variables, for saving credentials and other build parameters securely. Refer below the code snippet for the job definition
jobs:
  build:
    working_directory: ~/docker-superset
    docker:
      - image: docker:17.05.0-ce-git

    steps:
      - checkout
      - setup_remote_docker:
         version: 17.05.0-ce

      - run:
         name: list files
         command: ls -lsrt ~/docker-superset

      - run:
         name: Build Docker image
         command: docker build -t abhioncbr/docker-superset:$SUPERSET_VERSION --build-arg SUPERSET_VERSION=$SUPERSET_VERSION -f ~/docker-superset/docker-files/Dockerfile .

      - run:
          name: Push to DockerHub
          command: |
            docker login -u $DOCKERHUB_LOGIN -p $DOCKERHUB_PASSWORD
            docker push abhioncbr/docker-superset:$SUPERSET_VERSION

View complete config file here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment