🧩 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:

  1. Explain the purpose of continuous integration

  2. Set up a GitHub Actions workflow

  3. 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:#

  1. Create a Workflow File: In your GitHub repository, create a .github/workflows/ directory and add a YAML file for your workflow. For example, create a ci.yml file.

  2. Define Workflow: In the ci.yml file, 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 push or pull request events targeting the main branch.

  • Set up Python 3.8, install dependencies, run unit tests, and build the documentation.

  1. Push to GitHub: Once the workflow file is set up, push the changes to your GitHub repository. GitHub will automatically trigger the workflow.

  2. 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:#

  1. Install Testing Framework: Make sure that pytest or any other testing framework you are using is included in your requirements.txt file.

  2. Define Test Step: In the workflow, include a step to run the tests:

    - name: Run unit tests
      run: |
        pytest
    

Building Documentation on GitHub Actions:#

  1. Install Sphinx: Ensure that Sphinx and its dependencies are included in your requirements.txt file.

  2. 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.

Additional Resources#

πŸš€ Quiz#

πŸ“„ Assignment#