I tried to automate CI/CD with CircleCI and github

2022.09.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

With the need for DevOps in the automation of our applications, I have been doing a few studies and I stumbled on CircleCI as a CI/CD tool and decided to look a bit deeper.

In this post, I will be explaining with a simple demo an introduction to CircleCI.

Introduction

CircleCI is a service that detects changes in your codebase and executes tasks defined on it.

What is CI/CD?

This is a common term that everyone uses, it explains the continuous integration and continuous deployment

Continuous Integration (CI) - When you push a code update, you want to know if the code is still okay. For example, does the code compile (build), and do all of the tests pass?

Continuous Deployment (CD) - After making a code update, deploy that code to wherever it is supposed to be. For example, deploy a server to AWS. CI/CD is extremely valuable because automation saves time and optimizes workflows, which is especially important when working with others.

Setting up CI with CircleCI

From the GitHub repository, activate CI on it by configuring CircleCI.

First, generate a configuration file in order for CircleCI to understand what we want it to accomplish. Create a folder called ".circleci" and a file called "config.yml" inside it.

In the config.yml add this code:
version: 2.1
jobs:
build:
working_directory: ~/circleci-python
docker:
- image: "circleci/python:3.6.4"
steps:
- checkout
- run: python3 main.py
test:
working_directory: ~/circleci-python
docker:
- image: "circleci/python:3.6.4"
steps:
- checkout
- run: python3 main-test.py
workflows:
build_and_test:
jobs:
- build
- test:
requires:
- build

NOTE: make sure your config.yml is indented correctly

Now let's push our new config file to GitHub.

CircleCI should detect the code changes and run our build_and_test workflow for our branch. Also, the jobs run on CircleCI must be visible.

Testing the CircleCI

In the main-test.py file, I added a new line of code

assert Add(6,6) ==11

that is false, to check how the build output will act.

Conclusion

CI/CD is important, and I have shown how it can be integrated with a simple project.I will be setting up more complicated projects in the future.Look out for more on this post.