diff --git a/.github/workflows/pypi.yml b/.github/workflows/pypi.yml new file mode 100644 index 0000000..7f66f8b --- /dev/null +++ b/.github/workflows/pypi.yml @@ -0,0 +1,84 @@ +name: Publish to PyPI + +on: + push: + branches: [main] + workflow_dispatch: + +concurrency: + group: pypi-${{ github.ref }} + cancel-in-progress: false + +permissions: + contents: read + +jobs: + build: + if: github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + outputs: + publish_needed: ${{ steps.check.outputs.publish_needed }} + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Check if version already on PyPI + id: check + run: | + python - <<'PY' + import os + import tomllib + import urllib.error + import urllib.request + + with open("pyproject.toml", "rb") as source: + project = tomllib.load(source)["project"] + name, version = project["name"], project["version"] + exists = False + try: + with urllib.request.urlopen(f"https://pypi.org/pypi/{name}/{version}/json", timeout=15): + exists = True + except urllib.error.HTTPError as exc: + if exc.code != 404: + raise + + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: + output.write(f"publish_needed={'false' if exists else 'true'}\n") + print(f"{name} {version} {'already exists on PyPI, skipping publish' if exists else 'is not on PyPI'}") + PY + + - name: Build distributions + if: steps.check.outputs.publish_needed == 'true' + run: | + python -m pip install build + python -m build + + - uses: actions/upload-artifact@v4 + if: steps.check.outputs.publish_needed == 'true' + with: + name: dist + path: dist/ + if-no-files-found: error + + publish: + needs: build + if: needs.build.outputs.publish_needed == 'true' + runs-on: ubuntu-latest + environment: pypi + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v4 + with: + name: dist + path: dist/ + + # Configure a trusted publisher at: + # https://pypi.org/manage/project/open-terminal/settings/publishing/ + # Owner: open-webui; repository: open-terminal; + # workflow: pypi.yml; environment: pypi. + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@release/v1