|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + # Trigger manually from GitHub Actions tab (optionally bump version) |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + version: |
| 8 | + description: "New version to publish (e.g. 0.2.0). Leave blank to use current." |
| 9 | + required: false |
| 10 | + default: "" |
| 11 | + dry_run: |
| 12 | + description: "Dry run — build but don't publish" |
| 13 | + required: false |
| 14 | + type: boolean |
| 15 | + default: false |
| 16 | + |
| 17 | + # Auto-publish when a GitHub Release is created |
| 18 | + release: |
| 19 | + types: [published] |
| 20 | + |
| 21 | +permissions: |
| 22 | + contents: write |
| 23 | + id-token: write # Required for OIDC trusted publisher (no API token needed!) |
| 24 | + |
| 25 | +jobs: |
| 26 | + # ────────────────────────────────────────────────────────────────── |
| 27 | + # 1. Build wheel + sdist |
| 28 | + # ────────────────────────────────────────────────────────────────── |
| 29 | + build: |
| 30 | + name: Build distribution packages |
| 31 | + runs-on: ubuntu-latest |
| 32 | + |
| 33 | + outputs: |
| 34 | + version: ${{ steps.get_version.outputs.version }} |
| 35 | + |
| 36 | + steps: |
| 37 | + - name: Checkout code |
| 38 | + uses: actions/checkout@v4 |
| 39 | + with: |
| 40 | + fetch-depth: 0 |
| 41 | + |
| 42 | + - name: Set up Python 3.12 |
| 43 | + uses: actions/setup-python@v5 |
| 44 | + with: |
| 45 | + python-version: "3.12" |
| 46 | + |
| 47 | + - name: Bump version (manual trigger with version input) |
| 48 | + if: github.event_name == 'workflow_dispatch' && github.event.inputs.version != '' |
| 49 | + env: |
| 50 | + NEW_VERSION: ${{ github.event.inputs.version }} |
| 51 | + run: | |
| 52 | + echo "Bumping version to $NEW_VERSION" |
| 53 | + sed -i "s/^version = .*/version = \"$NEW_VERSION\"/" pyproject.toml |
| 54 | + sed -i "s/__version__ = .*/__version__ = \"$NEW_VERSION\"/" git_diff/__init__.py |
| 55 | + git config user.name "github-actions[bot]" |
| 56 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 57 | + git add pyproject.toml git_diff/__init__.py |
| 58 | + git commit -m "chore: release v$NEW_VERSION [skip ci]" |
| 59 | + git tag "v$NEW_VERSION" |
| 60 | + git push origin HEAD --tags |
| 61 | +
|
| 62 | + - name: Get version |
| 63 | + id: get_version |
| 64 | + run: | |
| 65 | + VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])" 2>/dev/null || \ |
| 66 | + python -c "import tomli; print(tomli.load(open('pyproject.toml','rb'))['project']['version'])" 2>/dev/null || \ |
| 67 | + grep '^version' pyproject.toml | head -1 | sed 's/version = "\(.*\)"/\1/') |
| 68 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 69 | + echo "Building version: $VERSION" |
| 70 | +
|
| 71 | + - name: Install build tools |
| 72 | + run: python -m pip install --upgrade pip hatchling build |
| 73 | + |
| 74 | + - name: Build wheel and sdist |
| 75 | + run: python -m build |
| 76 | + |
| 77 | + - name: Verify build |
| 78 | + run: | |
| 79 | + ls -lh dist/ |
| 80 | + python -m pip install dist/*.whl |
| 81 | + git-diff --version |
| 82 | + python -c "import git_diff; print('Import OK:', git_diff.__version__)" |
| 83 | +
|
| 84 | + - name: Upload artifacts |
| 85 | + uses: actions/upload-artifact@v4 |
| 86 | + with: |
| 87 | + name: dist-packages |
| 88 | + path: dist/ |
| 89 | + retention-days: 7 |
| 90 | + |
| 91 | + # ────────────────────────────────────────────────────────────────── |
| 92 | + # 2. Publish to PyPI (skipped on dry run) |
| 93 | + # ────────────────────────────────────────────────────────────────── |
| 94 | + publish: |
| 95 | + name: Publish to PyPI |
| 96 | + needs: build |
| 97 | + runs-on: ubuntu-latest |
| 98 | + if: ${{ github.event.inputs.dry_run != 'true' }} |
| 99 | + |
| 100 | + environment: |
| 101 | + name: pypi |
| 102 | + url: https://pypi.org/p/git-diff |
| 103 | + |
| 104 | + permissions: |
| 105 | + id-token: write # OIDC — no username/password/token needed |
| 106 | + |
| 107 | + steps: |
| 108 | + - name: Download artifacts |
| 109 | + uses: actions/download-artifact@v4 |
| 110 | + with: |
| 111 | + name: dist-packages |
| 112 | + path: dist/ |
| 113 | + |
| 114 | + - name: Publish to PyPI via trusted publisher |
| 115 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 116 | + # Zero config needed when using OIDC trusted publisher |
| 117 | + # No username, no password, no API token — just works ✅ |
0 commit comments