An Introduction to DevOps With CircleCI
Continuous Integration With CircleCI
Modern software development is a collaborative activity in which development teams work on a shared code base, with each member contributing updates, new features, and fixing bugs. In a collaborative setting where code quality is crucial, manual operations in the software cycle such as testing are not necessarily a fair practice.
In this article, we'll go through the concept of CI/CD with CircleCI and how to set up a pipeline on CircleCI to execute tests automatically.
Let's start by ensuring you have the following requirements in place.
- A CircleCI account
- A GitHub account
- An understanding of how pipelines work
- A clone of our demo application
Do you like coffee? Fill your coffee mug; if coffee isn't your jam, grab your favourite beverage and join me.
What Is CI/CD?
Continuous/constant integration is abbreviated as CI, while continuous delivery/deployment is abbreviated as the CD. Both are essential components of the DevOps lifecycle.
Continuous integration is a practice that occurs when a developer commits code changes to GitHub or another code repository. As a result of this event, specific actions are triggered that check the code to ensure that it integrates easily with the existing codebase. This could happen frequently or at predetermined periods. The primary goal is to uncover integration issues and enforce quality checks to establish greater cohesiveness and development collaboration.
After successful integration, continuous delivery occurs. That could be after all tests, lint checks, and so on. At this point, everyone is confident in the software's quality because all conditions for its final release have been met. So, what comes next? If a new feature is incorporated, updates can now be confidently released to customers in a timely and sustainable manner. The continuous deployment/release is entirely automated and requires no user intervention.
The automated process involving CI and CD is known as the CI/CD pipeline.
How does CI/CD improve the software development cycle now that we've seen it? What are the advantages over other methods? So, let's look at the power that CI/CD possesses.
- Quick Delivery: Making changes, reverting changes, and deploying hotfixes are all accomplished with a single click, triggered by a developer action.
- Reliability: At the heart of automation is the elimination of human inefficiencies. With CI/CD, testers can detect bugs early in the development process and fix them rapidly.
- High-quality code: CI/CD allows developers to contribute code to a centralized repository where they can collaborate on detecting and correcting the most severe bugs.
- Customer Satisfaction: Because things are moving so quickly, clients are happy, and happy clients consume more and help you build a brand.
- Increased workforce productivity: Automated tests, deployments, and collaboration foster a positive work environment and flexible development that keeps the needle moving.
Everyone is happy and productive when CI/CD is implemented.
Congratulations on making it to this point in the article. In the following sections, we'll understand CircleCI, and I will demonstrate how to integrate CircleCI with GitHub and set up a pipeline on CircleCI.
What Is CircleCI?
To store your code, you'll need a code repository, such as GitHub or GitLab, which are platforms for storing, tracking, and collaborating on software projects. CircleCI, like the previous two code hubs, is a platform that enables development teams to automate build, test, and deployment.
CircleCI needs to get your code from code repositories like GitHub and Bitbucket to build, test, and deploy. You, as a developer, must instruct CircleCI on how to do so by providing some configurations.
Understanding the CircleCI Configuration
While CircleCI is intelligent enough to perform automated DevOps practices, it must know how to operate on that code. For example, when setting up a CI/CD pipeline for a Python app, you must run tests, and CircleCI must know the location of your requirements file, the testing library you use, and so on.
CircleCI runs pipelines from the
config.yml file, a YAML file stored in the root of your project directory in a directory called
.circleci that defines your project's entire pipeline. It contains the conditions you specify for a pipeline to run, such as the order, commands, and so on.
You will frequently come across the following terms while working with the config.yml file.
- version: denotes the CircleCI version
- command: the actual action to perform, for example,
pip install requirements.txt.
- steps: an ordered list of how commands are executed
- jobs: is a collection of steps executed as a unit, each with a unique name.
- Docker: An execution environment that defines the underlying technology or environment to run a job.
- workflows: a set of rules defining a collection of jobs and their run order
To get things right, let's look at the configuration we'll use in the following steps. It is located in the root directory of the cloned project in a folder
version: 2 # CircleCI version
build: # name of this job
docker: # executor
- image: cimg/python:3.9.2 # Docker image(CircleCI authored) version
- checkout # Retrieve code from GitHub
# Create a venv, activate it, then install the pip packages
name: Install pip packages # name of this action
python3 -m venv venv
pip install -r requirements.txt
# run tests using pytest in verbose mode
name: Test with pytest
Above, we define the CircleCI version first, then specify the job and assign it an execution environment, in this case, Docker. Next, we list the actions under the steps key in the correct sequence: first, we pull the code from GitHub; then, just like in a local environment, we create a virtual environment, activate it, and install the pip packages from the requirements file. Finally, we can use the pytest command to run tests.
That appeared oversimplified.
For more information, see the Introduction to YAML Configurations guide.
Setting Up a CI/CD Pipeline in CircleCI
Make sure you've pushed the cloned project to your GitHub account before you start working with CircleCI. It is already fully configured to run automated tests.
Is that all done? Set up a continuous integration pipeline as follows.
Step 1: Log in to your CircleCI account, pick your project from the Projects tab, and then click Set Up Project.
Step 2: In the pop-up window that appears, the first option is selected by default; in the input box with a placeholder "From which branch," type in the name of the branch you pushed your project to on GitHub; by default, you must have pushed it to main; type in "main". Click "Set Up Project".
CircleCI will smartly trigger your workflow, and the status of your workflow run will be displayed on the next page.
Once your workflow has succeeded, you can click on the green status badge to get more insights into your job(s).
Congratulations! That's all about setting up a CI pipeline.
You've made it to the end of this article. Did you discover anything new or find what you were looking for? I certainly hope so.
This article taught us why CI/CD is essential, what it is, and how to set up a pipeline on CircleCI. We successfully configured a CircleCI pipeline to run tests on our Python application. Before that, we spent some time learning the terminology used in the CircleCI configuration. You also understand the CircleCI configuration file's structure and what it does. Automation is fun and interesting. That concludes this article; until next time, continue to learn and share your thoughts in the comments section.
Have fun along the way.