|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: 'Version to release (e.g. 0.1.0)' |
| 8 | + required: true |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + id-token: read |
| 13 | + packages: write |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: release |
| 17 | + cancel-in-progress: false |
| 18 | + |
| 19 | +jobs: |
| 20 | + build-and-testpypi: |
| 21 | + name: Build package and publish to TestPyPI |
| 22 | + runs-on: ubuntu-latest |
| 23 | + environment: |
| 24 | + name: testpypi-release |
| 25 | + outputs: |
| 26 | + version: ${{ steps.validate-version.outputs.version }} |
| 27 | + steps: |
| 28 | + - name: Check out repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + fetch-depth: 0 |
| 32 | + |
| 33 | + - name: Set up Python 3.11 |
| 34 | + uses: actions/setup-python@v5 |
| 35 | + with: |
| 36 | + python-version: '3.11' |
| 37 | + |
| 38 | + - name: Validate version input |
| 39 | + id: validate-version |
| 40 | + run: | |
| 41 | + VERSION_INPUT="${{ inputs.version }}" |
| 42 | + VERSION_FILE=$(python - <<'PY' |
| 43 | +import pathlib, re |
| 44 | +text = pathlib.Path('setup.py').read_text() |
| 45 | +match = re.search(r'version\s*=\s*[\"\']([^\"\']+)[\"\']', text) |
| 46 | +if not match: |
| 47 | + raise SystemExit('Could not find version in setup.py') |
| 48 | +print(match.group(1)) |
| 49 | +PY |
| 50 | +) |
| 51 | + echo "Detected version: $VERSION_FILE" |
| 52 | + if [ "$VERSION_INPUT" != "$VERSION_FILE" ]; then |
| 53 | + echo "::error::Input version '$VERSION_INPUT' does not match setup.py version '$VERSION_FILE'" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + echo "version=$VERSION_FILE" >> "$GITHUB_OUTPUT" |
| 57 | + |
| 58 | + - name: Install build dependencies |
| 59 | + run: | |
| 60 | + python -m pip install --upgrade pip |
| 61 | + pip install build twine |
| 62 | +
|
| 63 | + - name: Build distribution artifacts |
| 64 | + run: | |
| 65 | + python -m build |
| 66 | + twine check dist/* |
| 67 | +
|
| 68 | + - name: Publish to TestPyPI |
| 69 | + uses: pypa/gh-action-pypi-publish@v1.10.0 |
| 70 | + with: |
| 71 | + repository-url: https://test.pypi.org/legacy/ |
| 72 | + packages-dir: dist |
| 73 | + user: __token__ |
| 74 | + password: ${{ secrets.TEST_PYPI_API_TOKEN }} |
| 75 | + |
| 76 | + - name: Smoke test install from TestPyPI |
| 77 | + env: |
| 78 | + PACKAGE_VERSION: ${{ steps.validate-version.outputs.version }} |
| 79 | + run: | |
| 80 | + python -m venv /tmp/testpypi-venv |
| 81 | + source /tmp/testpypi-venv/bin/activate |
| 82 | + pip install --upgrade pip |
| 83 | + pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple "pnpl==$PACKAGE_VERSION" |
| 84 | +
|
| 85 | + - name: Upload built artifacts |
| 86 | + uses: actions/upload-artifact@v4 |
| 87 | + with: |
| 88 | + name: pnpl-dist-${{ steps.validate-version.outputs.version }} |
| 89 | + path: dist/* |
| 90 | + if-no-files-found: error |
| 91 | + |
| 92 | + publish-production: |
| 93 | + name: Tag release, publish to PyPI, and create GitHub release |
| 94 | + needs: build-and-testpypi |
| 95 | + runs-on: ubuntu-latest |
| 96 | + environment: |
| 97 | + name: pypi-release |
| 98 | + steps: |
| 99 | + - name: Check out repository |
| 100 | + uses: actions/checkout@v4 |
| 101 | + with: |
| 102 | + fetch-depth: 0 |
| 103 | + |
| 104 | + - name: Configure Git |
| 105 | + run: | |
| 106 | + git config user.name "github-actions[bot]" |
| 107 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 108 | +
|
| 109 | + - name: Create and push tag |
| 110 | + env: |
| 111 | + VERSION: ${{ needs.build-and-testpypi.outputs.version }} |
| 112 | + run: | |
| 113 | + if git rev-parse "v$VERSION" >/dev/null 2>&1; then |
| 114 | + echo "::error::Tag v$VERSION already exists" |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | + git tag "v$VERSION" "$GITHUB_SHA" |
| 118 | + git push origin "v$VERSION" |
| 119 | +
|
| 120 | + - name: Download built artifacts |
| 121 | + uses: actions/download-artifact@v4 |
| 122 | + with: |
| 123 | + name: pnpl-dist-${{ needs.build-and-testpypi.outputs.version }} |
| 124 | + path: dist |
| 125 | + |
| 126 | + - name: Publish to PyPI |
| 127 | + uses: pypa/gh-action-pypi-publish@v1.10.0 |
| 128 | + with: |
| 129 | + packages-dir: dist |
| 130 | + user: __token__ |
| 131 | + password: ${{ secrets.PYPI_API_TOKEN }} |
| 132 | + |
| 133 | + - name: Create GitHub release |
| 134 | + uses: ncipollo/release-action@v1 |
| 135 | + with: |
| 136 | + tag: v${{ needs.build-and-testpypi.outputs.version }} |
| 137 | + name: v${{ needs.build-and-testpypi.outputs.version }} |
| 138 | + artifacts: "dist/*" |
| 139 | + generateReleaseNotes: true |
| 140 | + draft: false |
0 commit comments