|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + # Trigger this workflow when a new tag is pushed |
| 5 | + push: |
| 6 | + tags: |
| 7 | + - '*' |
| 8 | + |
| 9 | +jobs: |
| 10 | + build-and-publish: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + strategy: |
| 14 | + matrix: |
| 15 | + python-version: ["3.9", "3.10", "3.11", "3.12"] |
| 16 | + |
| 17 | + steps: |
| 18 | + # Step 1: Check out the repository |
| 19 | + - name: Check out code |
| 20 | + uses: actions/checkout@v3 |
| 21 | + |
| 22 | + # Step 2: Set up Python environment with matrix |
| 23 | + - name: Set up Python ${{ matrix.python-version }} |
| 24 | + uses: actions/setup-python@v4 |
| 25 | + with: |
| 26 | + python-version: ${{ matrix.python-version }} |
| 27 | + |
| 28 | + # Step 3: Install build dependencies |
| 29 | + - name: Install build dependencies |
| 30 | + run: | |
| 31 | + python -m pip install --upgrade pip |
| 32 | + pip install setuptools wheel twine |
| 33 | +
|
| 34 | + # Step 4: Build the package (wheel and source distribution) |
| 35 | + - name: Build the package |
| 36 | + run: | |
| 37 | + python setup.py sdist bdist_wheel |
| 38 | +
|
| 39 | + # Step 5: Publish the package to PyPI (only run once for one version) |
| 40 | + - name: Publish to PyPI |
| 41 | + if: matrix.python-version == '3.9' # Publish only once, on Python 3.9 |
| 42 | + env: |
| 43 | + PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }} # Use the PyPI token stored in GitHub Secrets |
| 44 | + run: | |
| 45 | + python -m twine upload dist/* |
| 46 | +
|
| 47 | + # Step 6: Clean up the build artifacts |
| 48 | + - name: Remove build artifacts |
| 49 | + if: matrix.python-version == '3.9' # Clean up once after publishing |
| 50 | + run: rm -rf dist build *.egg-info |
0 commit comments