3.2.2 #5
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 | |
| paths: | |
| - pyproject.toml | |
| jobs: | |
| pypi-publish: | |
| name: Build and publish to PyPI | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: pypi | |
| url: https://pypi.org/p/nd2py | |
| permissions: | |
| id-token: write # 这是 Trusted Publisher 必须的权限 | |
| contents: read | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| - name: Check whether package version changed | |
| id: version | |
| env: | |
| BEFORE_SHA: ${{ github.event.before }} | |
| run: | | |
| python - <<'PY' | |
| import os | |
| import subprocess | |
| import tomllib | |
| from pathlib import Path | |
| def project_version(text: str) -> str: | |
| data = tomllib.loads(text) | |
| return data["project"]["version"] | |
| new_version = project_version(Path("pyproject.toml").read_text()) | |
| before_sha = os.environ["BEFORE_SHA"] | |
| old_version = None | |
| if before_sha and before_sha != "0000000000000000000000000000000000000000": | |
| try: | |
| old_text = subprocess.check_output( | |
| ["git", "show", f"{before_sha}:pyproject.toml"], | |
| text=True, | |
| ) | |
| except subprocess.CalledProcessError: | |
| old_text = None | |
| if old_text is not None: | |
| old_version = project_version(old_text) | |
| should_publish = old_version != new_version | |
| with open(os.environ["GITHUB_OUTPUT"], "a") as output: | |
| output.write(f"old_version={old_version or ''}\n") | |
| output.write(f"new_version={new_version}\n") | |
| output.write(f"should_publish={str(should_publish).lower()}\n") | |
| if should_publish: | |
| print(f"Publishing nd2py {new_version} (previous version: {old_version or 'none'})") | |
| else: | |
| print(f"pyproject.toml changed, but project.version is still {new_version}; skipping publish") | |
| PY | |
| - name: Install build tool | |
| if: steps.version.outputs.should_publish == 'true' | |
| run: python -m pip install --upgrade build | |
| - name: Build a binary wheel and a source tarball | |
| if: steps.version.outputs.should_publish == 'true' | |
| run: python -m build | |
| - name: Publish package distributions to PyPI | |
| if: steps.version.outputs.should_publish == 'true' | |
| uses: pypa/gh-action-pypi-publish@release/v1 |