ci: publish to PyPI on merge if version bumped #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish to PyPI | |
| on: | |
| push: | |
| branches: | |
| - main | |
| release: | |
| types: [published] | |
| jobs: | |
| check-version: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| version-changed: ${{ steps.check.outputs.changed }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check if version was bumped | |
| id: check | |
| run: | | |
| CURRENT_VERSION=$(grep '^version = ' pyproject.toml | grep -oE '[0-9]+\.[0-9]+\.[0-9]+') | |
| LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
| if [ -z "$LAST_TAG" ]; then | |
| echo "No previous tags found, version changed" | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| else | |
| LAST_VERSION=${LAST_TAG#v} | |
| if [ "$CURRENT_VERSION" != "$LAST_VERSION" ]; then | |
| echo "Version changed from $LAST_VERSION to $CURRENT_VERSION" | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Version unchanged at $CURRENT_VERSION" | |
| echo "changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| fi | |
| test: | |
| if: github.event_name == 'release' || (github.event_name == 'push' && needs.check-version.outputs.version-changed == 'true') | |
| needs: check-version | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| - name: Install uv | |
| uses: astral-sh/setup-uv@v7 | |
| with: | |
| enable-cache: true | |
| cache-dependency-glob: | | |
| **/uv.lock | |
| **/pyproject.toml | |
| - name: Install dependencies | |
| run: uv sync --locked --python-preference=system --group dev | |
| - name: Run tests | |
| run: uv run pytest | |
| publish: | |
| needs: [check-version, test] | |
| if: github.event_name == 'release' || (github.event_name == 'push' && needs.check-version.outputs.version-changed == 'true') | |
| runs-on: ubuntu-latest | |
| environment: release | |
| permissions: | |
| id-token: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| - name: Install uv | |
| uses: astral-sh/setup-uv@v7 | |
| with: | |
| enable-cache: true | |
| cache-dependency-glob: | | |
| **/uv.lock | |
| **/pyproject.toml | |
| - name: Build distribution | |
| run: uv build | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@release/v1 |