diff --git a/.github/workflows/pypi_publish.yaml b/.github/workflows/pypi_publish.yaml index 99cbcc1fade2..6439c5f7f19a 100644 --- a/.github/workflows/pypi_publish.yaml +++ b/.github/workflows/pypi_publish.yaml @@ -1,73 +1,47 @@ -# Adapted from https://blog.deepjyoti30.dev/pypi-release-github-action - name: PyPI release on: workflow_dispatch: push: tags: - - "*" + - v* + branches: + - 'v*-release' + +permissions: + contents: read jobs: - find-and-checkout-latest-branch: + build-and-test: runs-on: ubuntu-22.04 - outputs: - latest_branch: ${{ steps.set_latest_branch.outputs.latest_branch }} steps: - - name: Checkout Repo - uses: actions/checkout@v6 + - name: Checkout repo + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Set up Python - uses: actions/setup-python@v6 - with: - python-version: '3.10' - - - name: Fetch latest branch - id: fetch_latest_branch - run: | - pip install -U requests packaging - LATEST_BRANCH=$(python utils/fetch_latest_release_branch.py) - echo "Latest branch: $LATEST_BRANCH" - echo "latest_branch=$LATEST_BRANCH" >> $GITHUB_ENV - - - name: Set latest branch output - id: set_latest_branch - run: echo "::set-output name=latest_branch::${{ env.latest_branch }}" - - release: - needs: find-and-checkout-latest-branch - runs-on: ubuntu-22.04 - - steps: - - name: Checkout Repo - uses: actions/checkout@v6 - with: - ref: ${{ needs.find-and-checkout-latest-branch.outputs.latest_branch }} - - - name: Setup Python - uses: actions/setup-python@v6 + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5 with: python-version: "3.10" - - name: Install dependencies + - name: Install build dependencies run: | python -m pip install --upgrade pip - pip install -U setuptools wheel twine + pip install -U build pip install -U torch --index-url https://download.pytorch.org/whl/cpu - name: Build the dist files - run: python setup.py bdist_wheel && python setup.py sdist + run: python -m build + + - name: Validate dist metadata + run: | + pip install twine + twine check --strict dist/* - - name: Publish to the test PyPI - env: - TWINE_USERNAME: ${{ secrets.TEST_PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.TEST_PYPI_PASSWORD }} - run: twine upload dist/* -r pypitest --repository-url=https://test.pypi.org/legacy/ + - name: Install from built wheel + run: pip install dist/*.whl - name: Test installing diffusers and importing run: | - pip install diffusers && pip uninstall diffusers -y - pip install -i https://test.pypi.org/simple/ diffusers pip install -U transformers python utils/print_env.py python -c "from diffusers import __version__; print(__version__)" @@ -75,8 +49,30 @@ jobs: python -c "from diffusers import DiffusionPipeline; pipe = DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe', safety_checker=None); pipe('ah suh du')" python -c "from diffusers import *" - - name: Publish to PyPI - env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - run: twine upload dist/* -r pypi + - name: Upload build artifacts + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: python-dist + path: dist/ + + publish-to-pypi: + needs: build-and-test + if: startsWith(github.ref, 'refs/tags/') + runs-on: ubuntu-latest + environment: pypi-release + permissions: + id-token: write + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - name: Download build artifacts + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: python-dist + path: dist/ + + - name: Publish package distributions to TestPyPI + uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # release/v1 + with: + verbose: true diff --git a/utils/fetch_latest_release_branch.py b/utils/fetch_latest_release_branch.py deleted file mode 100644 index 5b0be6253e1b..000000000000 --- a/utils/fetch_latest_release_branch.py +++ /dev/null @@ -1,73 +0,0 @@ -# coding=utf-8 -# Copyright 2025 The HuggingFace Team. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -import requests -from packaging.version import parse - - -# GitHub repository details -USER = "huggingface" -REPO = "diffusers" - - -def fetch_all_branches(user, repo): - branches = [] # List to store all branches - page = 1 # Start from first page - while True: - # Make a request to the GitHub API for the branches - response = requests.get( - f"https://api.github.com/repos/{user}/{repo}/branches", - params={"page": page}, - timeout=60, - ) - - # Check if the request was successful - if response.status_code == 200: - # Add the branches from the current page to the list - branches.extend([branch["name"] for branch in response.json()]) - - # Check if there is a 'next' link for pagination - if "next" in response.links: - page += 1 # Move to the next page - else: - break # Exit loop if there is no next page - else: - print("Failed to retrieve branches:", response.status_code) - break - - return branches - - -def main(): - # Fetch all branches - branches = fetch_all_branches(USER, REPO) - - # Filter branches. - # print(f"Total branches: {len(branches)}") - filtered_branches = [] - for branch in branches: - if branch.startswith("v") and ("-release" in branch or "-patch" in branch): - filtered_branches.append(branch) - # print(f"Filtered: {branch}") - - sorted_branches = sorted(filtered_branches, key=lambda x: parse(x.split("-")[0][1:]), reverse=True) - latest_branch = sorted_branches[0] - # print(f"Latest branch: {latest_branch}") - return latest_branch - - -if __name__ == "__main__": - print(main())