|
| 1 | +name: Publish to PyPI on version change |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - pyproject.toml |
| 9 | + |
| 10 | +jobs: |
| 11 | + publish: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + environment: |
| 14 | + name: pypi |
| 15 | + url: https://pypi.org/p/threewtoolkit |
| 16 | + permissions: |
| 17 | + id-token: write |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Check out main branch |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 2 |
| 24 | + |
| 25 | + - name: Check if project version changed |
| 26 | + id: version_changed |
| 27 | + run: | |
| 28 | + OLD_VERSION=$(git show HEAD^:pyproject.toml | sed -n 's/^version = "\(.*\)"/\1/p' | head -n 1 || true) |
| 29 | + NEW_VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' pyproject.toml | head -n 1) |
| 30 | +
|
| 31 | + echo "Old version: $OLD_VERSION" |
| 32 | + echo "New version: $NEW_VERSION" |
| 33 | +
|
| 34 | + if [ -z "$NEW_VERSION" ]; then |
| 35 | + echo "Could not find version line in pyproject.toml" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | +
|
| 39 | + if [ "$OLD_VERSION" = "$NEW_VERSION" ]; then |
| 40 | + echo "Version did not change. Skipping publish." |
| 41 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 42 | + else |
| 43 | + echo "Version changed. Continuing publish." |
| 44 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 45 | + fi |
| 46 | +
|
| 47 | + - name: Validate package version |
| 48 | + if: steps.version_changed.outputs.changed == 'true' |
| 49 | + run: | |
| 50 | + python3 <<'PY' |
| 51 | + import re |
| 52 | + import sys |
| 53 | + from pathlib import Path |
| 54 | + from packaging.version import Version |
| 55 | +
|
| 56 | + text = Path("pyproject.toml").read_text() |
| 57 | +
|
| 58 | + match = re.search(r'(?m)^version = "([^"]+)"', text) |
| 59 | + if not match: |
| 60 | + sys.exit('Could not find a line starting with: version = "..." in pyproject.toml') |
| 61 | +
|
| 62 | + version = match.group(1) |
| 63 | +
|
| 64 | + try: |
| 65 | + Version(version) |
| 66 | + except Exception as exc: |
| 67 | + sys.exit(f"Invalid PEP 440 version {version!r}: {exc}") |
| 68 | +
|
| 69 | + print(f"Publishing version: {version!r}") |
| 70 | + PY |
| 71 | +
|
| 72 | + - name: Set up Python |
| 73 | + if: steps.version_changed.outputs.changed == 'true' |
| 74 | + uses: actions/setup-python@v5 |
| 75 | + with: |
| 76 | + python-version: "3.11" |
| 77 | + |
| 78 | + - name: Install build tools |
| 79 | + if: steps.version_changed.outputs.changed == 'true' |
| 80 | + run: python -m pip install --upgrade pip build packaging |
| 81 | + |
| 82 | + - name: Build distribution |
| 83 | + if: steps.version_changed.outputs.changed == 'true' |
| 84 | + run: python -m build |
| 85 | + |
| 86 | + - name: Publish to PyPI |
| 87 | + if: steps.version_changed.outputs.changed == 'true' |
| 88 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 89 | + with: |
| 90 | + password: ${{ secrets.PYPI_API_TOKEN }} |
0 commit comments