CI/CD
This document explains how to set up the GitHub Actions workflow for automated linting, testing, and badge generation.
Overview
Section titled “Overview”The CI workflow (.github/workflows/ci.yml) automatically:
- Runs Ruff linting on all Python code
- Runs pytest with coverage reporting
- Enforces minimum 75% test coverage
- Updates dynamic badges for test status and coverage percentage
Workflow Triggers
Section titled “Workflow Triggers”The workflow runs on:
- Push to
mainordevelopbranches - Pull requests to
mainordevelopbranches - Manual trigger via
workflow_dispatch
1. Lint Job
Section titled “1. Lint Job”- Runs Ruff linter (
ruff check) - Runs Ruff formatter check (
ruff format --check) - Fails if any linting issues are found
2. Test Job
Section titled “2. Test Job”- Runs pytest with coverage
- Requires minimum 75% coverage (fails if below)
- Extracts test count and coverage percentage
- Updates dynamic badges (on
mainbranch only)
Badge Setup
Section titled “Badge Setup”The workflow uses dynamic badges that update automatically with each CI run. This requires a one-time setup:
Step 1: Create a GitHub Gist
Section titled “Step 1: Create a GitHub Gist”- Go to https://gist.github.com/
- Create a new public gist with any filename (e.g.,
badges.md) - Add minimal content (e.g.,
# Badges) - Create the gist and note the gist ID from the URL
- URL format:
https://gist.github.com/USERNAME/GIST_ID - Example: If URL is
https://gist.github.com/FredDsR/abc123def456, the gist ID isabc123def456
- URL format:
Step 2: Create a Personal Access Token
Section titled “Step 2: Create a Personal Access Token”- Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic)
- Click “Generate new token (classic)”
- Give it a descriptive name (e.g., “CI Badge Updates”)
- Select scope: gist (only this scope is needed)
- Generate token and copy it immediately (you won’t see it again)
Step 3: Add Repository Secrets
Section titled “Step 3: Add Repository Secrets”- Go to your repository → Settings → Secrets and variables → Actions
- Add two new repository secrets:
- Name:
GIST_SECRET- Value: The personal access token from Step 2
- Name:
GIST_ID- Value: The gist ID from Step 1
- Name:
Step 4: Update README Badges
Section titled “Step 4: Update README Badges”Replace GIST_ID in the README.md badge URLs with your actual gist ID:
Replace YOUR_GIST_ID with the actual gist ID from Step 1.
How It Works
Section titled “How It Works”- On every push/PR, the workflow runs linting and tests
- If tests pass, it extracts:
- Total number of tests
- Coverage percentage
- On
mainbranch, it updates two JSON files in the gist:tests-badge.json: Test count badge datacoverage-badge.json: Coverage percentage badge data
- Badges in README automatically display the latest values
Coverage Badge Colors
Section titled “Coverage Badge Colors”The coverage badge automatically changes color based on percentage:
- 🔴 Red: 0-59%
- 🟡 Yellow: 60-74%
- 🟢 Green: 75-100%
Note: The build will fail if coverage is below 75% (enforced by --cov-fail-under=75). The badge color is visual only; the actual pass/fail threshold is exactly 75%.
Local Testing
Section titled “Local Testing”Before pushing, you can run the same checks locally:
# Lint checkuv run ruff check src/ tests/uv run ruff format --check src/ tests/
# Run tests with coverageuv run pytest --cov=arandu --cov-report=term-missing --cov-fail-under=75Troubleshooting
Section titled “Troubleshooting”Badges not updating
Section titled “Badges not updating”- Verify the workflow ran successfully (check Actions tab)
- Check that secrets
GIST_SECRETandGIST_IDare set correctly - Ensure the gist is public (private gists won’t work with shields.io)
- Wait a few minutes for shields.io cache to refresh
Coverage failing
Section titled “Coverage failing”If coverage drops below 75%, the workflow will fail. To fix:
- Add more tests to increase coverage
- Or adjust the threshold in
.github/workflows/ci.yml(line with--cov-fail-under=75)
Linting failures
Section titled “Linting failures”If linting fails:
- Run
uv run ruff check --fix src/ tests/to auto-fix issues - Run
uv run ruff format src/ tests/to format code - Commit the changes
Workflow File Location
Section titled “Workflow File Location”.github/workflows/ci.yml
Required Dependencies
Section titled “Required Dependencies”The workflow uses:
astral-sh/setup-uv@v5- Install uv package manageractions/setup-python@v5- Install Python 3.13schneegans/dynamic-badges-action@v1.7.0- Update gist badges