DEV Community

Mingming Ma
Mingming Ma

Posted on

GitHub Actions Workflows: CI practice

I would like to share my process about the use of Continuous Integration (CI) with GitHub Action on my Python project txt2html

CI workflow file location

To create GitHub Action CI workflow file, we need to create a file like .github/workflows/ci.yml, the YAML file defining the workflow must be placed in the .github/workflows directory of your repository. The file must have either a .yml or .yaml file extension.

Note that the correct folder name is workflows, not workflow as I mistakenly used before.

Workflow basics

Look at my c1.yml file of my project

# .github/workflows/ci.yml

# Continuous Integration (CI) Workflow
name: ci

# This workflow will run whenever we push commits to the `main` branch, or
# whenever there's a pull request to the `main` branch. See:
# https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#on
on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

jobs:
  lint:
    name: Lint with Flake8
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.8'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8
      - name: Run Flake8
        run: flake8 .

  format:
    name: Check formatting with Black
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.8'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install black
      - name: Run Black
        run: black . --check

  test:
    name: Run Unit Tests
    runs-on: ubuntu-latest
    needs: [lint, format] # Ensures lint and format jobs complete successfully before running tests
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.8'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pytest
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
      - name: Run Tests
        run: pytest

Enter fullscreen mode Exit fullscreen mode

How it is triggered: on section

  1. When a pull request is made to the main branch.
  2. When commits are pushed to the main branch.

Note that the pull request event runs the workflow on code that is proposed to be merged but isn't in the main branch yet. It is a preemptive check that occurs before the merge. It is common when we make a PR and see the checks of our commits

Running Jobs: job section

It includes three jobs:

  1. Lint: This job uses Flake8 to check the code for style issues.
  2. Format: This job checks the code formatting using Black, a Python code formatter.
  3. Test: This job runs after both the lint and format jobs complete successfully (needs: [lint, format]). It runs the unit tests using pytest.

Top comments (0)