A comprehensive CI/CD template for Python projects with automated code quality checks, testing, and package building using GitHub Actions.
- π Git commit message format validation
- π¨ Code formatting with Ruff and clang-format
- π§ Static type checking with MyPy
- π§ͺ Comprehensive testing with pytest
- π¦ Automated package building and validation
- π Optional PyPI publishing and GitHub releases
This project enforces a structured commit message format to ensure clear and traceable project history.
<type>[<SCOPE>]: <short-summary>
Problem:
<description of the problem being solved>
Solution:
<description of the solution implemented>
Test:
<description of how the change was tested>
JIRA: <PROJECT-123>
<type>: Type of changefeat: New featurefix: Bug fixdocs: Documentation changesstyle: Code style changes (formatting, etc.)refactor: Code refactoringtest: Adding or updating testschore: Maintenance tasks
<SCOPE>: Area of the codebase affected (e.g.,api,auth,ui,db)<short-summary>: Brief description in imperative moodJIRA: Reference to JIRA ticket in formatPROJECT-123
feat[auth]: add JWT-based user authentication
Problem:
- The application needs secure user authentication to protect sensitive operations and provide personalized user experiences.
Solution:
- Implemented JWT-based authentication system with bcrypt password hashing, including login, logout, and token refresh mechanisms. Added middleware for route protection and user session management.
Test:
- Added comprehensive unit tests for authentication functions (95% coverage) and integration tests for login/logout endpoints. Tested token expiration and refresh scenarios.
JIRA: AUTH-456
The recommended project structure follows Python packaging best practices:
your-python-project/
βββ .github/
β βββ workflows/
β βββ ci.yml # Main CI/CD pipeline
βββ ci/ # CI tools directory
β βββ check_mr_logs.py # Git commit message checker
β βββ code_format_helper.py # Code formatting checker
β βββ typing_helper.py # Static type checker
β βββ ruff.toml # Ruff configuration
βββ src/ # Source code directory
β βββ your_package/
β βββ __init__.py
β βββ main.py
βββ tests/ # Test directory
β βββ __init__.py
β βββ test_main.py
βββ .gitignore # Git ignore file
βββ .gitmessage # Git commit template
βββ pyproject.toml # Project configuration
βββ requirements.txt # Production dependencies
βββ requirements-dev.txt # Development dependencies
βββ README.md # This file
βββ LICENSE # License file
βββ CHANGELOG.md # Change log
Modern Python project configuration file containing:
- Project metadata and dependencies
- Build system configuration
- Tool configurations (pytest, mypy, ruff)
[project]
name = "your-project-name"
version = "0.1.0"
description = "Your project description"
requires-python = ">=3.9"
dependencies = []
[project.optional-dependencies]
dev = ["pytest>=7.0", "mypy>=1.0", "ruff>=0.1"]Code formatting and linting configuration:
- Line length: 88 characters
- Python target version: 3.11
- Enabled rules: pyflakes, pycodestyle, security warnings
The GitHub Actions workflow provides a comprehensive automated pipeline that runs on every push and pull request.
graph TD
A[Push/PR] --> B[Check Commit Messages]
B --> C[Code Format Check]
B --> D[Type Check]
B --> E[Unit Tests]
C --> F[Build Check]
D --> F
E --> F
F --> G{Main Branch?}
G -->|Yes| I[Build & Archive Package]
G -->|No| J[Complete]
I --> J
- Validates git commit message format
- Ensures all commits follow the required template
- Trigger: All pushes and PRs
- Failure: Stops entire pipeline
- Python: Formatting and linting with Ruff
- C/C++: Formatting with clang-format
- Checks only changed files for efficiency
- Configuration: Uses
ci/ruff.toml
- Static type analysis with MyPy
- Checks only changed Python files
- Configuration: Defined in
pyproject.toml
- Runs comprehensive test suite with pytest
- Matrix testing: Python 3.9, 3.10, 3.11, 3.12
- Coverage reporting: Generates coverage reports
- Integration: Uploads coverage to Codecov (optional)
- Validates package can be built correctly
- Dependencies: All previous checks must pass
- Uses
python -m buildandtwine check
- Trigger: Only on main branch pushes after all checks pass
- Package Building: Creates wheel and source distributions
- Validation: Verifies package integrity with twine
- Artifacts: Saves packages for 30 days
- Build Info: Generates detailed build reports
- Optional Release: Creates GitHub release if tag is pushed
Each successful build produces:
- Package files:
.whl(wheel) and.tar.gz(source distribution) - Build summary: Detailed report with version, commit info, and status
- 30-day retention: Available for download from GitHub Actions
# Setup development environment
pip install -r requirements-dev.txt
# Pre-commit checks
ruff format . # Format code
ruff check . # Lint code
mypy src/ # Type check
pytest # Run tests
# Commit with proper format
git commit # Uses .gitmessage template-
Enable GitHub Actions in repository settings
-
Branch Protection
for main branch (recommended):
- Require PR reviews
- Require status checks to pass
- Require branches to be up to date
PYPI_API_TOKEN: For PyPI package publishingCODECOV_TOKEN: For code coverage reporting
-
Use this template or copy the structure
-
Copy CI tools
to
ci/directory:
cp your-tools/* ci/chmod +x ci/*.py
-
Copy workflow to
.github/workflows/ci.yml -
Update configuration
:
- Modify
pyproject.tomlwith your project details - Update package name and structure
- Modify
-
Install dependencies:
pip install -r requirements-dev.txt -
Make first commit using the required format
-
Push to GitHub and watch the CI pipeline run
To enable automatic PyPI publishing:
-
Add PyPI token to GitHub Secrets
-
Push a git tag
for version release:
git tag v1.0.0git push origin v1.0.0
-
Automatic publishing will be triggered for tagged releases
- Fork the repository
- Create a feature branch
- Follow the commit message format
- Ensure all CI checks pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- π Documentation: Check the workflow files and configuration
- π Issues: Report bugs via GitHub Issues
- π‘ Suggestions: Feature requests welcome