|
| 1 | +name: Build |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main # allow to trigger the workflow with tag push event |
| 7 | + pull_request: |
| 8 | + paths: |
| 9 | + - setup.py |
| 10 | + - setup.cfg |
| 11 | + - pyproject.toml |
| 12 | + - MANIFEST.in |
| 13 | + - CMakeLists.txt |
| 14 | + - include/** |
| 15 | + - src/** |
| 16 | + - torchopt/version.py |
| 17 | + - .github/workflow/build.yml |
| 18 | + release: |
| 19 | + types: |
| 20 | + - published |
| 21 | + # Allow to trigger the workflow manually |
| 22 | + workflow_dispatch: |
| 23 | + |
| 24 | +permissions: |
| 25 | + contents: read |
| 26 | + |
| 27 | +concurrency: |
| 28 | + group: "${{ github.workflow }}-${{ github.ref }}" |
| 29 | + cancel-in-progress: ${{ github.event_name == 'pull_request' }} |
| 30 | + |
| 31 | +jobs: |
| 32 | + build: |
| 33 | + runs-on: ubuntu-18.04 |
| 34 | + if: github.repository == 'metaopt/TorchOpt' && (github.event_name != 'push' || startsWith(github.ref, 'refs/tags/')) |
| 35 | + timeout-minutes: 45 |
| 36 | + steps: |
| 37 | + - name: Checkout |
| 38 | + uses: actions/checkout@v3 |
| 39 | + with: |
| 40 | + submodules: "recursive" |
| 41 | + fetch-depth: 1 |
| 42 | + |
| 43 | + - name: Set up Python 3.7 |
| 44 | + id: py37 |
| 45 | + uses: actions/setup-python@v4 |
| 46 | + with: |
| 47 | + python-version: "3.7" |
| 48 | + update-environment: false |
| 49 | + |
| 50 | + - name: Set up Python 3.8 |
| 51 | + id: py38 |
| 52 | + uses: actions/setup-python@v4 |
| 53 | + with: |
| 54 | + python-version: "3.8" |
| 55 | + update-environment: false |
| 56 | + |
| 57 | + - name: Set up Python 3.9 |
| 58 | + id: py39 |
| 59 | + uses: actions/setup-python@v4 |
| 60 | + with: |
| 61 | + python-version: "3.9" |
| 62 | + update-environment: false |
| 63 | + |
| 64 | + - name: Set up Python 3.10 |
| 65 | + id: py310 |
| 66 | + uses: actions/setup-python@v4 |
| 67 | + with: |
| 68 | + python-version: "3.10" |
| 69 | + update-environment: false |
| 70 | + |
| 71 | + - name: Set up Python executable paths |
| 72 | + run: | |
| 73 | + echo "${{ steps.py37.outputs.python-path }}" > .python-paths |
| 74 | + echo "${{ steps.py38.outputs.python-path }}" >> .python-paths |
| 75 | + echo "${{ steps.py39.outputs.python-path }}" >> .python-paths |
| 76 | + echo "${{ steps.py310.outputs.python-path }}" >> .python-paths |
| 77 | +
|
| 78 | + - name: Setup CUDA Toolkit |
| 79 | + uses: Jimver/cuda-toolkit@v0.2.7 |
| 80 | + id: cuda-toolkit |
| 81 | + with: |
| 82 | + cuda: "11.6.2" |
| 83 | + method: network |
| 84 | + sub-packages: '["nvcc"]' |
| 85 | + - run: | |
| 86 | + CUDA_VERSION="${{steps.cuda-toolkit.outputs.cuda}}" |
| 87 | + echo "CUDA_VERSION=${CUDA_VERSION}" >> "${GITHUB_ENV}" |
| 88 | + TORCH_INDEX_URL="https://download.pytorch.org/whl/cu$(echo "${CUDA_VERSION}" | cut -d'.' -f-2 | tr -d '.')" |
| 89 | + echo "TORCH_INDEX_URL=${TORCH_INDEX_URL}" >> "${GITHUB_ENV}" |
| 90 | +
|
| 91 | + echo "Installed CUDA version is: ${CUDA_VERSION}" |
| 92 | + echo "CUDA install location: ${{steps.cuda-toolkit.outputs.CUDA_PATH}}" |
| 93 | + nvcc -V |
| 94 | + echo "Torch index URL: ${TORCH_INDEX_URL}" |
| 95 | +
|
| 96 | + - name: Build sdist and wheels |
| 97 | + run: | |
| 98 | + DEFAULT_PYTHON="$(head -n 1 .python-paths)" |
| 99 | +
|
| 100 | + while read -r PYTHON; do |
| 101 | + echo "Building wheel with Python: ${PYTHON} ($("${PYTHON}" --version))" |
| 102 | + "${PYTHON}" -m pip install --upgrade pip setuptools wheel build |
| 103 | + "${PYTHON}" -m pip install --extra-index-url "${TORCH_INDEX_URL}" \ |
| 104 | + -r requirements.txt |
| 105 | + if [[ "${PYTHON}" == "${DEFAULT_PYTHON}" ]]; then |
| 106 | + "${PYTHON}" -m build |
| 107 | + else |
| 108 | + "${PYTHON}" -m build --wheel |
| 109 | + fi |
| 110 | + done < .python-paths |
| 111 | +
|
| 112 | + - name: List built sdist and wheels |
| 113 | + run: | |
| 114 | + if [[ -n "$(find dist -maxdepth 0 -not -empty -print 2>/dev/null)" ]]; then |
| 115 | + echo "Built sdist and wheels:" |
| 116 | + ls -lh dist/ |
| 117 | + else |
| 118 | + echo "No sdist and wheels are built." |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | +
|
| 122 | + - name: Audit and repair wheels |
| 123 | + run: | |
| 124 | + while read -r PYTHON; do |
| 125 | + PYVER="cp$("${PYTHON}" --version | cut -d ' ' -f2 | cut -d '.' -f-2 | tr -d '.')" |
| 126 | + echo "Audit and repair wheel for Python: ${PYTHON} (${PYVER})" |
| 127 | + LIBTORCH_PATH="$("${PYTHON}" -c 'import os, site; print(os.path.join(site.getsitepackages()[0], "torch", "lib"))')" |
| 128 | + "${PYTHON}" -m pip install --upgrade git+https://github.com/XuehaiPan/auditwheel.git@torchopt |
| 129 | + ( |
| 130 | + export LD_LIBRARY_PATH="${LIBTORCH_PATH}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}" |
| 131 | + echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" |
| 132 | + "${PYTHON}" -m auditwheel show dist/torchopt-*-${PYVER}-*.whl && |
| 133 | + "${PYTHON}" -m auditwheel repair --plat manylinux2014_x86_64 --wheel-dir wheelhouse dist/torchopt-*-${PYVER}-*.whl |
| 134 | + ) |
| 135 | + done < .python-paths |
| 136 | +
|
| 137 | + rm dist/torchopt-*.whl |
| 138 | + mv wheelhouse/torchopt-*manylinux*.whl dist/ |
| 139 | +
|
| 140 | + - name: List built sdist and wheels |
| 141 | + run: | |
| 142 | + if [[ -n "$(find dist -maxdepth 0 -not -empty -print 2>/dev/null)" ]]; then |
| 143 | + echo "Built sdist and wheels:" |
| 144 | + ls -lh dist/ |
| 145 | + else |
| 146 | + echo "No sdist and wheels are built." |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | +
|
| 150 | + - name: Test sdist and wheels |
| 151 | + run: | |
| 152 | + DEFAULT_PYTHON="$(head -n 1 .python-paths)" |
| 153 | + while read -r PYTHON; do |
| 154 | + PYVER="cp$("${PYTHON}" --version | cut -d ' ' -f2 | cut -d '.' -f-2 | tr -d '.')" |
| 155 | + mkdir -p "temp-${PYVER}" |
| 156 | + pushd "temp-${PYVER}" |
| 157 | + if [[ "${PYTHON}" == "${DEFAULT_PYTHON}" ]]; then |
| 158 | + echo "Testing sdist with Python: ${PYTHON} (${PYVER})" |
| 159 | + "${PYTHON}" -m pip uninstall torch torchopt -y |
| 160 | + "${PYTHON}" -m pip install --extra-index-url https://download.pytorch.org/whl/cpu \ |
| 161 | + ../dist/torchopt-*.tar.gz |
| 162 | + "${PYTHON}" -c 'import torchopt' |
| 163 | + fi |
| 164 | + echo "Testing wheel with Python: ${PYTHON} (${PYVER})" |
| 165 | + "${PYTHON}" -m pip uninstall torch torchopt -y |
| 166 | + "${PYTHON}" -m pip install --extra-index-url https://download.pytorch.org/whl/cpu \ |
| 167 | + ../dist/torchopt-*-${PYVER}-*.whl |
| 168 | + "${PYTHON}" -c 'import torchopt' |
| 169 | + "${PYTHON}" -m pip uninstall torch torchopt -y |
| 170 | + popd |
| 171 | + done < .python-paths |
| 172 | +
|
| 173 | + - name: Check consistency between the package version and release tag |
| 174 | + if: startsWith(github.ref, 'refs/tags/') |
| 175 | + run: | |
| 176 | + RELEASE_TAG="${GITHUB_REF#refs/*/}" |
| 177 | + PACKAGE_VER="v$(python setup.py --version)" |
| 178 | + if [[ "${PACKAGE_VER}" != "${RELEASE_TAG}" ]]; then |
| 179 | + echo "package ver. (${PACKAGE_VER}) != release tag. (${RELEASE_TAG})" |
| 180 | + exit 1 |
| 181 | + fi |
| 182 | +
|
| 183 | + - name: Publish to TestPyPI |
| 184 | + if: startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' |
| 185 | + uses: pypa/gh-action-pypi-publish@v1.5.0 |
| 186 | + with: |
| 187 | + user: __token__ |
| 188 | + password: ${{ secrets.TESTPYPI_UPLOAD_TOKEN }} |
| 189 | + repository_url: https://test.pypi.org/legacy/ |
| 190 | + verbose: true |
| 191 | + print_hash: true |
| 192 | + skip_existing: true |
| 193 | + |
| 194 | + - name: Publish to PyPI |
| 195 | + if: startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' |
| 196 | + uses: pypa/gh-action-pypi-publish@v1.5.0 |
| 197 | + with: |
| 198 | + user: __token__ |
| 199 | + password: ${{ secrets.PYPI_UPLOAD_TOKEN }} |
| 200 | + verbose: true |
| 201 | + print_hash: true |
| 202 | + skip_existing: true |
0 commit comments