Skip to content

Latest commit

 

History

History
88 lines (66 loc) · 3.54 KB

python-publish.md

File metadata and controls

88 lines (66 loc) · 3.54 KB

Python Build Workflow

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.


Purpose

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.

Workflow Inputs

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.

Workflow Secrets

Name Required Description
PYPI_API_TOKEN true The API token for publishing to PyPI.

Workflow Jobs

build-publish

This job runs on ubuntu-latest and performs the following steps:

  1. Debug variables: Prints the input variables and environment variables for debugging purposes.
  2. Checkout: Checks out the repository.
  3. Set up Python: Sets up the specified Python version.
  4. Install dependencies: Installs pip, pipx, and Poetry.
  5. Remove dist: Removes the dist directory.
  6. Debug: Configures Poetry to use the test PyPI repository.
  7. AutoPatch version: Automatically increments the version if poetry_version_options is provided and version is not specified.
  8. Set specific version: Sets the specified version if version is provided.
  9. Build package: Builds the package using Poetry with the specified build parameters.
  10. Validate build: Validates the build by listing the contents of the dist directory.
  11. Run tests: Runs pytest tests if pytest_run is true.
  12. Publish to PyPi: Publishes the package to PyPI using the provided API token.
  13. Update Source on GitHub: Updates the source code on GitHub with the new version.

Example Usage

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 }}

Notes

  • 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.