|
| 1 | +name: Publish Python Package to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: 'Version to publish (semantic version)' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + dry_run: |
| 11 | + description: 'Run in dry-run mode only' |
| 12 | + required: false |
| 13 | + type: boolean |
| 14 | + default: true |
| 15 | + repository: |
| 16 | + description: 'PyPI repository (pypi or testpypi)' |
| 17 | + required: false |
| 18 | + type: choice |
| 19 | + options: |
| 20 | + - 'pypi' |
| 21 | + - 'testpypi' |
| 22 | + default: 'pypi' |
| 23 | + push: |
| 24 | + tags: |
| 25 | + - 'python-v*' |
| 26 | + - 'pypi-v*' |
| 27 | + release: |
| 28 | + types: [published] |
| 29 | + |
| 30 | +permissions: |
| 31 | + contents: write |
| 32 | + packages: write |
| 33 | + id-token: write # For PyPI trusted publishing |
| 34 | + |
| 35 | +env: |
| 36 | + CARGO_TERM_COLOR: always |
| 37 | + RUST_BACKTRACE: 1 |
| 38 | + |
| 39 | +jobs: |
| 40 | + validate: |
| 41 | + name: Validate Python Package |
| 42 | + runs-on: ubuntu-latest |
| 43 | + steps: |
| 44 | + - name: Checkout repository |
| 45 | + uses: actions/checkout@v4 |
| 46 | + |
| 47 | + - name: Set up Python |
| 48 | + uses: actions/setup-python@v5 |
| 49 | + with: |
| 50 | + python-version: "3.12" |
| 51 | + |
| 52 | + - name: Install uv |
| 53 | + uses: astral-sh/setup-uv@v5 |
| 54 | + with: |
| 55 | + enable-cache: true |
| 56 | + |
| 57 | + - name: Install Rust |
| 58 | + uses: dtolnay/rust-toolchain@stable |
| 59 | + |
| 60 | + - name: Validate package metadata |
| 61 | + working-directory: crates/terraphim_automata_py |
| 62 | + run: | |
| 63 | + python -c "import tomllib; pkg = tomllib.load(open('pyproject.toml', 'rb')); print('Package name:', pkg['project']['name']); print('Version:', pkg['project']['version'])" |
| 64 | +
|
| 65 | + - name: Validate version format |
| 66 | + run: | |
| 67 | + if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == refs/tags/* ]]; then |
| 68 | + VERSION=$(echo "${{ github.ref }}" | sed 's/refs\/tags\/python-v//;s/refs\/tags\/pypi-v//') |
| 69 | + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 70 | + echo "Invalid version format: $VERSION" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + echo "Version to publish: $VERSION" |
| 74 | + fi |
| 75 | +
|
| 76 | + build: |
| 77 | + name: Build Python Distributions |
| 78 | + runs-on: ${{ matrix.os }} |
| 79 | + needs: validate |
| 80 | + strategy: |
| 81 | + fail-fast: false |
| 82 | + matrix: |
| 83 | + os: [ubuntu-latest, windows-latest, macos-latest] |
| 84 | + python-version: ['3.9', '3.10', '3.11', '3.12'] |
| 85 | + include: |
| 86 | + - os: ubuntu-latest |
| 87 | + target: x86_64-unknown-linux-gnu |
| 88 | + - os: windows-latest |
| 89 | + target: x86_64-pc-windows-msvc |
| 90 | + - os: macos-latest |
| 91 | + target: x86_64-apple-darwin |
| 92 | + macos-arch: universal |
| 93 | + |
| 94 | + steps: |
| 95 | + - name: Checkout repository |
| 96 | + uses: actions/checkout@v4 |
| 97 | + |
| 98 | + - name: Set up Python |
| 99 | + uses: actions/setup-python@v5 |
| 100 | + with: |
| 101 | + python-version: ${{ matrix.python-version }} |
| 102 | + |
| 103 | + - name: Install Rust |
| 104 | + uses: dtolnay/rust-toolchain@stable |
| 105 | + with: |
| 106 | + toolchain: stable |
| 107 | + targets: ${{ matrix.target }} |
| 108 | + |
| 109 | + - name: Install uv |
| 110 | + uses: astral-sh/setup-uv@v5 |
| 111 | + with: |
| 112 | + enable-cache: true |
| 113 | + python-version: ${{ matrix.python-version }} |
| 114 | + |
| 115 | + - name: Cache Cargo dependencies |
| 116 | + uses: actions/cache@v4 |
| 117 | + with: |
| 118 | + path: | |
| 119 | + ~/.cargo/registry/index/ |
| 120 | + ~/.cargo/registry/cache/ |
| 121 | + ~/.cargo/git/db/ |
| 122 | + target/ |
| 123 | + key: ${{ runner.os }}-cargo-${{ matrix.target }}-pypi-${{ hashFiles('**/Cargo.lock') }} |
| 124 | + |
| 125 | + - name: Install Python build dependencies |
| 126 | + working-directory: crates/terraphim_automata_py |
| 127 | + run: | |
| 128 | + uv pip install --system maturin pytest pytest-benchmark build |
| 129 | +
|
| 130 | + - name: Build wheel |
| 131 | + uses: PyO3/maturin-action@v1 |
| 132 | + with: |
| 133 | + working-directory: crates/terraphim_automata_py |
| 134 | + args: --release --out dist --find-interpreter --target ${{ matrix.target }} |
| 135 | + sccache: 'true' |
| 136 | + manylinux: auto |
| 137 | + |
| 138 | + - name: Upload wheel artifacts |
| 139 | + uses: actions/upload-artifact@v4 |
| 140 | + with: |
| 141 | + name: wheels-${{ matrix.os }}-py${{ matrix.python-version }} |
| 142 | + path: crates/terraphim_automata_py/dist/*.whl |
| 143 | + if-no-files-found: error |
| 144 | + |
| 145 | + build-sdist: |
| 146 | + name: Build Source Distribution |
| 147 | + runs-on: ubuntu-latest |
| 148 | + needs: validate |
| 149 | + steps: |
| 150 | + - name: Checkout repository |
| 151 | + uses: actions/checkout@v4 |
| 152 | + |
| 153 | + - name: Set up Python |
| 154 | + uses: actions/setup-python@v5 |
| 155 | + with: |
| 156 | + python-version: "3.12" |
| 157 | + |
| 158 | + - name: Install uv |
| 159 | + uses: astral-sh/setup-uv@v5 |
| 160 | + with: |
| 161 | + enable-cache: true |
| 162 | + |
| 163 | + - name: Install Rust |
| 164 | + uses: dtolnay/rust-toolchain@stable |
| 165 | + |
| 166 | + - name: Build source distribution |
| 167 | + uses: PyO3/maturin-action@v1 |
| 168 | + with: |
| 169 | + working-directory: crates/terraphim_automata_py |
| 170 | + command: sdist |
| 171 | + args: --out dist |
| 172 | + |
| 173 | + - name: Upload sdist artifact |
| 174 | + uses: actions/upload-artifact@v4 |
| 175 | + with: |
| 176 | + name: sdist |
| 177 | + path: crates/terraphim_automata_py/dist/*.tar.gz |
| 178 | + if-no-files-found: error |
| 179 | + |
| 180 | + test: |
| 181 | + name: Test Package |
| 182 | + runs-on: ${{ matrix.os }} |
| 183 | + needs: build |
| 184 | + strategy: |
| 185 | + fail-fast: false |
| 186 | + matrix: |
| 187 | + os: [ubuntu-latest, windows-latest, macos-latest] |
| 188 | + python-version: ['3.9', '3.10', '3.11', '3.12'] |
| 189 | + |
| 190 | + steps: |
| 191 | + - name: Checkout repository |
| 192 | + uses: actions/checkout@v4 |
| 193 | + |
| 194 | + - name: Set up Python |
| 195 | + uses: actions/setup-python@v5 |
| 196 | + with: |
| 197 | + python-version: ${{ matrix.python-version }} |
| 198 | + |
| 199 | + - name: Install uv |
| 200 | + uses: astral-sh/setup-uv@v5 |
| 201 | + with: |
| 202 | + enable-cache: true |
| 203 | + |
| 204 | + - name: Download test distributions |
| 205 | + uses: actions/download-artifact@v4 |
| 206 | + with: |
| 207 | + name: wheels-${{ matrix.os }}-py${{ matrix.python-version }} |
| 208 | + path: dist |
| 209 | + |
| 210 | + - name: Install test dependencies |
| 211 | + working-directory: crates/terraphim_automata_py |
| 212 | + run: | |
| 213 | + uv pip install --system pytest pytest-benchmark pytest-cov black mypy ruff |
| 214 | + uv pip install --system terraphim-automata --find-links=../../dist |
| 215 | +
|
| 216 | + - name: Run tests |
| 217 | + working-directory: crates/terraphim_automata_py |
| 218 | + run: | |
| 219 | + # Run Python tests |
| 220 | + python -m pytest python/tests/ -v --cov=terraphim_automata --cov-report=term-missing |
| 221 | +
|
| 222 | + # Test basic import |
| 223 | + python -c "import terraphim_automata; print('✅ Package imports successfully')" |
| 224 | +
|
| 225 | + publish-pypi: |
| 226 | + name: Publish to PyPI |
| 227 | + runs-on: [self-hosted, Linux, terraphim, production, docker] |
| 228 | + environment: production |
| 229 | + needs: [build, build-sdist, test] |
| 230 | + permissions: |
| 231 | + id-token: write |
| 232 | + steps: |
| 233 | + - name: Checkout repository |
| 234 | + uses: actions/checkout@v4 |
| 235 | + |
| 236 | + - name: Set up Python |
| 237 | + uses: actions/setup-python@v5 |
| 238 | + with: |
| 239 | + python-version: "3.12" |
| 240 | + |
| 241 | + - name: Install uv |
| 242 | + uses: astral-sh/setup-uv@v5 |
| 243 | + with: |
| 244 | + enable-cache: true |
| 245 | + |
| 246 | + - name: Install Rust |
| 247 | + uses: dtolnay/rust-toolchain@stable |
| 248 | + |
| 249 | + - name: Install 1Password CLI |
| 250 | + uses: 1password/install-cli-action@v1.1.0 |
| 251 | + |
| 252 | + - name: Authenticate with 1Password |
| 253 | + run: | |
| 254 | + echo "${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}" | op account add --service-account-token |
| 255 | +
|
| 256 | + - name: Get PyPI token from 1Password (or use secret) |
| 257 | + id: token |
| 258 | + run: | |
| 259 | + TOKEN=$(op read "op://TerraphimPlatform/pypi.token/token" 2>/dev/null || echo "") |
| 260 | + if [[ -z "$TOKEN" ]]; then |
| 261 | + echo "⚠️ PyPI token not found in 1Password, using GitHub secret" |
| 262 | + TOKEN="${{ secrets.PYPI_API_TOKEN }}" |
| 263 | + fi |
| 264 | + echo "token=$TOKEN" >> $GITHUB_OUTPUT |
| 265 | + echo "✅ PyPI token retrieved" |
| 266 | +
|
| 267 | + - name: Download all artifacts |
| 268 | + uses: actions/download-artifact@v4 |
| 269 | + with: |
| 270 | + path: dist |
| 271 | + |
| 272 | + - name: Collect distributions |
| 273 | + run: | |
| 274 | + mkdir -p packages |
| 275 | + find dist -name "*.whl" -exec cp {} packages/ \; |
| 276 | + find dist -name "*.tar.gz" -exec cp {} packages/ \; |
| 277 | + echo "📦 Found packages:" |
| 278 | + ls -la packages/ |
| 279 | +
|
| 280 | + - name: Validate distributions |
| 281 | + run: | |
| 282 | + python -m pip install --upgrade twine |
| 283 | + python -m twine check packages/* |
| 284 | + echo "✅ All distributions are valid" |
| 285 | +
|
| 286 | + - name: Set publishing repository |
| 287 | + id: repo |
| 288 | + run: | |
| 289 | + REPOSITORY="${{ inputs.repository }}" |
| 290 | + if [[ "$REPOSITORY" == "testpypi" ]]; then |
| 291 | + TWINE_REPOSITORY_URL="https://test.pypi.org/legacy/" |
| 292 | + echo "🧪 Publishing to TestPyPI" |
| 293 | + else |
| 294 | + TWINE_REPOSITORY_URL="https://upload.pypi.org/legacy/" |
| 295 | + echo "🚀 Publishing to production PyPI" |
| 296 | + fi |
| 297 | + echo "url=$TWINE_REPOSITORY_URL" >> $GITHUB_OUTPUT |
| 298 | +
|
| 299 | + - name: Publish to PyPI |
| 300 | + run: | |
| 301 | + if [[ "${{ inputs.dry_run }}" == "true" ]]; then |
| 302 | + echo "🧪 Dry run mode - validating packages only" |
| 303 | + python -m twine upload --repository-url ${{ steps.repo.outputs.url }} --username __token__ --password ${{ steps.token.outputs.token }} --skip-existing --dry-run packages/* |
| 304 | + else |
| 305 | + echo "🚀 Publishing to PyPI..." |
| 306 | + python -m twine upload --repository-url ${{ steps.repo.outputs.url }} --username __token__ --password ${{ steps.token.outputs.token }} --skip-existing packages/* |
| 307 | + echo "✅ Packages published successfully!" |
| 308 | + fi |
| 309 | +
|
| 310 | + - name: Verify published packages |
| 311 | + if: inputs.dry_run != 'true' |
| 312 | + run: | |
| 313 | + # Wait for package to be available |
| 314 | + sleep 60 |
| 315 | +
|
| 316 | + PACKAGE_NAME="terraphim-automata" |
| 317 | + PACKAGE_VERSION=$(python -c "import tomllib; pkg = tomllib.load(open('crates/terraphim_automata_py/pyproject.toml', 'rb')); print(pkg['project']['version'])") |
| 318 | +
|
| 319 | + echo "🔍 Verifying package on PyPI..." |
| 320 | + python -m pip install --upgrade pip |
| 321 | +
|
| 322 | + # Try to install from PyPI (or TestPyPI) |
| 323 | + if [[ "${{ inputs.repository }}" == "testpypi" ]]; then |
| 324 | + python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ "$PACKAGE_NAME==$PACKAGE_VERSION" || echo "⚠️ Package not yet visible on TestPyPI" |
| 325 | + else |
| 326 | + python -m pip install "$PACKAGE_NAME==$PACKAGE_VERSION" || echo "⚠️ Package not yet visible on PyPI" |
| 327 | + fi |
| 328 | +
|
| 329 | + echo "📊 Package verification complete" |
| 330 | +
|
| 331 | + - name: Create GitHub Release |
| 332 | + if: startsWith(github.ref, 'refs/tags/') && inputs.dry_run != 'true' |
| 333 | + uses: actions/create-release@v1 |
| 334 | + env: |
| 335 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 336 | + with: |
| 337 | + tag_name: ${{ github.ref }} |
| 338 | + release_name: "terraphim-automata ${{ github.ref_name }}" |
| 339 | + body: | |
| 340 | + ## Python Package Release |
| 341 | +
|
| 342 | + **Package**: `terraphim-automata` |
| 343 | + **Version**: ${{ github.ref_name }} |
| 344 | + **Repository**: ${{ inputs.repository }} |
| 345 | +
|
| 346 | + ### 🚀 Installation |
| 347 | + ```bash |
| 348 | + pip install terraphim-automata |
| 349 | + ``` |
| 350 | +
|
| 351 | + or for development: |
| 352 | + ```bash |
| 353 | + pip install terraphim-automata[dev] |
| 354 | + ``` |
| 355 | +
|
| 356 | + ### ✨ Features |
| 357 | + - **Fast Autocomplete**: Sub-millisecond prefix search |
| 358 | + - **Knowledge Graph Integration**: Semantic connectivity analysis |
| 359 | + - **Native Performance**: Rust backend with PyO3 bindings |
| 360 | + - **Cross-Platform**: Linux, macOS, Windows support |
| 361 | + - **Python 3.9+**: Modern Python support |
| 362 | +
|
| 363 | + ### 📊 Performance |
| 364 | + - **Autocomplete Index**: ~749 bytes |
| 365 | + - **Knowledge Graph**: ~856 bytes |
| 366 | + - **Native Extension**: Optimized binary wheels |
| 367 | +
|
| 368 | + ### 🔗 Links |
| 369 | + - [PyPI package](https://pypi.org/project/terraphim-automata) |
| 370 | + - [Documentation](https://github.com/terraphim/terraphim-ai/tree/main/crates/terraphim_automata_py) |
| 371 | +
|
| 372 | + --- |
| 373 | + 🤖 Generated on: $(date) |
| 374 | + draft: false |
| 375 | + prerelease: ${{ contains(github.ref, '-alpha') || contains(github.ref, '-beta') || contains(github.ref, '-rc') }} |
| 376 | + |
| 377 | + - name: Notify completion |
| 378 | + if: inputs.dry_run != 'true' |
| 379 | + run: | |
| 380 | + echo "🎉 PyPI publishing workflow completed successfully!" |
| 381 | + echo "📦 Package: terrraphim-automata" |
| 382 | + echo "📋 Repository: ${{ inputs.repository }}" |
0 commit comments