Skip to content

Continuous Integration

As explained, continuous integration is the automated pipeline process spawned using GitHub Actions in order to validate that the code changes can be integrated without errors, triggered by an event defined in the YML file. These checks are also defined by you, the user, in the .github/workflows/continuous-integration.yml file, which follows the GitHub Actions syntax.

Example Configuration File

YAML
name: Continuous Integration

on:
  push:
    branches: [main]
    # The pipeline will run on pushes to the main branch only
    paths-ignore:
      - 'docs/**'
    # Documentation folders are often large and unneeded to run tests
  pull_request:
    branches: [main]
    paths-ignore:
      - 'docs/**'

jobs:
  pre-commit-check:
    runs-on: ubuntu-latest
    steps:
      -
        name: Setup code repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 1
        # Fetch the code, from the latest commit of the current branch
      -
        name: Setup Python 3.8
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
        # Make sure Python is installed in this virtual GitHub environment
      -
        name: Load cached pre-commit repos
        id: cached-pre-commit-repos
        uses: actions/cache@v2
        with:
          path: |
            ~/.cache/pre-commit
          key: precommit-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }}
        # Load cached pre-commit configs
      -
        name: Get all changed files
        id: changed-files
        uses: tj-actions/changed-files@v36
        # Get all the changed files that were changed
      -
        name: Perform pre-commit checks # Run Precommit checks on those files
        run: |
            pip install pre-commit
            pre-commit run --files ${{ steps.changed-files.outputs.all_changed_files }} 
  github-tests:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    strategy:
      matrix:
        python-version: [ "3.8" ]
    steps:
      -
        name: Setup code repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 1
      -
        name: Setup Python
        uses: actions/setup-python@v4
        with:
            python-version: ${{ matrix.python-version }}
      -
        name: Install poetry
        run: |
          pip install poetry==$POETRY_VERSION
      -
        name: Setup ssh agent
        uses: webfactory/ssh-agent@v0.4.1
        with:
            ssh-private-key: ${{ secrets.SPS_SSH_ID }}
        # Use GitHub Secret to get SSH key to allow private repo download
      -
        name: Install linux-dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -yqq --no-install-recommends curl ssh git build-essential
      -
        name: Install requirements
        # This will often install private repos, hence the need for above SSH
        run: |
          poetry install
      -
        name: Run tests
        # Run your predefined test cases in this GitHub virtual environment
        run: |
          echo "Running tests!"
          poetry run pytest
          echo "Finished running tests!"

Example Workflow Diagram