
(4.6-continuous-integration)=
# 🧩 4.6 Continuous Integration

```{contents}
:depth: 3
```

## 🔰 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](https://www.youtube.com/watch?v=1er2cjUq1UI)

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

```yaml
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.

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

4. **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](https://www.youtube.com/watch?v=R8_veQiYBjI)

### 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:
   ```yaml
   - 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:
   ```yaml
   - 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

- [GitHub Actions Documentation](https://docs.github.com/en/actions)
- [Continuous Integration Overview](https://www.atlassian.com/continuous-delivery/continuous-integration)
- [Pytest Documentation](https://docs.pytest.org/en/stable/)
- [Sphinx Documentation](https://www.sphinx-doc.org/en/master/)
- [CI/CD Best Practices](https://www.redhat.com/en/topics/devops/what-is-ci-cd)
- [GitHub Actions for Python Applications](https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python)

## 🚀 Quiz

::::{tab-set}
:sync-group: category

:::{tab-item} W 2024
:sync: w2024

:::

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/370070/assignments/1393628?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/393352/assignments/1499731?display=full_width_with_nav
:::

::::

## 📄 Assignment

::::{tab-set}
:sync-group: category

:::{tab-item} W 2024
:sync: w2024

:::

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/370070/assignments/1394236?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/393352/assignments/1499732?display=full_width_with_nav
:::

::::
