Create CI/CD pipeline using CircleCI

Introduction:

In this blog, we’re going to learn how to create CI/CD pipeline using CircleCI. By the end of this article, you’ll get hands-on experience in writing your own pipeline.

CircleCI’s primary objective is to enable software teams to accelerate their innovation process by effectively managing change. It empowers technology-driven organizations to achieve their highest potential, enhancing productivity and fostering innovation. CircleCI offers enterprise-class support and services, supporting various platforms such as Linux, macOS, Android, and Windows, both on the cloud and on-premises.

Through intelligent automation, CircleCI facilitates seamless build, test, and deployment processes.

What is CircleCI?

Circle CI is a powerful Continuous Integration tool that automates the entire build and deployment process. CircleCI leverages containers to run multiple builds and deployments in parallel.

Circle CI continuously deploys your code to the docker container registry and enables you to track the status of your builds. Apply frontend, backend, and serverless architecture patterns by leveraging CircleCI’s intuitive and powerful CI/CD features that automate everything from managing Docker images and trays, to deploying services on Kubernetes.

CircleCI is a popular tool for building and deploying software. It offers a simple and flexible way to automate your workflow, allowing you to focus on writing code instead of managing infrastructure. In this blog, we will walk through the steps to create CI/CD pipeline using CircleCI.

Benefits of CircleCI:

  • Utilize the API to fetch information concerning jobs and workflows.
  • Leverage the CLI for local access to advanced tools.
  • Benefit from test insights to detect flaky tests.
  • Offers quick setup and installation features.
  • Offers automated parallelism.
  • Offers maintenance with no complexities.
  • Requires no dedicated server to operate CircleCI.

Pros of CircleCI:

  • Time-saving maintenance: CircleCI provides an effortless setup process and eliminates redundancies, leading to significant time savings. Its streamlined maintenance ensures simplicity without adding complexities.
  • Improved productivity: CircleCI’s integrated build, test, and deployment process contributes to enhanced productivity. Debugging build issues becomes more efficient with SSH access. Automated parallelism and other additional features boost productivity and build efficiency across various projects.
  • Consistency in builds across projects: CircleCI simplifies management and sharing of encrypted variables within the tool. SSH facilitates the secure sharing of these variables with build containers, promoting consistency across multiple projects.

Cons of CircleCI:

  • Available for only GitHub or BitBucket repositories
  • Not all CI/CD flow customizations are compatible with plugins.
  • As the tool does not offer unlimited builds, additional credits may need to be purchased.

Building a Pipeline using CircleCI

CircleCI is a popular tool for building and deploying software. It offers a simple and flexible way to automate your workflow, allowing you to focus on writing code instead of managing infrastructure. In this blog, we will walk through the steps to create CI/CD pipeline using CircleCI.

Pre-requisites:

Before we begin, please ensure you have the following:

  • A CircleCI account
  • A GitHub account.
  • A repository containing your code.

Install and Configure the CircleCI Local CLI:

Installation:

Linux: Install with Snap

To install the CircleCI command line interface (CLI), Docker, and both the security and auto-update features that accompany Snap packages, use the following commands:


sudo snap install docker circleci 
sudo snap connect circleci:docker docker

macOS: Install with Homebrew

If we are using Homebrew with macOS, we can install the CLI with the following command:

brew install circleci

If we already have Docker for Mac installed, we can use this command instead:

brew install --ignore-dependencies circleci

Windows: Install with Chocolatey

For Windows users, CircleCI offers a Chocolatey package:

choco install circleci-cli -y

Manual install

To download and install the CLI manually, visit the GitHub releases page. This method is ideal if we prefer the CLI to be installed in a specific path on our system.  

Configure the CLI:

Prior to utilizing the CLI, it is essential to generate a CircleCI API token from the Personal API Token tab. After getting your token, configure the CLI by running:

circleci setup

During the setup process, it will be prompted to enter configuration settings. When using the CLI with the CircleCI cloud, keep the default CircleCI host. In case of using the CircleCI server, modify the value to match your installation address (e.g., circleci.your-org.com).

Configuring through the CircleCI website:

We can also simply sign-up/login to the CircleCI website and can directly set up the project, this will prompt you to add the config.yml if it’s not added to your repository.

We can either add it from there or manually make the config file changes and commit to the repository.

Validate a CircleCI config

To further validate your configuration, browse to a directory with a circleci/config.yml file and run:


circleci config validate 
# Config file at .circleci/config.yml is valid

Step 1: Configure CircleCI

The first step is to configure CircleCI to build your code. To do this, you will need to create a configuration file called .circleci/config.yml in the root directory of your repository. This file defines the steps that CircleCI will follow to build and test your code.

An example of a simple configuration file:

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:10
    steps:
      - checkout
      - run: npm install
      - run: npm test]\
[

This configuration file defines a single job called build. The job uses a Docker image that includes Node.js version 10. The steps of the job are to check out the code from GitHub, install dependencies using npm, and run tests using npm test.

Step 2: Connect CircleCI to GitHub

Next, you will need to connect CircleCI to your GitHub account. This will allow CircleCI to access your repository and build your code.

To connect CircleCI to GitHub, follow these steps:

  1. Log in to your CircleCI account.
  2. Access the left sidebar and locate the ‘Add Projects’ button. Click on the ‘add projects’ and proceed.
  3. Find the repository you want to build and click the “Set Up Project” button.
  4. Follow the prompts to grant CircleCI access to your repository.

Step 3: Trigger a Build

With CircleCI configured and connected to your repository, you are now ready to trigger a build. To do this, simply push a commit to your repository. CircleCI will automatically detect the new commit and start a build.

You can monitor the progress of the build by going to the CircleCI dashboard. Once the build is complete, you can view the build logs and test results to see if everything ran successfully.

Step 4: Add Deployment Steps

Once you have a working build pipeline, you may want to add deployment steps to automatically deploy your code to a production environment. CircleCI offers a number of deployment integrations, including AWS, Heroku, and Google Cloud Platform.

Example: Python Sample Program

Here we will take an example of a configuration file that builds and tests simple Python program to add two numbers:

create CI/CD Pipeline using CircleCI
create CI/CD Pipeline using CircleCI

To add a deployment step, you will need to modify your configuration file to include the necessary deployment steps.

version: 2.1
jobs:
 build:
   working_directory: ~/circleci-python
   docker:
     - image: "circleci/python:3.9"
   steps:
     - checkout
     - run: python3 main.py
 test:
   working_directory: ~/circleci-python
   docker:
     - image: "circleci/python:3.9"
   steps:
     - checkout
     - run: python3 test_main.py
workflows:
 build_and_test:
   jobs:
     - build
     - test:
         requires:
           - build
create CI/CD Pipeline using CircleCI

This configuration file establishes two distinct jobs: Build and Test. We aim to execute main.py and conduct our tests. Additionally, we have specified that the build job and the test job should be performed for every branch.

The workflows section of the configuration file defines a workflow that runs both the build and test jobs. The test job will only run if the build job succeeds and the commit is on the master branch.

To enable CircleCI to execute the required tasks, it is necessary to grant them access to our repository, thereby authorizing them to perform these operations. By doing so, CircleCI will be able to automatically detect any alterations made to the code and initiate the build_and_test workflow for our branch. Consequently, all job executions can be conveniently monitored through CircleCI’s dashboard.

create CI/CD Pipeline using CircleCI- All pipelines
create CI/CD Pipeline using CircleCI-Test

Conclusion:

In this blog, we have walked through the steps to create CI/CD pipeline using CircleCI. By following these steps, you can automate your workflow and ensure that your code is built, tested, and deployed consistently and reliably.

Stay tuned for more!