CI / CD integration in simple python project

Tram Ho

1. Introduction

CI is Continuous Integration. It is a software development method that requires team members to integrate their work regularly, at least once a day. Each integration is automatically “built” (including tests) to detect errors as quickly as possible. The whole team recognizes that this approach minimizes integration problems and allows for faster software development.

A CI script starts with a developer commit code on the repository (github, for example). Any change will trigger a CI life cycle. The steps in a CI scenario are usually as follows:

  • First, the developer commits the code to the repo.
  • The CI server monitors the repo and checks to see if there are any changes on the repo (continuously, such as once a minute)
  • Once the commit occurs, the CI server detects that the repo has changed, so it receives the latest code from the repo and then builds, runs the unit, and integrates the test.
  • The CI server generates feedback and sends it to the project members
  • CI server continues to wait for changes in the repo

Using CI, you will save time:

  • More speed in coding and fixbug
  • Be confident that you build stable software with fewer errors
  • Ensure the product works on different machines
  • Eliminate a lot of expenses and allow you to focus on what’s important
  • Reduce time to resolve conflicts

2. Demo

Initialize a project with a virtual environment

Clone project from github account + install virtual environment, here I have 1 repository CalculatorLibrary

on your computer must have python and virtualenv installed, the version of python depends on your choice, here I choose python 3.6

Write a basic python example: here I have the calculator.py file

From the CalulatorLibrary section, there will be the following:

Writing Test:

The first step involves running a program, to parse the code for potential errors. flake8 is often used to check if your code matches the standard Python encoding. Make sure your code is readable for the rest of the Python community.

The second step is unit testing. A unit test is designed to test a function or code unit. Python comes with a standard unit testing library, but other libraries exist and are very popular. This example uses pytest.

A standard practice that goes hand in hand with testing is calculating code (covered). Code coverage is the percentage of code covered by your tests. pytest has an extension, pytest-cov, which helps you understand your code coverage.

Create test file for calculation function, file test_calculator.py

commit and push the code to your repo

connect with CircleCI

CircleCI needs to know how you run the project, requires a specific format. CircleCi requires 1 .circleic folder in your repo and 1 config file in it. The config file includes the structure for all the steps to build the server needed to execute. CircleCi requires a file named config.yml. This file uses a data initialization language, specifically can be found as follows . The purpose of YAML is for the reader to work and work well with the common programming language. There are 3 ways to express data in YAML file:

  • Mappings (key-value pairs)
  • Sequences (lists)
  • Scalars (strings or numbers)

Create a config.yml file in the .circleci directory corresponding to the project demo

Initialize circleci management at https://circleci.com/ . Login with your github account and connect to create a job corresponding to the CalculatorLibrary repo The page will also create you the config.yml file corresponding to the project’s programming language.

Push the code up and test it !!!

Will receive the same interface as above, encounter 1 error pep8 already. fix according to the comment and then push it again

the rest of my Continuous Deployment will be in the next post !! Source: https://realpython.com/python-continuous-integration/#next-steps

Share the news now

Source : Viblo