projectsyn/commodore

View on GitHub
.github/workflows/publish-pypi.yml

Summary

Maintainability
Test Coverage
name: PyPI release

on:
  push:
    branches:
    - master
    tags:
    - v*
  pull_request:
    branches:
    - master

jobs:
  build-and-publish:
    # Skip job on forks
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false
    name: Build and publish to PyPI
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - uses: Gr1N/setup-poetry@v9
      - uses: actions/cache@v4
        with:
          path: ~/.cache/pypoetry/virtualenvs
          key: ${{ runner.os }}-publish-pypi-${{ hashFiles('poetry.lock') }}
      - name: Setup poetry environment
        run: |
          poetry install
      - name: Inject package versions to action env
        run: make inject-version
      - name: Set Python package version for release
        if: startsWith(github.ref, 'refs/tags/v')
        run: poetry version "${PYVERSION}"
      - name: Set Python package version
        if: "!startsWith(github.ref, 'refs/tags/v')"
        run: |
          poetry version "${PYVERSION}.dev${GITHUB_RUN_NUMBER}"
      - name: Build Python package
        run: poetry build
      - name: Publish to PyPI
        if: startsWith(github.ref, 'refs/tags/v')
        env:
          # increase connection timeout to PyPI to 60s
          # NOTE(sg): This is probably not required
          POETRY_REQUESTS_TIMEOUT: "60"
        run: poetry publish -u __token__ -p ${{ secrets.PYPI_TOKEN }}
      - name: Publish to TestPyPI
        if: "!startsWith(github.ref, 'refs/tags/v')"
        env:
          # increase connection timeout to TestPyPI to 60s
          POETRY_REQUESTS_TIMEOUT: "60"
        run: |
          poetry config repositories.test-pypi https://test.pypi.org/legacy/
          poetry publish -r test-pypi -u __token__ -p ${{ secrets.TEST_PYPI_TOKEN }}