Using GitHub Actions To Add Continuous Integration (CI) To A Project (GitHub / Python)

In this blog post I will be discussing Continuous Integration (CI) with GitHub Actions.

Readers of previous blog entries may remember that I’ve tackled Continuous Integration (CI) with GitLab previously (see: https://geektechstuff.com/2021/04/05/python-unit-test-and-some-ci-via-gitlab-python/ ) and that last autumn (2021) I got into more testing (see: https://geektechstuff.com/2021/10/06/personal-update-django-and-mvc-mtv/ ). 

Thankfully both have helped as the current module of my degree apprenticeship is around Software Engineering and Agile, with the assignment focusing on web development via Python (Django, FastAPI, Flask etc). I’m using Flask, and as the coding will be assessed I really wanted to make sure that it didn’t break as I created / updated it, so I’ve put in some unit tests.The coding for my assignment (eventually) has to be public, so I’m hosting it on public GitHub ( https://github.com/geektechdude ), and as it’s on GitHub I decided to add some CI in case I forget to run the tests locally before pushing a branch or a commit to main.

Unit Tests

My project contains a directory called “tests” with various Unit Tests inside. As mentioned above, when I make changes, I run the Unit Tests to a) make sure nothing has broken and b) make sure that the functions work as expected (e.g. entering a serial number records the correct data). On my local machine I can run a command (flask test) to see the tests run and the outputs.

  • A Unit Test file with some assertions
  • Python Unit Tests running

GitHub Actions

When pushing to my GitHub Repo (main or a branch) I have a GitHub Actions file (github-actions.yml) written in YAML which asks GitHub to run the same Unit Tests. This is helpful in case I forget to run the tests when making a change. The GitHub Action file resides within a directory called “workflows” within the “.github” directory at the root of my project (e.g. /PROJECT FOLDER/.github/workflows/github-actions.yml).

Screenshot showing directory layout of project
Project Directory Layout

The GitHub Actions file is made up of a name, a trigger and then the actions to carry out. In my case this is “Flask App Tests”, a “push” and then a build of Ubuntu with Python 3.9, an install of my projects dependencies (via pip requirements.txt) and finally the Unit Tests running.

GitHub Actions YAML file
GitHub Actions YAML file

GitHub checks the .github/worklows directory and automatically creates actions from the plans it finds, adding them to the repository under the actions tab as a workflow. If the action runs and has a failure (e.g. due to a Unit Test failing) then GitHub Actions emails to say that the build has failed. 

  • GitHub Actions Workflow screen
  • A GitHub Actions job failure email

One response to “Using GitHub Actions To Add Continuous Integration (CI) To A Project (GitHub / Python)”

  1. Running Flask As A Linux Service (Linux / Python) – Geek Tech Stuff Avatar

    […] on with the Software Engineering & Agile module of my degree, I need to be able to show my Flask web application running. After picking a cloud platform (in […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect