|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + pull_request: |
| 7 | + paths: |
| 8 | + - 'pyproject.toml' |
| 9 | + - 'web_hacker/**' |
| 10 | + - '.github/workflows/publish.yml' |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + dry_run: |
| 14 | + description: 'Dry run (build only, no publish)' |
| 15 | + required: false |
| 16 | + default: false |
| 17 | + type: boolean |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + name: Build distribution |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Set up Python |
| 27 | + uses: actions/setup-python@v5 |
| 28 | + with: |
| 29 | + python-version: '3.12' |
| 30 | + |
| 31 | + - name: Install build dependencies |
| 32 | + run: | |
| 33 | + python -m pip install --upgrade pip |
| 34 | + pip install build hatchling |
| 35 | +
|
| 36 | + - name: Build package |
| 37 | + run: python -m build |
| 38 | + |
| 39 | + - name: Check version not already published |
| 40 | + if: github.event_name == 'release' |
| 41 | + run: | |
| 42 | + VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])") |
| 43 | + echo "Package version: $VERSION" |
| 44 | + |
| 45 | + # Check if version exists on PyPI |
| 46 | + if pip index versions web-hacker 2>/dev/null | grep -q "$VERSION"; then |
| 47 | + echo "::error::Version $VERSION already exists on PyPI! Update version in pyproject.toml" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + echo "✓ Version $VERSION is new and ready to publish" |
| 51 | +
|
| 52 | + - name: List built artifacts |
| 53 | + run: ls -la dist/ |
| 54 | + |
| 55 | + - name: Test local install |
| 56 | + run: | |
| 57 | + pip install dist/*.whl |
| 58 | + python -c "import web_hacker; print('Successfully imported web_hacker')" |
| 59 | +
|
| 60 | + - name: Upload distribution artifacts |
| 61 | + uses: actions/upload-artifact@v4 |
| 62 | + with: |
| 63 | + name: python-package-distributions |
| 64 | + path: dist/ |
| 65 | + |
| 66 | + publish-testpypi: |
| 67 | + name: Publish to TestPyPI |
| 68 | + if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run != 'true') |
| 69 | + needs: build |
| 70 | + runs-on: ubuntu-latest |
| 71 | + environment: |
| 72 | + name: release |
| 73 | + url: https://test.pypi.org/p/web-hacker |
| 74 | + permissions: |
| 75 | + id-token: write |
| 76 | + steps: |
| 77 | + - name: Download distribution artifacts |
| 78 | + uses: actions/download-artifact@v4 |
| 79 | + with: |
| 80 | + name: python-package-distributions |
| 81 | + path: dist/ |
| 82 | + |
| 83 | + - name: Publish to TestPyPI |
| 84 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 85 | + with: |
| 86 | + repository-url: https://test.pypi.org/legacy/ |
| 87 | + |
| 88 | + test-install: |
| 89 | + name: Test install from TestPyPI |
| 90 | + if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run != 'true') |
| 91 | + needs: publish-testpypi |
| 92 | + runs-on: ubuntu-latest |
| 93 | + steps: |
| 94 | + - name: Set up Python |
| 95 | + uses: actions/setup-python@v5 |
| 96 | + with: |
| 97 | + python-version: '3.12' |
| 98 | + |
| 99 | + - name: Wait for TestPyPI to index package |
| 100 | + run: sleep 60 |
| 101 | + |
| 102 | + - name: Test install from TestPyPI |
| 103 | + run: | |
| 104 | + pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ web-hacker |
| 105 | + python -c "import web_hacker; print('Successfully imported web_hacker')" |
| 106 | +
|
| 107 | + publish-pypi: |
| 108 | + name: Publish to PyPI |
| 109 | + if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run != 'true') |
| 110 | + needs: test-install |
| 111 | + runs-on: ubuntu-latest |
| 112 | + environment: |
| 113 | + name: release |
| 114 | + url: https://pypi.org/p/web-hacker |
| 115 | + permissions: |
| 116 | + id-token: write |
| 117 | + steps: |
| 118 | + - name: Download distribution artifacts |
| 119 | + uses: actions/download-artifact@v4 |
| 120 | + with: |
| 121 | + name: python-package-distributions |
| 122 | + path: dist/ |
| 123 | + |
| 124 | + - name: Publish to PyPI |
| 125 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 126 | + |
| 127 | + verify-pypi: |
| 128 | + name: Verify install from PyPI |
| 129 | + if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run != 'true') |
| 130 | + needs: publish-pypi |
| 131 | + runs-on: ubuntu-latest |
| 132 | + steps: |
| 133 | + - name: Set up Python |
| 134 | + uses: actions/setup-python@v5 |
| 135 | + with: |
| 136 | + python-version: '3.12' |
| 137 | + |
| 138 | + - name: Wait for PyPI to index package |
| 139 | + run: sleep 60 |
| 140 | + |
| 141 | + - name: Test install from PyPI |
| 142 | + run: | |
| 143 | + pip install web-hacker |
| 144 | + python -c "import web_hacker; print('Successfully imported web_hacker from PyPI')" |
0 commit comments