|
| 1 | +# This workflow is used to release packages to PyPI when its |
| 2 | +# pyproject.toml version is changed or a new package is added. |
| 3 | + |
| 4 | +name: Release on Version Change |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +jobs: |
| 12 | + detect-version-changes: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + outputs: |
| 15 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 16 | + has-changes: ${{ steps.set-matrix.outputs.has-changes }} |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Get changed files |
| 24 | + id: changed-files |
| 25 | + uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46 |
| 26 | + with: |
| 27 | + files: | |
| 28 | + **/pyproject.toml |
| 29 | +
|
| 30 | + - name: Check for version changes or new packages |
| 31 | + id: check-versions |
| 32 | + if: steps.changed-files.outputs.any_changed == 'true' |
| 33 | + run: | |
| 34 | + ./.github/scripts/check-version-changes.sh "${{ steps.changed-files.outputs.all_changed_files }}" |
| 35 | +
|
| 36 | + - name: Set matrix |
| 37 | + id: set-matrix |
| 38 | + run: | |
| 39 | + packages='${{ steps.check-versions.outputs.packages }}' |
| 40 | + if [ -z "$packages" ] || [ "$packages" = "[]" ]; then |
| 41 | + echo "has-changes=false" >> $GITHUB_OUTPUT |
| 42 | + echo "matrix={\"include\":[]}" >> $GITHUB_OUTPUT |
| 43 | + else |
| 44 | + echo "has-changes=true" >> $GITHUB_OUTPUT |
| 45 | + # Create matrix with package directories |
| 46 | + matrix=$(echo "$packages" | jq -c '{include: [.[] | {package: .}]}') |
| 47 | + echo "matrix=$matrix" >> $GITHUB_OUTPUT |
| 48 | + echo "Matrix: $matrix" |
| 49 | + fi |
| 50 | +
|
| 51 | + build-and-test: |
| 52 | + needs: detect-version-changes |
| 53 | + if: needs.detect-version-changes.outputs.has-changes == 'true' |
| 54 | + runs-on: ubuntu-latest |
| 55 | + strategy: |
| 56 | + matrix: ${{ fromJson(needs.detect-version-changes.outputs.matrix) }} |
| 57 | + permissions: |
| 58 | + contents: write |
| 59 | + packages: write |
| 60 | + steps: |
| 61 | + - name: Checkout code |
| 62 | + uses: actions/checkout@v4 |
| 63 | + with: |
| 64 | + fetch-depth: 0 |
| 65 | + |
| 66 | + - name: Extract package name and version |
| 67 | + working-directory: ${{ matrix.package }} |
| 68 | + run: | |
| 69 | + # Extract package name |
| 70 | + PACKAGE_NAME=$(grep -m1 '^name = ' pyproject.toml | cut -d'"' -f2) |
| 71 | + echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_ENV |
| 72 | +
|
| 73 | + # Extract version |
| 74 | + VERSION=$(grep -m1 '^version = ' pyproject.toml | cut -d'"' -f2) |
| 75 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 76 | +
|
| 77 | + echo "Building $PACKAGE_NAME version $VERSION" |
| 78 | +
|
| 79 | + - name: Set up the environment |
| 80 | + uses: ./.github/actions/setup-uv-env |
| 81 | + with: |
| 82 | + python-version: "3.10" |
| 83 | + is-server: ${{ startsWith(matrix.package, 'servers/') }} |
| 84 | + working-directory: ${{ matrix.package }} |
| 85 | + |
| 86 | + - name: Build release distributions |
| 87 | + working-directory: ${{ matrix.package }} |
| 88 | + run: | |
| 89 | + uv build --wheel --out-dir dist | tee build.log |
| 90 | +
|
| 91 | + # Verify build artifacts |
| 92 | + ls -la dist/ |
| 93 | + echo "Built artifacts for ${{ env.PACKAGE_NAME }} v${{ env.VERSION }}" |
| 94 | +
|
| 95 | + - name: Upload release distributions |
| 96 | + uses: actions/upload-artifact@v4 |
| 97 | + with: |
| 98 | + name: release-dists-${{ env.PACKAGE_NAME }}-${{ env.VERSION }} |
| 99 | + path: ${{ matrix.package }}/dist/ |
| 100 | + |
| 101 | + pypi-publish: |
| 102 | + needs: [detect-version-changes, build-and-test] |
| 103 | + if: needs.detect-version-changes.outputs.has-changes == 'true' |
| 104 | + runs-on: ubuntu-latest |
| 105 | + strategy: |
| 106 | + matrix: ${{ fromJson(needs.detect-version-changes.outputs.matrix) }} |
| 107 | + permissions: |
| 108 | + id-token: write |
| 109 | + contents: read |
| 110 | + steps: |
| 111 | + - name: Checkout code |
| 112 | + uses: actions/checkout@v4 |
| 113 | + with: |
| 114 | + fetch-depth: 0 |
| 115 | + |
| 116 | + - name: Extract package name and version |
| 117 | + working-directory: ${{ matrix.package }} |
| 118 | + run: | |
| 119 | + PACKAGE_NAME=$(grep -m1 '^name = ' pyproject.toml | cut -d'"' -f2) |
| 120 | + echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_ENV |
| 121 | +
|
| 122 | + VERSION=$(grep -m1 '^version = ' pyproject.toml | cut -d'"' -f2) |
| 123 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 124 | +
|
| 125 | + - name: Retrieve release distributions |
| 126 | + uses: actions/download-artifact@v4 |
| 127 | + with: |
| 128 | + name: release-dists-${{ env.PACKAGE_NAME }}-${{ env.VERSION }} |
| 129 | + path: dist/ |
| 130 | + |
| 131 | + - name: Publish release distributions to PyPI |
| 132 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 133 | + |
| 134 | + - name: Send status to Slack |
| 135 | + if: always() |
| 136 | + |
| 137 | + with: |
| 138 | + webhook: ${{ secrets.PACKAGE_RELEASE_SLACK_WEBHOOK_URL }} |
| 139 | + webhook-type: webhook-trigger |
| 140 | + payload: | |
| 141 | + { |
| 142 | + "status": "${{ (job.status == 'failure' || job.status == 'cancelled') && 'Failed' || 'Success' }}", |
| 143 | + "package": "${{ env.PACKAGE_NAME }}", |
| 144 | + "version": "${{ env.VERSION }}", |
| 145 | + "url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 146 | + } |
0 commit comments