This documentation describes the functionality, inputs, secrets, and usage of the Python Build reusable workflow. It provides automation for building, testing, and publishing Python packages using Poetry and pytest.
The Python Build workflow enables seamless CI/CD for Python projects. It supports:
- Environment setup with a specific Python version.
- Version management (manual or automatic increments).
- Package building using Poetry.
- Optional pytest execution for testing.
- Publishing to PyPI or a test repository.
- Committing changes to GitHub with the updated version.
Name | Type | Required | Default | Description |
---|---|---|---|---|
version |
string | false | The specific version to set for the package. | |
poetry_version_options |
string | false | patch | The version increment option for Poetry. |
python-version |
string | false | 3.11 | The Python version to use. |
poetry_build_params |
string | false | Additional parameters for the Poetry build. | |
pytest_run |
boolean | true | Whether to run pytest tests. | |
pytest_params |
string | false | Additional parameters for pytest. |
Name | Required | Description |
---|---|---|
PYPI_API_TOKEN |
true | The API token for publishing to PyPI. |
This job runs on ubuntu-latest
and performs the following steps:
- Debug variables: Prints the input variables and environment variables for debugging purposes.
- Checkout: Checks out the repository.
- Set up Python: Sets up the specified Python version.
- Install dependencies: Installs pip, pipx, and Poetry.
- Remove dist: Removes the
dist
directory. - Debug: Configures Poetry to use the test PyPI repository.
- AutoPatch version: Automatically increments the version if
poetry_version_options
is provided andversion
is not specified. - Set specific version: Sets the specified version if
version
is provided. - Build package: Builds the package using Poetry with the specified build parameters.
- Validate build: Validates the build by listing the contents of the
dist
directory. - Run tests: Runs pytest tests if
pytest_run
is true. - Publish to PyPi: Publishes the package to PyPI using the provided API token.
- Update Source on GitHub: Updates the source code on GitHub with the new version.
name: Publish Python Package
on:
push:
branches:
- main
jobs:
publish:
uses: ./.github/workflows/python-build.yml
with:
version: '1.0.0'
poetry_version_options: 'minor'
python-version: '3.11'
poetry_build_params: '--format wheel'
pytest_run: true
pytest_params: '--maxfail=1 --disable-warnings'
secrets:
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
- Ensure that the PYPI_API_TOKEN secret is added to your repository settings.
- Adjust the python-version, poetry_version_options, and other inputs as needed for your project.
- The pytest_run input must be set to true or false to indicate whether to run tests.