Spaces:
Running
Running
| name: Publish Release | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: write # Ensure write access to push tags | |
| jobs: | |
| pypi: | |
| name: Publish to PyPI | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Checkout the code | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| # Step 2: Set up Python | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.x' | |
| # Step 3: Install dependencies | |
| - name: Install dependencies | |
| run: pip install toml requests | |
| # Step 4: Extract current version from pyproject.toml | |
| - name: Extract current version | |
| id: get_version | |
| run: | | |
| VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])") | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| # Step 5: Get latest version from PyPI | |
| - name: Get latest version from PyPI | |
| id: get_pypi_version | |
| run: | | |
| LATEST_PYPI_VERSION=$(python -c "import toml; import requests; PACKAGE_NAME = toml.load('pyproject.toml')['project']['name']; response = requests.get(f'https://pypi.org/pypi/{PACKAGE_NAME}/json'); print(response.json()['info']['version'])") | |
| echo "LATEST_PYPI_VERSION=$LATEST_PYPI_VERSION" >> $GITHUB_ENV | |
| # Step 6: Compare current version with the latest tag | |
| - name: Check if version is bumped | |
| id: check_version | |
| run: | | |
| if [ "${{ env.VERSION }}" = "${{ env.LATEST_PYPI_VERSION }}" ]; then | |
| echo "Version not bumped. Exiting." | |
| echo "version_bumped=false" >> $GITHUB_ENV | |
| else | |
| echo "Version bumped. Proceeding." | |
| echo "version_bumped=true" >> $GITHUB_ENV | |
| fi | |
| # Step 5: Remove problematic optional dependencies | |
| - name: Strip problematic optional dependencies | |
| run: | | |
| python - <<EOF | |
| import toml | |
| from pathlib import Path | |
| pyproject_path = Path("pyproject.toml") | |
| data = toml.loads(pyproject_path.read_text()) | |
| # Process optional dependencies | |
| optional_deps = data.get("project", {}).get("optional-dependencies", {}) | |
| for key, deps in optional_deps.items(): | |
| new_deps = [] | |
| for dep in deps: | |
| if "@git" in dep: | |
| dep = dep.split("@git")[0].strip() # Remove everything after "@git" | |
| new_deps.append(dep) | |
| optional_deps[key] = new_deps | |
| pyproject_path.write_text(toml.dumps(data)) | |
| EOF | |
| # Step 7: Install Flit (only if version bumped) | |
| - name: Install Flit | |
| if: env.version_bumped == 'true' | |
| run: pip install flit | |
| # Step 8: Create .pypirc file (only if version bumped) | |
| - name: Create .pypirc file | |
| if: env.version_bumped == 'true' | |
| run: | | |
| echo "[pypi]" > ~/.pypirc | |
| echo "username = __token__" >> ~/.pypirc | |
| echo "password = ${{ secrets.PYPI_API_TOKEN }}" >> ~/.pypirc | |
| # Step 9: Build and publish package (only if version bumped) | |
| - name: Build and Publish Package | |
| if: env.version_bumped == 'true' | |
| run: | | |
| flit build | |
| flit publish | |