|
1 | 1 | name: Publish to PyPI |
2 | 2 |
|
3 | 3 | permissions: |
4 | | - actions: write |
| 4 | + contents: write # Required for creating releases and tags |
| 5 | + actions: read |
| 6 | + id-token: write # Required for OIDC |
5 | 7 |
|
6 | 8 | on: |
7 | 9 | workflow_run: |
|
12 | 14 | - main |
13 | 15 |
|
14 | 16 | jobs: |
| 17 | + check-version: |
| 18 | + name: Check Version Change |
| 19 | + runs-on: ubuntu-latest |
| 20 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 21 | + outputs: |
| 22 | + version-changed: ${{ steps.version-check.outputs.changed }} |
| 23 | + new-version: ${{ steps.version-check.outputs.version }} |
| 24 | + steps: |
| 25 | + - name: Checkout code |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 2 # Need previous commit for comparison |
| 29 | + |
| 30 | + - name: Check version change |
| 31 | + id: version-check |
| 32 | + run: | |
| 33 | + # Extract current version from pyproject.toml |
| 34 | + current_version=$(grep -Po '(?<=^version = ")[^"]*' pyproject.toml) |
| 35 | + echo "Current version: $current_version" |
| 36 | +
|
| 37 | + # Check if version changed in last commit |
| 38 | + if git diff HEAD~1 HEAD pyproject.toml | grep -q "^[+-]version ="; then |
| 39 | + echo "Version changed in this commit" |
| 40 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 41 | + echo "version=$current_version" >> "$GITHUB_OUTPUT" |
| 42 | + else |
| 43 | + echo "Version unchanged - skipping release" |
| 44 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 45 | + fi |
| 46 | +
|
15 | 47 | pypi-release: |
16 | 48 | name: PyPI Release |
17 | 49 | runs-on: ubuntu-latest |
18 | | - if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 50 | + needs: check-version |
| 51 | + if: ${{ needs.check-version.outputs.version-changed == 'true' }} |
19 | 52 | environment: |
20 | 53 | name: pypi |
21 | 54 | url: https://pypi.org/project/graphomotor/ |
| 55 | + timeout-minutes: 10 |
22 | 56 |
|
23 | 57 | steps: |
24 | | - - uses: actions/checkout@v4 |
| 58 | + - name: Checkout code |
| 59 | + uses: actions/checkout@v4 |
25 | 60 | with: |
26 | | - fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }} |
| 61 | + fetch-depth: 1 # Only need current commit for build |
27 | 62 |
|
28 | | - - uses: actions/setup-python@v5 |
| 63 | + - name: Set up Python |
| 64 | + uses: actions/setup-python@v5 |
29 | 65 | with: |
30 | 66 | python-version-file: pyproject.toml |
31 | 67 |
|
32 | | - - name: Skip release if version unchanged |
33 | | - run: | |
34 | | - version_change=$(git diff -r HEAD^1 pyproject.toml | grep -E "^(\+|-)version =") |
35 | | - if [[ -z "$version_change" ]]; then |
36 | | - gh run cancel ${{ github.run_id }} |
37 | | - gh run watch ${{ github.run_id }} |
38 | | - fi |
39 | | - env: |
40 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
41 | | - |
42 | 68 | - name: Install uv |
43 | 69 | uses: astral-sh/setup-uv@bd01e18f51369d5a26f1651c3cb451d3417e3bba # v6.3.1 |
44 | 70 | with: |
45 | 71 | enable-cache: true |
46 | 72 |
|
47 | | - - name: Build package |
48 | | - run: uv build |
| 73 | + - name: Cache uv dependencies |
| 74 | + uses: actions/cache@v4 |
| 75 | + with: |
| 76 | + path: ~/.cache/uv |
| 77 | + key: ${{ runner.os }}-uv-${{ hashFiles('**/pyproject.toml', '**/uv.lock') }} |
| 78 | + restore-keys: | |
| 79 | + ${{ runner.os }}-uv- |
| 80 | +
|
| 81 | + - name: Validate package metadata |
| 82 | + run: | |
| 83 | + echo "Building version: ${{ needs.check-version.outputs.new-version }}" |
| 84 | + uv build --wheel --sdist |
| 85 | + ls -la dist/ |
| 86 | +
|
| 87 | + # Validate package contents |
| 88 | + uv run python -c " |
| 89 | + import tarfile |
| 90 | + import zipfile |
| 91 | + import os |
| 92 | +
|
| 93 | + # Check sdist contents |
| 94 | + for f in os.listdir('dist'): |
| 95 | + if f.endswith('.tar.gz'): |
| 96 | + with tarfile.open(f'dist/{f}', 'r:gz') as tar: |
| 97 | + print(f'SDist contents ({f}):') |
| 98 | + for member in tar.getnames()[:10]: # Show first 10 files |
| 99 | + print(f' {member}') |
| 100 | + elif f.endswith('.whl'): |
| 101 | + with zipfile.ZipFile(f'dist/{f}', 'r') as zip_file: |
| 102 | + print(f'Wheel contents ({f}):') |
| 103 | + for member in zip_file.namelist()[:10]: # Show first 10 files |
| 104 | + print(f' {member}') |
| 105 | + " |
| 106 | +
|
| 107 | + - name: Upload build artifacts |
| 108 | + uses: actions/upload-artifact@v4 |
| 109 | + with: |
| 110 | + name: python-package-distributions |
| 111 | + path: dist/ |
| 112 | + retention-days: 30 |
49 | 113 |
|
50 | 114 | - name: Publish to PyPI |
51 | 115 | uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4 |
| 116 | + with: |
| 117 | + print-hash: true |
| 118 | + verbose: true |
| 119 | + # OIDC authentication - no API token needed |
| 120 | + |
| 121 | + - name: Create GitHub Release |
| 122 | + env: |
| 123 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + run: | |
| 125 | + gh release create "v${{ needs.check-version.outputs.new-version }}" \ |
| 126 | + --title "Release v${{ needs.check-version.outputs.new-version }}" \ |
| 127 | + --notes "## What's Changed |
| 128 | +
|
| 129 | + Automated release for version ${{ needs.check-version.outputs.new-version }} |
| 130 | +
|
| 131 | + ### Package Information |
| 132 | + - **PyPI**: https://pypi.org/project/graphomotor/${{ needs.check-version.outputs.new-version }}/ |
| 133 | + - **Changelog**: See commit history for detailed changes |
| 134 | +
|
| 135 | + Full package verification and testing completed via CI pipeline." \ |
| 136 | + dist/* |
0 commit comments