|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "[0-9]+.[0-9]+.[0-9]+" |
| 7 | + |
| 8 | +env: |
| 9 | + PACKAGE_NAME: "direct-data-driven-mpc" |
| 10 | + TAG_NAME: ${{ github.ref_name }} |
| 11 | + PYTHON_VERSION: '3.12' |
| 12 | + |
| 13 | +jobs: |
| 14 | + verify_tag: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Verify tag matches pyproject.toml version |
| 23 | + id: release |
| 24 | + run: | |
| 25 | + TAG_VERSION=$TAG_NAME |
| 26 | + PYPROJECT_VERSION=$(sed -nE 's/^version\s*=\s*"([^"]+)"/\1/p' pyproject.toml) |
| 27 | + echo "Tag version: $TAG_VERSION" |
| 28 | + echo "pyproject.toml version: $PYPROJECT_VERSION" |
| 29 | + if [ "$TAG_VERSION" != "$PYPROJECT_VERSION" ]; then |
| 30 | + echo "Tag does not match pyproject.toml version." |
| 31 | + exit 1 |
| 32 | + else |
| 33 | + echo "Tag matches pyproject.toml version." |
| 34 | + fi |
| 35 | +
|
| 36 | + - name: Verify tag points to latest main commit |
| 37 | + run: | |
| 38 | + git fetch origin main |
| 39 | + LATEST_MAIN_SHA=$(git rev-parse origin/main) |
| 40 | + echo "Tag commit SHA: $GITHUB_SHA" |
| 41 | + echo "Latest main commit SHA: $LATEST_MAIN_SHA" |
| 42 | + if [ "$GITHUB_SHA" != "$LATEST_MAIN_SHA" ]; then |
| 43 | + echo "Tag does not point to the latest main commit." |
| 44 | + exit 1 |
| 45 | + else |
| 46 | + echo "Tag points to the latest main commit." |
| 47 | + fi |
| 48 | +
|
| 49 | + check_pypi: |
| 50 | + needs: verify_tag |
| 51 | + runs-on: ubuntu-latest |
| 52 | + steps: |
| 53 | + - name: Fetch information from PyPI |
| 54 | + run: | |
| 55 | + response=$(curl -s https://pypi.org/pypi/${{ env.PACKAGE_NAME }}/json || echo "{}") |
| 56 | + latest_previous_version=$(echo $response | jq --raw-output "select(.releases != null) | .releases | keys_unsorted | last") |
| 57 | + if [ -z "$latest_previous_version" ]; then |
| 58 | + echo "Package not found on PyPI." |
| 59 | + latest_previous_version="0.0.0" |
| 60 | + fi |
| 61 | + echo "Latest version on PyPI: $latest_previous_version" |
| 62 | + echo "latest_previous_version=$latest_previous_version" >> $GITHUB_ENV |
| 63 | +
|
| 64 | + - name: Compare versions and exit if not newer |
| 65 | + run: | |
| 66 | + NEW_VERSION=$TAG_NAME |
| 67 | + LATEST_VERSION=$latest_previous_version |
| 68 | + HIGHEST_VERSION=$(printf '%s\n' "$LATEST_VERSION" "$NEW_VERSION" | sort -rV | head -n 1) |
| 69 | + if [[ "$HIGHEST_VERSION" != "$NEW_VERSION" || "$NEW_VERSION" = "$LATEST_VERSION" ]]; then |
| 70 | + echo "The new version $NEW_VERSION is not greater than the latest version $LATEST_VERSION on PyPI." |
| 71 | + exit 1 |
| 72 | + else |
| 73 | + echo "The new version $NEW_VERSION is greater than the latest version $LATEST_VERSION on PyPI." |
| 74 | + fi |
| 75 | +
|
| 76 | + build: |
| 77 | + needs: check_pypi |
| 78 | + runs-on: ubuntu-latest |
| 79 | + steps: |
| 80 | + - name: Checkout |
| 81 | + uses: actions/checkout@v4 |
| 82 | + |
| 83 | + - name: Set up Python |
| 84 | + uses: actions/setup-python@v5 |
| 85 | + with: |
| 86 | + python-version: ${{ env.PYTHON_VERSION }} |
| 87 | + |
| 88 | + - name: Upgrade pip and install build dependencies |
| 89 | + run: | |
| 90 | + python -m pip install --upgrade pip |
| 91 | + pip install build |
| 92 | +
|
| 93 | + - name: Install project package in editable mode |
| 94 | + run: | |
| 95 | + pip install -e . |
| 96 | +
|
| 97 | + - name: Build source and wheel distribution |
| 98 | + run: | |
| 99 | + python -m build |
| 100 | +
|
| 101 | + - name: Upload artifacts |
| 102 | + uses: actions/upload-artifact@v4 |
| 103 | + with: |
| 104 | + name: dist |
| 105 | + path: dist/ |
| 106 | + |
| 107 | + pypi_publish: |
| 108 | + needs: build |
| 109 | + runs-on: ubuntu-latest |
| 110 | + environment: |
| 111 | + name: release |
| 112 | + permissions: |
| 113 | + id-token: write |
| 114 | + steps: |
| 115 | + - name: Download artifacts |
| 116 | + uses: actions/download-artifact@v4 |
| 117 | + with: |
| 118 | + name: dist |
| 119 | + path: dist/ |
| 120 | + |
| 121 | + - name: Publish distribution to PyPI |
| 122 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 123 | + |
| 124 | + github_release: |
| 125 | + needs: pypi_publish |
| 126 | + runs-on: ubuntu-latest |
| 127 | + permissions: |
| 128 | + contents: write |
| 129 | + steps: |
| 130 | + - name: Checkout |
| 131 | + uses: actions/checkout@v4 |
| 132 | + with: |
| 133 | + fetch-depth: 0 |
| 134 | + |
| 135 | + - name: Download artifacts |
| 136 | + uses: actions/download-artifact@v4 |
| 137 | + with: |
| 138 | + name: dist |
| 139 | + path: dist/ |
| 140 | + |
| 141 | + - name: Create GitHub Release |
| 142 | + uses: softprops/action-gh-release@v2 |
| 143 | + with: |
| 144 | + generate_release_notes: true |
| 145 | + files: dist/* |
0 commit comments