|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*" |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: pypi-${{ github.ref }} |
| 13 | + cancel-in-progress: false |
| 14 | + |
| 15 | +jobs: |
| 16 | + build: |
| 17 | + name: Build distributions |
| 18 | + runs-on: ubuntu-latest |
| 19 | + outputs: |
| 20 | + stable: ${{ steps.version.outputs.stable }} |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v7 |
| 23 | + |
| 24 | + - uses: actions/setup-python@v6 |
| 25 | + with: |
| 26 | + python-version: "3.11" |
| 27 | + |
| 28 | + - name: Validate tag matches package version |
| 29 | + id: version |
| 30 | + env: |
| 31 | + RELEASE_TAG: ${{ github.ref_name }} |
| 32 | + run: | |
| 33 | + package_version="$(python -c 'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["project"]["version"])')" |
| 34 | + if [ "$RELEASE_TAG" != "v$package_version" ]; then |
| 35 | + echo "Tag $RELEASE_TAG does not match package version $package_version." |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | +
|
| 39 | + if [[ "$package_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 40 | + echo "stable=true" >> "$GITHUB_OUTPUT" |
| 41 | + else |
| 42 | + echo "stable=false" >> "$GITHUB_OUTPUT" |
| 43 | + fi |
| 44 | +
|
| 45 | + - name: Install test and build tools |
| 46 | + run: | |
| 47 | + python -m pip install --upgrade -e . |
| 48 | + python -m pip install --upgrade pytest pytest-asyncio build twine |
| 49 | +
|
| 50 | + - name: Run unit tests |
| 51 | + run: python -m pytest tests/ --ignore=tests/test_integration.py -v |
| 52 | + |
| 53 | + - name: Build distributions |
| 54 | + run: python -m build |
| 55 | + |
| 56 | + - name: Check distribution metadata |
| 57 | + run: python -m twine check dist/* |
| 58 | + |
| 59 | + - name: Smoke test wheel |
| 60 | + run: | |
| 61 | + python -m venv /tmp/insforge-wheel-test |
| 62 | + /tmp/insforge-wheel-test/bin/python -m pip install dist/*.whl |
| 63 | + cd /tmp |
| 64 | + /tmp/insforge-wheel-test/bin/python -c 'import insforge; from importlib.metadata import version; print(version("insforge"))' |
| 65 | +
|
| 66 | + - name: Upload distributions |
| 67 | + uses: actions/upload-artifact@v7 |
| 68 | + with: |
| 69 | + name: python-package-distributions |
| 70 | + path: dist/ |
| 71 | + if-no-files-found: error |
| 72 | + retention-days: 1 |
| 73 | + |
| 74 | + publish: |
| 75 | + name: Publish distributions |
| 76 | + needs: build |
| 77 | + runs-on: ubuntu-latest |
| 78 | + permissions: |
| 79 | + id-token: write |
| 80 | + steps: |
| 81 | + - name: Download distributions |
| 82 | + uses: actions/download-artifact@v8 |
| 83 | + with: |
| 84 | + name: python-package-distributions |
| 85 | + path: dist/ |
| 86 | + |
| 87 | + - name: Publish to PyPI |
| 88 | + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0 |
| 89 | + |
| 90 | + release: |
| 91 | + name: Create stable GitHub Release |
| 92 | + if: needs.build.outputs.stable == 'true' |
| 93 | + needs: [build, publish] |
| 94 | + runs-on: ubuntu-latest |
| 95 | + permissions: |
| 96 | + contents: write |
| 97 | + |
| 98 | + steps: |
| 99 | + - name: Create GitHub Release |
| 100 | + env: |
| 101 | + GH_TOKEN: ${{ github.token }} |
| 102 | + RELEASE_TAG: ${{ github.ref_name }} |
| 103 | + REPOSITORY: ${{ github.repository }} |
| 104 | + run: | |
| 105 | + if gh release view "$RELEASE_TAG" --repo "$REPOSITORY" >/dev/null 2>&1; then |
| 106 | + echo "GitHub Release $RELEASE_TAG already exists" |
| 107 | + else |
| 108 | + gh release create "$RELEASE_TAG" --repo "$REPOSITORY" --generate-notes --verify-tag |
| 109 | + fi |
0 commit comments