From 091e43aac03a51103fea3da3647facdd47930aaf Mon Sep 17 00:00:00 2001 From: zjowowen Date: Thu, 31 Oct 2024 20:42:16 +0800 Subject: [PATCH] Polish workflows. --- .github/workflows/release.yml | 114 ++++++++++++++++++++--------- .github/workflows/test_release.yml | 57 +++++++++++++++ 2 files changed, 138 insertions(+), 33 deletions(-) create mode 100644 .github/workflows/test_release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e471193..437824a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,47 +4,95 @@ on: # Trigger this workflow when a new tag is pushed push: tags: - - '*' + - 'v*' jobs: - build-and-publish: + build: + name: Build distribution 📦 runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.9", "3.10", "3.11", "3.12"] - steps: - # Step 1: Check out the repository - - name: Check out code - uses: actions/checkout@v3 + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.x" + - name: Install pypa/build + run: >- + python3 -m + pip install + build + --user + - name: Build a binary wheel and a source tarball + run: python3 -m build + - name: Store the distribution packages + uses: actions/upload-artifact@v4 + with: + name: python-package-distributions + path: dist/ - # Step 2: Set up Python environment with matrix - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + publish-to-pypi: + name: >- + Publish Python 🐍 distribution 📦 to PyPI + if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes + needs: + - build + runs-on: ubuntu-latest + environment: + name: pypi + url: https://pypi.org/p/GenerativeRL # Replace with your PyPI project name + permissions: + id-token: write # IMPORTANT: mandatory for trusted publishing + steps: + - name: Download all the dists + uses: actions/download-artifact@v4 + with: + name: python-package-distributions + path: dist/ + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 with: - python-version: ${{ matrix.python-version }} + password: ${{ secrets.PYPI_API_TOKEN }} - # Step 3: Install build dependencies - - name: Install build dependencies - run: | - python -m pip install --upgrade pip - pip install setuptools wheel twine + github-release: + name: >- + Sign the Python 🐍 distribution 📦 with Sigstore + and upload them to GitHub Release + needs: + - publish-to-pypi + runs-on: ubuntu-latest - # Step 4: Build the package (wheel and source distribution) - - name: Build the package - run: | - python setup.py sdist bdist_wheel + permissions: + contents: write # IMPORTANT: mandatory for making GitHub Releases + id-token: write # IMPORTANT: mandatory for sigstore - # Step 5: Publish the package to PyPI (only run once for one version) - - name: Publish to PyPI - if: matrix.python-version == '3.9' # Publish only once, on Python 3.9 + steps: + - name: Download all the dists + uses: actions/download-artifact@v4 + with: + name: python-package-distributions + path: dist/ + - name: Sign the dists with Sigstore + uses: sigstore/gh-action-sigstore-python@v3.0.0 + with: + inputs: >- + ./dist/*.tar.gz + ./dist/*.whl + - name: Create GitHub Release env: - PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }} # Use the PyPI token stored in GitHub Secrets - run: | - python -m twine upload dist/* - - # Step 6: Clean up the build artifacts - - name: Remove build artifacts - if: matrix.python-version == '3.9' # Clean up once after publishing - run: rm -rf dist build *.egg-info + GITHUB_TOKEN: ${{ github.token }} + run: >- + gh release create + '${{ github.ref_name }}' + --repo '${{ github.repository }}' + --notes "" + - name: Upload artifact signatures to GitHub Release + env: + GITHUB_TOKEN: ${{ github.token }} + # Upload to GitHub Release using the `gh` CLI. + # `dist/` contains the built packages, and the + # sigstore-produced signatures and certificates. + run: >- + gh release upload + '${{ github.ref_name }}' dist/** + --repo '${{ github.repository }}' diff --git a/.github/workflows/test_release.yml b/.github/workflows/test_release.yml new file mode 100644 index 0000000..011ef73 --- /dev/null +++ b/.github/workflows/test_release.yml @@ -0,0 +1,57 @@ +name: Publish to TestPyPI + +on: + # Trigger this workflow when a new tag is pushed + push: + tags: + - 'test*' + +jobs: + build: + name: Build distribution 📦 + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.x" + - name: Install pypa/build + run: >- + python3 -m + pip install + build + --user + - name: Build a binary wheel and a source tarball + run: python3 -m build + - name: Store the distribution packages + uses: actions/upload-artifact@v4 + with: + name: python-package-distributions + path: dist/ + + publish-to-testpypi: + name: Publish Python 🐍 distribution 📦 to TestPyPI + needs: + - build + runs-on: ubuntu-latest + + environment: + name: testpypi + url: https://test.pypi.org/p/GenerativeRL + + permissions: + id-token: write # IMPORTANT: mandatory for trusted publishing + + steps: + - name: Download all the dists + uses: actions/download-artifact@v4 + with: + name: python-package-distributions + path: dist/ + - name: Publish distribution 📦 to TestPyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + repository-url: https://test.pypi.org/legacy/ + password: ${{ secrets.PYPI_API_TOKEN }} \ No newline at end of file