|
| 1 | +name: Build wheel and release into PYPI |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - dev |
| 9 | + paths: |
| 10 | + - VERSION |
| 11 | + |
| 12 | +jobs: |
| 13 | + build_wheels: |
| 14 | + if: github.repository_owner == 'mlcommons' |
| 15 | + name: Build wheel |
| 16 | + runs-on: ubuntu-latest |
| 17 | + environment: release |
| 18 | + |
| 19 | + permissions: |
| 20 | + id-token: write |
| 21 | + contents: write |
| 22 | + |
| 23 | + strategy: |
| 24 | + fail-fast: false |
| 25 | + steps: |
| 26 | + # Step 1: Checkout the code |
| 27 | + - uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + fetch-depth: 2 |
| 30 | + ssh-key: ${{ secrets.DEPLOY_KEY }} |
| 31 | + ref: ${{ github.ref_name }} |
| 32 | + |
| 33 | + # Step 2: Set up Python |
| 34 | + - uses: actions/setup-python@v3 |
| 35 | + |
| 36 | + # Step 3: Check if VERSION file has changed in this push |
| 37 | + - name: Check if VERSION file has changed |
| 38 | + id: version_changed |
| 39 | + run: | |
| 40 | + git config --global user.name mlcommons-bot |
| 41 | + git config --global user.email "mlcommons-bot@users.noreply.github.com" |
| 42 | + if git diff --name-only $(git merge-base HEAD HEAD~1) | grep -q "VERSION"; then |
| 43 | + echo "VERSION file has been modified" |
| 44 | + echo "::set-output name=version_changed::true" |
| 45 | + new_version=$(cat VERSION) |
| 46 | + else |
| 47 | + echo "VERSION file has NOT been modified" |
| 48 | + echo "::set-output name=version_changed::false" |
| 49 | + fi |
| 50 | + echo "::set-output name=new_version::$new_version" |
| 51 | +
|
| 52 | + # Step 4: Increment version if VERSION was not changed |
| 53 | + - name: Increment version if necessary |
| 54 | + id: do_version_increment |
| 55 | + if: steps.version_changed.outputs.version_changed == 'false' |
| 56 | + run: | |
| 57 | + # Check if VERSION file exists, else initialize it |
| 58 | + if [ ! -f VERSION ]; then |
| 59 | + echo "0.0.0" > VERSION |
| 60 | + fi |
| 61 | + |
| 62 | + version=$(cat VERSION) |
| 63 | + IFS='.' read -r major minor patch <<< "$version" |
| 64 | + patch=$((patch + 1)) |
| 65 | + new_version="$major.$minor.$patch" |
| 66 | + echo $new_version > VERSION |
| 67 | + echo "New version: $new_version" |
| 68 | + echo "::set-output name=new_version::$new_version" |
| 69 | +
|
| 70 | + # Step 5: Commit the updated version to the repository |
| 71 | + - name: Commit updated version |
| 72 | + if: steps.version_changed.outputs.version_changed == 'false' |
| 73 | + run: | |
| 74 | + git add VERSION |
| 75 | + git commit -m "Increment version to ${{ steps.do_version_increment.outputs.new_version }}" |
| 76 | + git pull --rebase |
| 77 | + git push |
| 78 | +
|
| 79 | + # Step 6: Install required dependencies |
| 80 | + - name: Install requirements |
| 81 | + run: python3 -m pip install setuptools wheel build |
| 82 | + |
| 83 | + # Step 7: Build the Python wheel |
| 84 | + - name: Build wheels |
| 85 | + working-directory: ./ |
| 86 | + run: python3 -m build && rm dist/*.whl |
| 87 | + |
| 88 | + # Step 8: Publish to PyPI |
| 89 | + - name: Publish to PyPI |
| 90 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 91 | + with: |
| 92 | + verify-metadata: true |
| 93 | + skip-existing: true |
| 94 | + packages-dir: dist |
| 95 | + repository-url: https://upload.pypi.org/legacy/ |
| 96 | + verbose: true |
0 commit comments