|
| 1 | +# Reusable Workflows Guide |
| 2 | + |
| 3 | +This template provides a comprehensive set of reusable GitHub Actions workflows that can be called from other repositories. These workflows are designed to provide standardized CI/CD operations for Python projects using UV. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +All reusable workflows are prefixed with `rw_` and can be called from external repositories using the format: |
| 8 | + |
| 9 | +```yaml |
| 10 | +uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_<workflow_name>.yaml@master |
| 11 | +``` |
| 12 | +
|
| 13 | +## Reusable Actions |
| 14 | +
|
| 15 | +In addition to workflows, this template also provides **reusable actions** that can be called from external repositories. Actions provide reusable functionality that can be used across multiple workflow steps. |
| 16 | +
|
| 17 | +### Available Actions |
| 18 | +
|
| 19 | +#### `setup-python-uv` |
| 20 | +A composite action that sets up Python, installs UV, and optionally installs project dependencies with caching support. |
| 21 | + |
| 22 | +**Inputs:** |
| 23 | +- `python-version` (string, optional): Python version to use (default: '3.13') |
| 24 | +- `install-dependencies` (boolean, optional): Whether to install project dependencies (default: 'true') |
| 25 | +- `dependency-groups` (string, optional): Dependency groups to install (default: 'release-ci') |
| 26 | +- `cache-dependencies` (boolean, optional): Whether to cache UV dependencies (default: 'true') |
| 27 | + |
| 28 | +**Usage:** |
| 29 | +```yaml |
| 30 | +steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v5 |
| 33 | + |
| 34 | + - name: Setup Python with UV |
| 35 | + uses: Chisanan232/Template-Python-UV-Project/.github/actions/setup-python-uv@master |
| 36 | + with: |
| 37 | + python-version: '3.12' |
| 38 | + install-dependencies: 'true' |
| 39 | + dependency-groups: 'dev,test' |
| 40 | + cache-dependencies: 'true' |
| 41 | +``` |
| 42 | + |
| 43 | +**Features:** |
| 44 | +- Automatically sets up Python with the specified version |
| 45 | +- Installs and configures UV package manager |
| 46 | +- Intelligent caching of UV dependencies for faster builds |
| 47 | +- Flexible dependency group installation |
| 48 | +- Verification steps to ensure proper setup |
| 49 | + |
| 50 | +## Available Reusable Workflows |
| 51 | + |
| 52 | +### Core Testing & CI Workflows |
| 53 | + |
| 54 | +#### 1. `rw_build_and_test.yaml` |
| 55 | +Runs comprehensive test suite including unit, integration, e2e, and contract tests. |
| 56 | + |
| 57 | +**Inputs:** |
| 58 | +- `run_e2e` (boolean, optional): Whether to run end-to-end tests (default: false) |
| 59 | + |
| 60 | +**Secrets:** |
| 61 | +- `e2e_test_api_token` (optional): API token for e2e tests |
| 62 | + |
| 63 | +**Usage:** |
| 64 | +```yaml |
| 65 | +jobs: |
| 66 | + test: |
| 67 | + uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_build_and_test.yaml@master |
| 68 | + with: |
| 69 | + run_e2e: true |
| 70 | + secrets: |
| 71 | + e2e_test_api_token: ${{ secrets.YOUR_API_TOKEN }} |
| 72 | +``` |
| 73 | + |
| 74 | +#### 2. `rw_run_all_test_and_record.yaml` |
| 75 | +Orchestrates all testing and uploads coverage reports to CodeCov and SonarCloud. |
| 76 | + |
| 77 | +**Inputs:** |
| 78 | +- `run_e2e` (boolean, optional): Whether to run end-to-end tests (default: false) |
| 79 | + |
| 80 | +**Secrets:** |
| 81 | +- `codecov_token` (optional): CodeCov token for uploading coverage |
| 82 | +- `sonar_token` (required): SonarCloud token for code quality analysis |
| 83 | +- `e2e_test_api_token` (optional): API token for e2e tests |
| 84 | + |
| 85 | +**Usage:** |
| 86 | +```yaml |
| 87 | +jobs: |
| 88 | + ci: |
| 89 | + uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_run_all_test_and_record.yaml@master |
| 90 | + secrets: |
| 91 | + codecov_token: ${{ secrets.CODECOV_TOKEN }} |
| 92 | + sonar_token: ${{ secrets.SONAR_TOKEN }} |
| 93 | +``` |
| 94 | + |
| 95 | +#### 3. `rw_uv_run_test_with_multi_py_versions.yaml` |
| 96 | +Runs tests across multiple Python versions using UV. |
| 97 | + |
| 98 | +**Inputs:** |
| 99 | +- `test_type` (string, required): Type of test (unit-test, integration-test, e2e-test, etc.) |
| 100 | +- `test_folder` (string, required): Path to test folder |
| 101 | +- `install_dependency_with_group` (string, optional): UV dependency group to install |
| 102 | +- `max-parallel` (number, optional): Maximum parallel jobs |
| 103 | + |
| 104 | +**Secrets:** |
| 105 | +- `e2e_test_api_token` (optional): API token for e2e tests |
| 106 | + |
| 107 | +### Release & Publishing Workflows |
| 108 | + |
| 109 | +#### 4. `rw_parse_release_intent.yaml` |
| 110 | +Parses release configuration and determines what components should be released. |
| 111 | + |
| 112 | +**Inputs:** |
| 113 | +- `level` (string, optional): Release level (patch, minor, major, auto) |
| 114 | +- `python` (string, optional): Python package release (auto, force, skip) |
| 115 | +- `docker` (string, optional): Docker image release (auto, force, skip) |
| 116 | +- `docs` (string, optional): Documentation versioning (auto, force, skip) |
| 117 | +- `notes` (string, optional): Release notes |
| 118 | + |
| 119 | +**Outputs:** |
| 120 | +- `do_release`: Whether to proceed with release |
| 121 | +- `level`: Determined release level |
| 122 | +- `python`: Python release mode |
| 123 | +- `docker`: Docker release mode |
| 124 | +- `docs`: Documentation release mode |
| 125 | + |
| 126 | +#### 5. `rw_build_git-tag_and_create_github-release_v2.yaml` |
| 127 | +Creates Git tags and GitHub releases. |
| 128 | + |
| 129 | +**Inputs:** |
| 130 | +- `version` (string, required): Version to tag and release |
| 131 | +- `checkout-sha` (string, optional): Specific commit SHA to checkout |
| 132 | +- `debug_mode` (boolean, optional): Enable debug mode |
| 133 | + |
| 134 | +**Secrets:** |
| 135 | +- `github_auth_token` (required): GitHub token for creating releases |
| 136 | + |
| 137 | +#### 6. `rw_python_package.yaml` |
| 138 | +Handles Python package operations (build, test, publish). |
| 139 | + |
| 140 | +**Inputs:** |
| 141 | +- `operation` (string, required): Operation (build, test, publish-pypi, publish-testpypi) |
| 142 | +- `python-version` (string, optional): Python version (default: 3.13) |
| 143 | +- `version` (string, optional): Package version for display |
| 144 | +- `checkout-sha` (string, optional): Specific commit SHA to checkout |
| 145 | +- `upload-artifacts` (boolean, optional): Whether to upload artifacts (default: true) |
| 146 | +- `artifact-name` (string, optional): Name for build artifacts |
| 147 | + |
| 148 | +**Usage:** |
| 149 | +```yaml |
| 150 | +jobs: |
| 151 | + release-python: |
| 152 | + uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_python_package.yaml@master |
| 153 | + with: |
| 154 | + operation: 'publish-pypi' |
| 155 | + version: '1.0.0' |
| 156 | + artifact-name: 'my-package' |
| 157 | +``` |
| 158 | + |
| 159 | +### Docker Operations |
| 160 | + |
| 161 | +#### 7. `rw_docker_operations.yaml` |
| 162 | +Comprehensive Docker operations including build, test, push, and security scanning. |
| 163 | + |
| 164 | +**Inputs:** |
| 165 | +- `operation` (string, required): Operation (build, test, push, security-scan) |
| 166 | +- `image-name` (string, optional): Docker image name |
| 167 | +- `registry` (string, optional): Container registry (default: ghcr.io) |
| 168 | +- `version` (string, optional): Image version/tag (default: latest) |
| 169 | +- `platforms` (string, optional): Target platforms (default: linux/amd64,linux/arm64) |
| 170 | +- `enable-sbom` (boolean, optional): Generate SBOM (default: false) |
| 171 | +- `enable-signing` (boolean, optional): Sign with cosign (default: false) |
| 172 | +- `health-check-port` (string, optional): Port for health check (default: 8000) |
| 173 | +- `health-check-path` (string, optional): Health check endpoint (default: /health) |
| 174 | + |
| 175 | +**Secrets:** |
| 176 | +- `dockerhub-user` (optional): DockerHub username |
| 177 | +- `registry-token` (optional): Registry authentication token |
| 178 | + |
| 179 | +**Usage:** |
| 180 | +```yaml |
| 181 | +jobs: |
| 182 | + docker-release: |
| 183 | + uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_docker_operations.yaml@master |
| 184 | + with: |
| 185 | + operation: 'push' |
| 186 | + registry: 'docker.io' |
| 187 | + version: '1.0.0' |
| 188 | + enable-sbom: true |
| 189 | + enable-signing: true |
| 190 | + secrets: |
| 191 | + dockerhub-user: ${{ secrets.DOCKERHUB_USERNAME }} |
| 192 | + registry-token: ${{ secrets.DOCKERHUB_TOKEN }} |
| 193 | +``` |
| 194 | + |
| 195 | +### Documentation |
| 196 | + |
| 197 | +#### 8. `rw_docs_operations.yaml` |
| 198 | +Handles documentation operations including building, versioning, and deployment. |
| 199 | + |
| 200 | +**Inputs:** |
| 201 | +- `operation` (string, required): Operation (build, test, version, deploy-preview) |
| 202 | +- `version` (string, optional): Documentation version |
| 203 | +- `checkout-sha` (string, optional): Specific commit SHA to checkout |
| 204 | +- `sections` (string, optional): JSON array of documentation sections |
| 205 | +- `strategy` (string, optional): Versioning strategy |
| 206 | +- `upload-artifacts` (boolean, optional): Whether to upload artifacts |
| 207 | +- `commit-changes` (boolean, optional): Whether to commit changes |
| 208 | + |
| 209 | +#### 9. `rw_organize_test_cov_reports.yaml` |
| 210 | +Organizes and uploads test coverage reports to CodeCov. |
| 211 | + |
| 212 | +**Inputs:** |
| 213 | +- `test_type` (string, required): Type of test for coverage report |
| 214 | + |
| 215 | +## Quick Start Examples |
| 216 | + |
| 217 | +### Basic CI Workflow |
| 218 | +```yaml |
| 219 | +name: CI |
| 220 | +on: |
| 221 | + push: |
| 222 | + branches: [main] |
| 223 | + pull_request: |
| 224 | + branches: [main] |
| 225 | +
|
| 226 | +jobs: |
| 227 | + test: |
| 228 | + uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_run_all_test_and_record.yaml@master |
| 229 | + secrets: |
| 230 | + codecov_token: ${{ secrets.CODECOV_TOKEN }} |
| 231 | + sonar_token: ${{ secrets.SONAR_TOKEN }} |
| 232 | +``` |
| 233 | + |
| 234 | +### Complete Release Workflow |
| 235 | +```yaml |
| 236 | +name: Release |
| 237 | +on: |
| 238 | + workflow_dispatch: |
| 239 | + inputs: |
| 240 | + level: |
| 241 | + type: choice |
| 242 | + options: [auto, patch, minor, major] |
| 243 | +
|
| 244 | +permissions: |
| 245 | + contents: write |
| 246 | + packages: write |
| 247 | + id-token: write |
| 248 | +
|
| 249 | +jobs: |
| 250 | + intent: |
| 251 | + uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_parse_release_intent.yaml@master |
| 252 | + with: |
| 253 | + level: ${{ inputs.level }} |
| 254 | +
|
| 255 | + release-python: |
| 256 | + uses: Chisanan232/Template-Python-UV-Project/.github/workflows/rw_python_package.yaml@master |
| 257 | + needs: intent |
| 258 | + if: needs.intent.outputs.do_release == 'true' |
| 259 | + with: |
| 260 | + operation: 'publish-pypi' |
| 261 | +``` |
| 262 | + |
| 263 | +## Configuration Requirements |
| 264 | + |
| 265 | +### Required Secrets |
| 266 | +- `CODECOV_TOKEN`: For uploading test coverage to CodeCov |
| 267 | +- `SONAR_TOKEN`: For SonarCloud code quality analysis |
| 268 | +- `DOCKERHUB_USERNAME` & `DOCKERHUB_TOKEN`: For DockerHub releases |
| 269 | +- `GITHUB_TOKEN`: Automatically provided by GitHub Actions |
| 270 | + |
| 271 | +### Project Structure Requirements |
| 272 | +Your project should follow this structure for optimal compatibility: |
| 273 | +``` |
| 274 | +your-project/ |
| 275 | +βββ src/your_package_name/ # Your Python package |
| 276 | +βββ test/ |
| 277 | +β βββ unit_test/ |
| 278 | +β βββ integration_test/ |
| 279 | +β βββ e2e_test/ |
| 280 | +β βββ contract_test/ |
| 281 | +βββ scripts/ |
| 282 | +β βββ run_all_tests.sh |
| 283 | +βββ pyproject.toml |
| 284 | +βββ uv.lock |
| 285 | +βββ .coveragerc |
| 286 | +βββ pytest.ini |
| 287 | +βββ codecov.yml |
| 288 | +``` |
| 289 | + |
| 290 | +### Customization Points |
| 291 | +When using these workflows, remember to update: |
| 292 | +1. Branch names (replace `<your_base_branch>` with your main branch) |
| 293 | +2. Package names (replace `<your_package_name>` with your actual package) |
| 294 | +3. Secret names (replace `<YOUR_*_SECRET>` with your actual secret names) |
| 295 | +4. Path patterns in trigger conditions |
| 296 | + |
| 297 | +## Best Practices |
| 298 | + |
| 299 | +1. **Version Pinning**: Always use `@master` or a specific version tag when calling reusable workflows |
| 300 | +2. **Secret Management**: Use GitHub repository secrets for sensitive data |
| 301 | +3. **Path Filters**: Customize path filters in triggers to match your project structure |
| 302 | +4. **Testing Strategy**: Use `run_e2e: false` for faster CI, enable for comprehensive testing |
| 303 | +5. **Release Automation**: Use the release workflows for consistent versioning and publishing |
| 304 | + |
| 305 | +## Troubleshooting |
| 306 | + |
| 307 | +### Common Issues |
| 308 | + |
| 309 | +1. **Missing Secrets**: Ensure all required secrets are configured in your repository settings |
| 310 | +2. **Path Mismatches**: Update file paths in workflow triggers to match your project structure |
| 311 | +3. **Permission Errors**: Verify that your repository has the correct permissions set |
| 312 | +4. **UV Dependencies**: Ensure your `pyproject.toml` includes the necessary dependency groups |
| 313 | + |
| 314 | +### Getting Help |
| 315 | + |
| 316 | +For issues with the reusable workflows: |
| 317 | +1. Check the workflow run logs for detailed error messages |
| 318 | +2. Verify your project structure matches the expected format |
| 319 | +3. Ensure all required secrets and permissions are configured |
| 320 | +4. Review the example workflows in the `examples/` directory |
0 commit comments