Merge branch 'main' of github.com:MemorySlice/memslicer #7
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: CI / Publish | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| permissions: | |
| contents: write # needed to push git tags | |
| id-token: write # needed for OIDC trusted publishing to PyPI | |
| jobs: | |
| # --------------------------------------------------------------------------- | |
| # Job 1: Run the test suite on every PR and every push to main | |
| # --------------------------------------------------------------------------- | |
| test: | |
| name: Test (Python ${{ matrix.python-version }}) | |
| runs-on: ubuntu-latest | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| python-version: ["3.10", "3.11", "3.12"] | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Python ${{ matrix.python-version }} | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| cache: pip | |
| - name: Install package with dev dependencies | |
| run: pip install -e ".[dev]" | |
| - name: Run tests | |
| run: python -m pytest tests/ -x -q | |
| # --------------------------------------------------------------------------- | |
| # Job 2: Detect whether the version in pyproject.toml changed | |
| # Runs only on pushes to main (not PRs). | |
| # --------------------------------------------------------------------------- | |
| check-version: | |
| name: Check for version bump | |
| runs-on: ubuntu-latest | |
| if: > | |
| github.event_name == 'push' && github.ref == 'refs/heads/main' && | |
| !contains(github.event.head_commit.message, 'docs: update CHANGELOG.md for v') | |
| outputs: | |
| version_changed: ${{ steps.version_check.outputs.version_changed }} | |
| new_version: ${{ steps.version_check.outputs.new_version }} | |
| steps: | |
| - name: Checkout repository (with full history) | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 2 # need HEAD and HEAD~1 to diff | |
| - name: Detect version change | |
| id: version_check | |
| run: | | |
| # Extract the version line from the previous commit (HEAD~1). | |
| # Fall back to an empty string when there is no previous commit | |
| # (i.e. the very first push). | |
| PREV_VERSION=$(git diff HEAD~1 -- pyproject.toml \ | |
| | grep '^-.*version\s*=' \ | |
| | grep -v '^\-\-\-' \ | |
| | head -1 \ | |
| | sed 's/^-[[:space:]]*//' \ | |
| | sed 's/version[[:space:]]*=[[:space:]]*//' \ | |
| | tr -d '"'"'"' ' \ | |
| || true) | |
| CURR_VERSION=$(grep '^version\s*=' pyproject.toml \ | |
| | head -1 \ | |
| | sed 's/version[[:space:]]*=[[:space:]]*//' \ | |
| | tr -d '"'"'"' ') | |
| echo "Previous version : '${PREV_VERSION}'" | |
| echo "Current version : '${CURR_VERSION}'" | |
| if [ -n "${CURR_VERSION}" ] && [ "${PREV_VERSION}" != "${CURR_VERSION}" ]; then | |
| echo "version_changed=true" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "version_changed=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| echo "new_version=${CURR_VERSION}" >> "$GITHUB_OUTPUT" | |
| # --------------------------------------------------------------------------- | |
| # Job 3: Build, publish to PyPI, and push a git tag — only when the version | |
| # changed AND the tests passed. | |
| # --------------------------------------------------------------------------- | |
| publish: | |
| name: Build & Publish to PyPI | |
| runs-on: ubuntu-latest | |
| needs: [test, check-version] | |
| # Gate: only run when tests passed AND version actually changed. | |
| if: > | |
| needs.check-version.outputs.version_changed == 'true' && | |
| needs.test.result == 'success' | |
| environment: | |
| name: pypi | |
| url: https://pypi.org/project/memslicer/ | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Python 3.12 | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| cache: pip | |
| - name: Install build dependencies | |
| run: pip install build hatchling | |
| - name: Install package with dev dependencies | |
| run: pip install -e ".[dev]" | |
| - name: Run tests (final gate before publish) | |
| run: python -m pytest tests/ -x -q | |
| - name: Build distribution packages | |
| run: python -m build | |
| - name: Verify dist contents | |
| run: ls -lh dist/ | |
| - name: Publish to PyPI (OIDC trusted publishing) | |
| uses: pypa/gh-action-pypi-publish@release/v1 | |
| # No API token required — relies on id-token: write permission above. | |
| - name: Create and push git tag | |
| env: | |
| NEW_VERSION: ${{ needs.check-version.outputs.new_version }} | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git tag "v${NEW_VERSION}" | |
| git push origin "v${NEW_VERSION}" |