|
| 1 | +# This workflow is adapted from: |
| 2 | +# https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/ |
| 3 | + |
| 4 | +name: PyPI Release |
| 5 | +on: |
| 6 | + push: |
| 7 | + tags: |
| 8 | + # Trigger on version tags (e.g., v1.0.0) |
| 9 | + - "v[0-9].[0-9].[0-9]*" |
| 10 | + - "[0-9].[0-9].[0-9]*" |
| 11 | + # Trigger on pre-release tags (e.g., v1.0.0-alpha.1) |
| 12 | + - "v[0-9].[0-9].[0-9]*-*" |
| 13 | + - "[0-9].[0-9].[0-9]*-*" |
| 14 | + |
| 15 | +env: |
| 16 | + # The name of the package to be published to PyPI and TestPyPI. |
| 17 | + PYPI_NAME: qc-PyCI |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + name: Build distribution |
| 22 | + runs-on: ${{ matrix.os }} |
| 23 | + strategy: |
| 24 | + matrix: |
| 25 | + # os: [ubuntu-latest, macos-latest] |
| 26 | + # python-version: ["3.9", "3.10", "3.11", "3.12"] |
| 27 | + os: [ubuntu-latest] |
| 28 | + python-version: ["3.12"] |
| 29 | + |
| 30 | + steps: |
| 31 | + # # TODO: Add C++ compliers |
| 32 | + # # Fetch CUDA toolkit using Jimver/cuda-toolkit |
| 33 | + # - name: Fetch CUDA toolkit |
| 34 | + # uses: Jimver/cuda-toolkit@v0.2.18 |
| 35 | + # id: cuda-toolkit |
| 36 | + # with: |
| 37 | + # cuda: "12.1.0" |
| 38 | + |
| 39 | + # - name: Check nvcc version |
| 40 | + # run: nvcc -V |
| 41 | + |
| 42 | + - uses: actions/checkout@v4 |
| 43 | + with: |
| 44 | + fetch-depth: 0 |
| 45 | + |
| 46 | + - name: Set up Python |
| 47 | + uses: actions/setup-python@v5 |
| 48 | + # with: |
| 49 | + # python-version: "3.12" |
| 50 | + with: |
| 51 | + python-version: ${{ matrix.python-version }} |
| 52 | + |
| 53 | + - name: Install system dependencies (Ubuntu) |
| 54 | + if: runner.os == 'Linux' |
| 55 | + run: | |
| 56 | + sudo apt-get update |
| 57 | + sudo apt-get install -y make gcc g++ python3-dev |
| 58 | +
|
| 59 | + - name: Install system dependencies (macOS) |
| 60 | + if: runner.os == 'macOS' |
| 61 | + run: | |
| 62 | + brew install gcc make |
| 63 | +
|
| 64 | + - name: Install development and distributions version |
| 65 | + run: | |
| 66 | + pip install --upgrade pip |
| 67 | + pip install -v . |
| 68 | + python -m pip install build setuptools wheel twine pytest |
| 69 | +
|
| 70 | + - name: Build Source Distribution |
| 71 | + run: | |
| 72 | + make |
| 73 | + make test |
| 74 | + python -m build --sdist |
| 75 | +
|
| 76 | + - name: Store the distribution packages |
| 77 | + uses: actions/upload-artifact@v4 |
| 78 | + with: |
| 79 | + name: python-package-distributions |
| 80 | + path: dist/ |
| 81 | + |
| 82 | + publish-to-pypi: |
| 83 | + name: Publish Python distribution to PyPI |
| 84 | + # only publish to PyPI on tag pushes |
| 85 | + if: startsWith(github.ref, 'refs/tags/') |
| 86 | + needs: |
| 87 | + - build |
| 88 | + runs-on: ubuntu-latest |
| 89 | + environment: |
| 90 | + name: PyPI-Release |
| 91 | + url: https://pypi.org/project/${{ env.PYPI_NAME }} |
| 92 | + permissions: |
| 93 | + id-token: write |
| 94 | + |
| 95 | + steps: |
| 96 | + - name: Download all the dists |
| 97 | + uses: actions/download-artifact@v4 |
| 98 | + with: |
| 99 | + name: python-package-distributions |
| 100 | + path: dist/ |
| 101 | + - name: Publish distribution to PyPI |
| 102 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 103 | + env: |
| 104 | + TWINE_USERNAME: "__token__" |
| 105 | + TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} |
| 106 | + |
| 107 | + github-release: |
| 108 | + name: Sign the Python distribution with Sigstore and upload them to GitHub Release |
| 109 | + needs: |
| 110 | + - publish-to-pypi |
| 111 | + runs-on: ubuntu-latest |
| 112 | + |
| 113 | + permissions: |
| 114 | + contents: write |
| 115 | + id-token: write |
| 116 | + |
| 117 | + steps: |
| 118 | + - name: Download all the dists |
| 119 | + uses: actions/download-artifact@v4 |
| 120 | + with: |
| 121 | + name: python-package-distributions |
| 122 | + path: dist/ |
| 123 | + - name: Sign the dists with Sigstore |
| 124 | + uses: sigstore/gh-action-sigstore-python@v3.0.0 |
| 125 | + with: |
| 126 | + inputs: >- |
| 127 | + ./dist/*.tar.gz |
| 128 | +
|
| 129 | + - name: Create GitHub Release |
| 130 | + env: |
| 131 | + GITHUB_TOKEN: ${{ github.token }} |
| 132 | + run: >- |
| 133 | + gh release create |
| 134 | + "${{ github.ref_name }}" |
| 135 | + --repo "${{ github.repository }}" |
| 136 | + --notes "" |
| 137 | + - name: Upload artifact signatures to GitHub Release |
| 138 | + env: |
| 139 | + GITHUB_TOKEN: ${{ github.token }} |
| 140 | + run: >- |
| 141 | + gh release upload |
| 142 | + "${{ github.ref_name }}" dist/** |
| 143 | + --repo "${{ github.repository }}" |
| 144 | +
|
| 145 | + publish-to-testpypi: |
| 146 | + name: Publish Python distribution to TestPyPI |
| 147 | + # if: ${{ github.ref == "refs/heads/master" && github.repository_owner == "theochem" }} |
| 148 | + needs: |
| 149 | + - build |
| 150 | + runs-on: ubuntu-latest |
| 151 | + |
| 152 | + environment: |
| 153 | + name: TestPyPI |
| 154 | + url: https://test.pypi.org/project/${{ env.PYPI_NAME }} |
| 155 | + |
| 156 | + permissions: |
| 157 | + id-token: write |
| 158 | + |
| 159 | + steps: |
| 160 | + - name: Download all the dists |
| 161 | + uses: actions/download-artifact@v4 |
| 162 | + with: |
| 163 | + name: python-package-distributions |
| 164 | + path: dist/ |
| 165 | + - name: Publish distribution to TestPyPI |
| 166 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 167 | + with: |
| 168 | + repository-url: https://test.pypi.org/legacy/ |
| 169 | + env: |
| 170 | + TWINE_USERNAME: "__token__" |
| 171 | + TWINE_PASSWORD: ${{ secrets.TEST_PYPI_TOKEN }} |
0 commit comments