|
| 1 | +name: Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, master ] |
| 6 | + paths: |
| 7 | + - 'pyproject.toml' |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + id-token: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + format-lint: |
| 15 | + name: "Format and Lint" |
| 16 | + uses: ./.github/workflows/_format-lint-action.yml |
| 17 | + |
| 18 | + test: |
| 19 | + name: "Run Tests" |
| 20 | + needs: format-lint |
| 21 | + uses: ./.github/workflows/_run-tests-action.yml |
| 22 | + |
| 23 | + build: |
| 24 | + name: "Build Docker Image" |
| 25 | + needs: [format-lint, test] |
| 26 | + uses: ./.github/workflows/_build-docker-action.yml |
| 27 | + |
| 28 | + check-version-changed: |
| 29 | + name: "Check if version changed" |
| 30 | + runs-on: ubuntu-latest |
| 31 | + outputs: |
| 32 | + version: ${{ steps.version.outputs.version }} |
| 33 | + version-changed: ${{ steps.check.outputs.changed }} |
| 34 | + is-prerelease: ${{ steps.version.outputs.is-prerelease }} |
| 35 | + steps: |
| 36 | + - uses: actions/checkout@v4 |
| 37 | + with: |
| 38 | + fetch-depth: 2 |
| 39 | + |
| 40 | + - name: Get current version |
| 41 | + id: version |
| 42 | + run: | |
| 43 | + VERSION=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/') |
| 44 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 45 | + |
| 46 | + # Check if version contains letters (indicating pre-release) |
| 47 | + if echo "$VERSION" | grep -q '[a-zA-Z]'; then |
| 48 | + echo "is-prerelease=true" >> $GITHUB_OUTPUT |
| 49 | + echo "Detected pre-release version: $VERSION" |
| 50 | + else |
| 51 | + echo "is-prerelease=false" >> $GITHUB_OUTPUT |
| 52 | + echo "Detected stable release version: $VERSION" |
| 53 | + fi |
| 54 | + |
| 55 | + - name: Check if version changed |
| 56 | + id: check |
| 57 | + run: | |
| 58 | + if git diff HEAD~1 HEAD --name-only | grep -q pyproject.toml; then |
| 59 | + if git diff HEAD~1 HEAD pyproject.toml | grep -q '^+version = '; then |
| 60 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 61 | + echo "Version changed in pyproject.toml" |
| 62 | + else |
| 63 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 64 | + echo "pyproject.toml changed but version did not change" |
| 65 | + fi |
| 66 | + else |
| 67 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 68 | + echo "pyproject.toml was not changed" |
| 69 | + fi |
| 70 | +
|
| 71 | + build-package: |
| 72 | + name: "Build Python Package" |
| 73 | + runs-on: ubuntu-latest |
| 74 | + needs: [build, check-version-changed] |
| 75 | + if: needs.check-version-changed.outputs.version-changed == 'true' |
| 76 | + steps: |
| 77 | + - uses: actions/checkout@v4 |
| 78 | + |
| 79 | + - name: Install uv |
| 80 | + uses: astral-sh/setup-uv@v4 |
| 81 | + with: |
| 82 | + enable-cache: true |
| 83 | + |
| 84 | + - name: Set up Python |
| 85 | + run: uv python install 3.10 |
| 86 | + |
| 87 | + - name: Build package |
| 88 | + run: uv build |
| 89 | + |
| 90 | + - name: Upload build artifacts |
| 91 | + uses: actions/upload-artifact@v4 |
| 92 | + with: |
| 93 | + name: dist |
| 94 | + path: dist/ |
| 95 | + |
| 96 | + publish-pypi: |
| 97 | + name: "Publish to PyPI" |
| 98 | + runs-on: ubuntu-latest |
| 99 | + needs: [build-package, check-version-changed] |
| 100 | + if: needs.check-version-changed.outputs.version-changed == 'true' |
| 101 | + environment: release |
| 102 | + steps: |
| 103 | + - name: Download build artifacts |
| 104 | + uses: actions/download-artifact@v4 |
| 105 | + with: |
| 106 | + name: dist |
| 107 | + path: dist/ |
| 108 | + |
| 109 | + - name: Install uv |
| 110 | + uses: astral-sh/setup-uv@v4 |
| 111 | + |
| 112 | + - name: Publish to PyPI |
| 113 | + run: uv publish --token ${{ secrets.PYPI_API_TOKEN }} |
| 114 | + working-directory: . |
| 115 | + |
| 116 | + create-release: |
| 117 | + name: "Create GitHub Release" |
| 118 | + runs-on: ubuntu-latest |
| 119 | + needs: [publish-pypi, check-version-changed] |
| 120 | + if: needs.check-version-changed.outputs.version-changed == 'true' |
| 121 | + steps: |
| 122 | + - uses: actions/checkout@v4 |
| 123 | + |
| 124 | + - name: Download build artifacts |
| 125 | + uses: actions/download-artifact@v4 |
| 126 | + with: |
| 127 | + name: dist |
| 128 | + path: dist/ |
| 129 | + |
| 130 | + - name: Create Release |
| 131 | + uses: softprops/action-gh-release@v2 |
| 132 | + with: |
| 133 | + tag_name: v${{ needs.check-version-changed.outputs.version }} |
| 134 | + name: Release v${{ needs.check-version-changed.outputs.version }} |
| 135 | + draft: false |
| 136 | + prerelease: ${{ needs.check-version-changed.outputs.is-prerelease == 'true' }} |
| 137 | + generate_release_notes: true |
| 138 | + files: dist/* |
0 commit comments