Merge pull request #5 from CocoRoF/main #6
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
| # ────────────────────────────────────────────────────────────── | |
| # PyPI auto-deploy workflow | |
| # Trigger: push to the deploy branch | |
| # Condition: only deploy if not yet published on PyPI, or version bumped | |
| # ────────────────────────────────────────────────────────────── | |
| name: Publish to PyPI | |
| on: | |
| push: | |
| branches: | |
| - deploy | |
| jobs: | |
| # ── Step 1: Version check ───────────────────────────────── | |
| check-version: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| should_publish: ${{ steps.decide.outputs.should_publish }} | |
| local_version: ${{ steps.local.outputs.version }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| - name: Read local version | |
| id: local | |
| run: | | |
| VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])") | |
| echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
| echo "📦 Local version: $VERSION" | |
| - name: Check PyPI for existing version | |
| id: pypi | |
| run: | | |
| LOCAL="${{ steps.local.outputs.version }}" | |
| # PyPI JSON API — returns 404 if the package does not exist | |
| HTTP_CODE=$(curl -s -o /tmp/pypi.json -w "%{http_code}" \ | |
| "https://pypi.org/pypi/f2a/json") | |
| if [ "$HTTP_CODE" = "404" ]; then | |
| echo "pypi_version=NONE" >> "$GITHUB_OUTPUT" | |
| echo "🆕 Package not yet on PyPI" | |
| else | |
| PYPI_VER=$(python -c " | |
| import json, pathlib | |
| data = json.loads(pathlib.Path('/tmp/pypi.json').read_text()) | |
| print(data['info']['version']) | |
| ") | |
| echo "pypi_version=$PYPI_VER" >> "$GITHUB_OUTPUT" | |
| echo "📡 PyPI version: $PYPI_VER" | |
| fi | |
| - name: Decide whether to publish | |
| id: decide | |
| run: | | |
| LOCAL="${{ steps.local.outputs.version }}" | |
| PYPI="${{ steps.pypi.outputs.pypi_version }}" | |
| if [ "$PYPI" = "NONE" ]; then | |
| echo "should_publish=true" >> "$GITHUB_OUTPUT" | |
| echo "✅ First publish — will deploy $LOCAL" | |
| elif [ "$LOCAL" != "$PYPI" ]; then | |
| echo "should_publish=true" >> "$GITHUB_OUTPUT" | |
| echo "✅ Version bumped ($PYPI → $LOCAL) — will deploy" | |
| else | |
| echo "should_publish=false" >> "$GITHUB_OUTPUT" | |
| echo "⏭️ Version $LOCAL already on PyPI — skipping" | |
| fi | |
| # ── Step 2: Tests ───────────────────────────────────────── | |
| test: | |
| needs: check-version | |
| if: needs.check-version.outputs.should_publish == 'true' | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| python-version: ["3.10", "3.11", "3.12"] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python ${{ matrix.python-version }} | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -e ".[dev]" | |
| pip install beautifulsoup4 html5lib | |
| - name: Run tests | |
| run: pytest git_action/tests/ -v --tb=short | |
| # ── Step 3: Build & Deploy ──────────────────────────────── | |
| publish: | |
| needs: [check-version, test] | |
| if: needs.check-version.outputs.should_publish == 'true' | |
| runs-on: ubuntu-latest | |
| environment: pypi | |
| permissions: | |
| id-token: write # Trusted Publisher (OIDC) | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| - name: Install build tools | |
| run: python -m pip install --upgrade pip build | |
| - name: Build package | |
| run: python -m build | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@release/v1 | |
| # Uses Trusted Publisher, so no API token required. | |
| # You must register GitHub Actions as a Trusted Publisher in the PyPI project settings. | |
| # To use a manual token instead, uncomment below: | |
| # with: | |
| # password: ${{ secrets.PYPI_API_TOKEN }} |