π§© 4.6 Continuous Integration#
π° Tutorial#
In this module, you will learn about Continuous Integration (CI) and how to use GitHub Actions to automate tasks like running unit tests and building documentation. By the end of this module, youβll be able to:
Explain the purpose of continuous integration
Set up a GitHub Actions workflow
Run unit tests and documentation builds on GitHub Actions
What is Continuous Integration?#
Continuous Integration (CI) is a software development practice where code changes are automatically tested and integrated into a shared repository frequently. This practice helps catch issues early, improve code quality, and ensure the integrity of the project.
Purpose of Continuous Integration:#
Early Detection of Errors: Running automated tests ensures that new changes donβt introduce bugs.
Consistent Code Quality: CI tools run linters and other static analysis tools to enforce code quality standards.
Faster Development: Automating testing and integration allows developers to focus on writing code rather than manually running tests.
Team Collaboration: By frequently integrating code, CI reduces the chances of conflicts when multiple team members are working on the same codebase.
Video Tutorial: Continuous Integration Explained
Setting Up a GitHub Actions Workflow#
GitHub Actions is a powerful CI/CD tool that allows you to automate workflows directly in your GitHub repository. You can use it to run tests, lint code, build documentation, and more every time new code is pushed to the repository.
Steps to Set Up a GitHub Actions Workflow:#
Create a Workflow File: In your GitHub repository, create a
.github/workflows/directory and add a YAML file for your workflow. For example, create aci.ymlfile.Define Workflow: In the
ci.ymlfile, define the steps to run the CI process. Below is an example of a workflow that runs unit tests and builds documentation.
name: CI Workflow
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run unit tests
run: |
pytest
- name: Build documentation
run: |
cd docs
make html
This workflow will:
Trigger on
pushorpull requestevents targeting themainbranch.Set up Python 3.8, install dependencies, run unit tests, and build the documentation.
Push to GitHub: Once the workflow file is set up, push the changes to your GitHub repository. GitHub will automatically trigger the workflow.
Monitor Workflow Execution: Go to the βActionsβ tab in your GitHub repository to see the workflow running and check its results.
Video Tutorial: Getting Started with GitHub Actions
Running Unit Tests and Documentation Builds on GitHub Actions#
The CI process often includes running automated tests and building documentation to ensure that everything works as expected after each code change.
Running Unit Tests on GitHub Actions:#
Install Testing Framework: Make sure that
pytestor any other testing framework you are using is included in yourrequirements.txtfile.Define Test Step: In the workflow, include a step to run the tests:
- name: Run unit tests run: | pytest
Building Documentation on GitHub Actions:#
Install Sphinx: Ensure that
Sphinxand its dependencies are included in yourrequirements.txtfile.Add Documentation Build Step: In the workflow, add a step to build the documentation:
- name: Build documentation run: | cd docs make html
This step will navigate to the docs folder and run the make html command to generate the HTML documentation.