apexbase 1.28.0

High-performance HTAP embedded database with Rust core
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
name: Build and Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Existing Git tag to build and publish as a release (e.g., v1.0.0)'
        required: true
        type: string

concurrency:
  group: release-${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }}
  cancel-in-progress: false

env:
  # The repo's local Cargo config uses target-cpu=native for developer and
  # benchmark builds. CI builds must stay portable because GitHub runners and
  # restored proc-macro artifacts can land on hosts with different CPU features.
  RUSTFLAGS: "-C target-cpu=generic"
  APEXBASE_CI_CPU: "generic"
  REGISTRY_USER_AGENT: "apexbase-release-workflow/1.0 (https://github.com/BirchKwok/ApexBase)"

jobs:
  # Resolve the requested tag before choosing the legacy Python or maturin path.
  resolve-release:
    runs-on: ubuntu-latest
    outputs:
      tag_name: ${{ steps.inspect.outputs.tag_name }}
      package_kind: ${{ steps.inspect.outputs.package_kind }}
      rust_features: ${{ steps.inspect.outputs.rust_features }}
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
    - name: Inspect release tag
      id: inspect
      env:
        RELEASE_TAG: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }}
      shell: bash
      run: |
        if [[ ! "$RELEASE_TAG" =~ ^v[0-9]+(\.[0-9]+)*([-.][0-9A-Za-z]+)*$ ]]; then
          echo "Release tag must look like v0.0.1; got '$RELEASE_TAG'."
          exit 1
        fi
        git rev-parse "refs/tags/$RELEASE_TAG^{commit}" >/dev/null
        git checkout --detach "refs/tags/$RELEASE_TAG"
        python - <<'PY'
        import json
        import os
        import sys
        import tomllib
        from pathlib import Path

        tag = os.environ["RELEASE_TAG"]
        pyproject = Path("pyproject.toml")
        if not pyproject.exists():
            sys.exit("The release tag has no pyproject.toml.")
        project = tomllib.loads(pyproject.read_text(encoding="utf-8"))
        version = project.get("project", {}).get("version")
        backend = project.get("build-system", {}).get("build-backend", "")
        if version != tag.removeprefix("v"):
            sys.exit(f"Package version {version!r} does not match tag {tag!r}.")
        package_kind = "maturin" if "maturin" in backend else "legacy"
        rust_features = [""]
        cargo_file = Path("Cargo.toml")
        if package_kind == "maturin" and cargo_file.exists():
            cargo = tomllib.loads(cargo_file.read_text(encoding="utf-8"))
            cargo_version = cargo.get("package", {}).get("version")
            if cargo_version != version:
                sys.exit(
                    f"Cargo package version {cargo_version!r} does not match "
                    f"Python package version {version!r}."
                )
            available_features = cargo.get("features", {})
            rust_features.extend(
                feature for feature in ("server", "flight")
                if feature in available_features
            )
        with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
            print(f"tag_name={tag}", file=output)
            print(f"package_kind={package_kind}", file=output)
            print(f"rust_features={json.dumps(rust_features)}", file=output)
        PY

  # ── Rust tests (core lib without PyO3, plus server/flight features) ──
  rust-test:
    needs: resolve-release
    if: needs.resolve-release.outputs.package_kind == 'maturin'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        features: ${{ fromJSON(needs.resolve-release.outputs.rust_features) }}
        rust: [stable]
      fail-fast: false

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
        ref: ${{ github.event.inputs.tag || github.ref }}

    - name: Install Rust ${{ matrix.rust }}
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: ${{ matrix.rust }}

    - name: Rust cache
      uses: Swatinem/rust-cache@v2
      with:
        key: ${{ matrix.os }}-${{ matrix.features }}-${{ env.APEXBASE_CI_CPU }}

    - name: Install system dependencies (Linux)
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update
        sudo apt-get install -y protobuf-compiler

    - name: Install system dependencies (macOS)
      if: runner.os == 'macOS'
      run: |
        brew install protobuf

    - name: Install system dependencies (Windows)
      if: runner.os == 'Windows'
      run: |
        choco install protoc -y

    - name: Validate Rust (${{ matrix.features && format('{0} feature', matrix.features) || 'core only' }})
      shell: bash
      run: |
        if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
          # Historical release backfills must validate the publishable library,
          # not recompile old test targets with today's Rust/dependency graph.
          cargo check --lib --no-default-features ${{ matrix.features && format('--features {0}', matrix.features) || '' }} --release
        else
          cargo test --no-default-features ${{ matrix.features && format('--features {0}', matrix.features) || '' }} --release
        fi
      env:
        RUST_BACKTRACE: 1

  # ── Python tests (existing) ──
  test:
    needs: resolve-release
    if: needs.resolve-release.outputs.package_kind == 'maturin'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
      fail-fast: false

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
        ref: ${{ github.event.inputs.tag || github.ref }}

    - name: Install Rust stable
      uses: dtolnay/rust-toolchain@stable

    - name: Rust cache
      uses: Swatinem/rust-cache@v2
      with:
        key: python-${{ matrix.os }}-${{ matrix.python-version }}-${{ env.APEXBASE_CI_CPU }}

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install system dependencies (Linux)
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update
        sudo apt-get install -y protobuf-compiler

    - name: Install system dependencies (macOS)
      if: runner.os == 'macOS'
      run: |
        brew install protobuf

    - name: Install system dependencies (Windows)
      if: runner.os == 'Windows'
      run: |
        choco install protoc -y

    - name: Install dependencies
      shell: bash
      run: |
        python -m venv .venv
        python - <<'PY'
        import os
        import pathlib
        import subprocess
        import sys

        venv = pathlib.Path(".venv").resolve()
        bin_dir = venv / ("Scripts" if os.name == "nt" else "bin")
        python = bin_dir / ("python.exe" if os.name == "nt" else "python")
        env = os.environ.copy()
        env["VIRTUAL_ENV"] = str(venv)
        env["PATH"] = str(bin_dir) + os.pathsep + env.get("PATH", "")

        subprocess.check_call([str(python), "-m", "pip", "install", "--upgrade", "pip"])
        # Keep the native-extension test stack pinned. The pytest suite loads
        # ApexBase, NumPy, PyArrow, pandas, and Polars in the same process; using
        # floating latest wheels has produced macOS native hangs during pytest.
        # Newer NumPy/PyArrow/pandas/Polars releases have dropped older Python
        # versions at different points, so pin by interpreter instead of using
        # one native stack for the whole 3.9-3.13 matrix.
        test_deps = [
            "pytest==8.4.2",
            "pytest-timeout==2.4.0",
            "maturin==1.9.1",
        ]
        if sys.version_info < (3, 10):
            test_deps.extend([
                "numpy==2.0.2",
                "pyarrow==17.0.0",
                "pandas==2.3.2",
                "polars==1.36.1",
            ])
        elif sys.version_info < (3, 11):
            test_deps.extend([
                "numpy==2.1.3",
                "pyarrow==17.0.0",
                "pandas==2.3.2",
                "polars==1.39.3",
            ])
        else:
            test_deps.extend([
                "numpy==2.1.3",
                "pyarrow==23.0.1",
                "pandas==3.0.1",
                "polars==1.39.3",
            ])
        print("Installing test dependencies:")
        for dep in test_deps:
            print(f"  {dep}")
        subprocess.check_call([str(python), "-m", "pip", "install", *test_deps])
        subprocess.check_call([str(python), "-m", "maturin", "develop", "--release", "--skip-install"], env=env)
        smoke = """
        import sys
        import tempfile
        sys.path.insert(0, 'apexbase/python')
        import apexbase
        import apexbase._core as core
        print(apexbase.__file__)
        print(core.__file__)
        with tempfile.TemporaryDirectory() as tmpdir:
            client = apexbase.ApexClient(dirpath=tmpdir)
            client.create_table('smoke')
            client.close()
        """
        subprocess.check_call([
            str(python),
            "-c",
            smoke,
        ], env=env)

        with open(os.environ["GITHUB_PATH"], "a", encoding="utf-8") as path_file:
            path_file.write(str(bin_dir) + os.linesep)
        with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as env_file:
            env_file.write(f"VIRTUAL_ENV={venv}{os.linesep}")
        PY

    - name: Run tests
      timeout-minutes: 10
      shell: bash
      run: |
        python - <<'PY'
        import sys
        from pathlib import Path

        sys.path.insert(0, str(Path("apexbase/python").resolve()))

        import apexbase
        import apexbase._core as core
        import numpy
        import pandas
        import polars
        import pyarrow
        import pytest

        print(f"python={sys.version}")
        print(f"apexbase={apexbase.__file__}")
        print(f"apexbase_core={core.__file__}")
        print(f"pytest={pytest.__version__}")
        print(f"numpy={numpy.__version__}")
        print(f"pyarrow={pyarrow.__version__}")
        print(f"pandas={pandas.__version__}")
        print(f"polars={polars.__version__}")
        PY
        python -X faulthandler -m pytest \
          --ignore=test/test_memory_vs_duckdb.py \
          --timeout=30 \
          --durations=40
      env:
        PYTHONFAULTHANDLER: "1"
        RUST_BACKTRACE: "1"

  # ── Build Linux wheels (manylinux) ──
  build-wheels-linux:
    needs: [resolve-release, test, rust-test]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
        ref: ${{ github.event.inputs.tag || github.ref }}

    - name: Build wheels (manylinux)
      uses: PyO3/maturin-action@v1
      env:
        RUSTFLAGS: ${{ env.RUSTFLAGS }}
      with:
        maturin-version: v1.9.1
        command: build
        args: --release --out dist --interpreter /opt/python/cp39-cp39/bin/python /opt/python/cp310-cp310/bin/python /opt/python/cp311-cp311/bin/python /opt/python/cp312-cp312/bin/python /opt/python/cp313-cp313/bin/python
        manylinux: 2014

    - name: Upload wheel artifacts
      uses: actions/upload-artifact@v4
      with:
        name: wheels-manylinux
        path: dist/*.whl

  # ── Build macOS / Windows wheels ──
  build-wheels:
    needs: [resolve-release, test, rust-test]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [windows-latest, macos-latest]
        python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
      fail-fast: false
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
        ref: ${{ github.event.inputs.tag || github.ref }}

    - name: Set up Python ${{ matrix.python-version }}
      id: py
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}

    - name: Show Python
      run: |
        python -c "import sys; print(sys.executable); print(sys.version)"

    - name: Build wheels
      uses: PyO3/maturin-action@v1
      env:
        RUSTFLAGS: ${{ env.RUSTFLAGS }}
        PYO3_PYTHON: ${{ steps.py.outputs.python-path }}
        PYTHON_SYS_EXECUTABLE: ${{ steps.py.outputs.python-path }}
      with:
        maturin-version: v1.9.1
        command: build
        args: --release --out dist --interpreter ${{ steps.py.outputs.python-path }}

    - name: Smoke test wheel (ApexStorage init)
      if: matrix.os == 'macos-latest' && matrix.python-version == '3.12'
      shell: bash
      run: |
        python -m pip install --upgrade pip
        python -m pip install "$(ls dist/*.whl | head -1)"
        python - <<'PY'
        import sys
        import tempfile
        import os

        # Import path used by repro scripts / minimal integrations.
        from apexbase._core import ApexStorage

        db_dir = tempfile.mkdtemp(prefix="apexbase_wheel_smoke_")
        ApexStorage(os.path.join(db_dir, "apexbase.apex"))
        print("wheel smoke test OK", sys.version.split()[0])
        PY

    - name: Upload wheel artifacts
      uses: actions/upload-artifact@v4
      with:
        name: wheels-${{ matrix.os }}-${{ matrix.python-version }}
        path: dist/*.whl

  # ── Build source distribution ──
  build-sdist:
    needs: [resolve-release, test, rust-test]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
        ref: ${{ github.event.inputs.tag || github.ref }}
    - name: Build sdist
      uses: PyO3/maturin-action@v1
      with:
        maturin-version: v1.9.1
        command: sdist
        args: --out dist
    - name: Upload sdist artifact
      uses: actions/upload-artifact@v4
      with:
        name: sdist
        path: dist/*.tar.gz

  # ── Publish the Python and Rust packages behind one shared gate ──
  #
  # PyPI and crates.io do not support a cross-registry transaction. Keep both
  # uploads in one job, preflight both packages before either upload, and make
  # retries reconcile a release if a registry/network failure interrupted the
  # first attempt after only one registry accepted the version.
  publish-packages:
    needs: [resolve-release, test, rust-test, build-wheels-linux, build-wheels, build-sdist]
    if: needs.resolve-release.outputs.package_kind == 'maturin'
    runs-on: ubuntu-latest
    env:
      RELEASE_TAG: ${{ needs.resolve-release.outputs.tag_name }}
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
        ref: ${{ needs.resolve-release.outputs.tag_name }}

    - name: Install Rust stable
      uses: dtolnay/rust-toolchain@stable

    - name: Rust cache
      uses: Swatinem/rust-cache@v2

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: "3.11"

    - name: Install twine
      run: |
        python -m pip install --upgrade pip
        python -m pip install twine

    - name: Download artifacts
      uses: actions/download-artifact@v4
      with:
        path: dist

    - name: Validate Python distributions
      shell: bash
      run: |
        set -euo pipefail
        mapfile -d '' distributions < <(
          find dist -type f \( -name '*.whl' -o -name '*.tar.gz' \) -print0
        )
        if [[ "${#distributions[@]}" -eq 0 ]]; then
          echo "No Python distributions were downloaded."
          exit 1
        fi
        python -m twine check "${distributions[@]}"

    - name: Check registry state
      id: registry-state
      shell: bash
      run: |
        set -euo pipefail
        VERSION="${RELEASE_TAG#v}"

        registry_status() {
          local registry="$1"
          local url="$2"
          local status
          status="$(
            curl --silent --show-error \
              --user-agent "$REGISTRY_USER_AGENT" \
              --output /dev/null \
              --write-out '%{http_code}' \
              "$url"
          )"
          case "$status" in
            200)
              echo "${registry}_exists=true" >> "$GITHUB_OUTPUT"
              ;;
            404)
              echo "${registry}_exists=false" >> "$GITHUB_OUTPUT"
              ;;
            *)
              echo "Unexpected HTTP ${status} while checking ${registry}."
              exit 1
              ;;
          esac
        }

        registry_status crate "https://crates.io/api/v1/crates/apexbase/${VERSION}"
        registry_status pypi "https://pypi.org/pypi/apexbase/${VERSION}/json"

    - name: Validate crate package
      if: steps.registry-state.outputs.crate_exists != 'true'
      run: cargo publish --dry-run --no-default-features --allow-dirty

    - name: Publish missing packages together
      shell: bash
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        CRATE_EXISTS: ${{ steps.registry-state.outputs.crate_exists }}
        PYPI_EXISTS: ${{ steps.registry-state.outputs.pypi_exists }}
      run: |
        set -euo pipefail

        cargo_pid=""
        pypi_pid=""

        if [[ "$CRATE_EXISTS" != "true" ]]; then
          cargo publish --no-default-features --allow-dirty --no-verify &
          cargo_pid="$!"
        else
          echo "The crate version is already published; no crates.io upload is needed."
        fi

        if [[ "$PYPI_EXISTS" != "true" ]]; then
          (
            mapfile -d '' distributions < <(
              find dist -type f \( -name '*.whl' -o -name '*.tar.gz' \) -print0
            )
            python -m twine upload --verbose "${distributions[@]}"
          ) &
          pypi_pid="$!"
        else
          echo "The Python package version is already published; no PyPI upload is needed."
        fi

        cargo_status=0
        pypi_status=0
        if [[ -n "$cargo_pid" ]]; then
          if wait "$cargo_pid"; then
            echo "crates.io upload completed."
          else
            cargo_status="$?"
          fi
        fi
        if [[ -n "$pypi_pid" ]]; then
          if wait "$pypi_pid"; then
            echo "PyPI upload completed."
          else
            pypi_status="$?"
          fi
        fi

        if [[ "$cargo_status" -ne 0 || "$pypi_status" -ne 0 ]]; then
          echo "Package publication failed: crates.io=${cargo_status}, PyPI=${pypi_status}."
          echo "Rerun this workflow for the same tag to publish whichever side is missing."
          exit 1
        fi

    - name: Confirm both registries contain the version
      shell: bash
      run: |
        set -euo pipefail
        VERSION="${RELEASE_TAG#v}"

        for attempt in {1..12}; do
          crate_status="$(
            curl --silent --show-error \
              --user-agent "$REGISTRY_USER_AGENT" \
              --output /dev/null \
              --write-out '%{http_code}' \
              "https://crates.io/api/v1/crates/apexbase/${VERSION}" || true
          )"
          pypi_status="$(
            curl --silent --show-error \
              --user-agent "$REGISTRY_USER_AGENT" \
              --output /dev/null \
              --write-out '%{http_code}' \
              "https://pypi.org/pypi/apexbase/${VERSION}/json" || true
          )"
          if [[ "$crate_status" == "200" && "$pypi_status" == "200" ]]; then
            echo "apexbase ${VERSION} is available on both crates.io and PyPI."
            exit 0
          fi
          echo "Registry confirmation ${attempt}/12: crates.io=${crate_status}, PyPI=${pypi_status}."
          sleep 10
        done

        echo "The version was not confirmed on both registries."
        exit 1

  # ── Build and publish historical pure-Python releases ──
  build-legacy:
    needs: resolve-release
    if: needs.resolve-release.outputs.package_kind == 'legacy'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        ref: ${{ needs.resolve-release.outputs.tag_name }}
    - uses: actions/setup-python@v5
      with:
        python-version: "3.11"
    - name: Build pure-Python wheel and source distribution
      run: |
        python -m pip install --upgrade pip build
        python -m build --wheel --sdist --outdir dist
    - name: Upload legacy distributions
      uses: actions/upload-artifact@v4
      with:
        name: legacy-distributions
        path: |
          dist/*.whl
          dist/*.tar.gz
        if-no-files-found: error

  publish-legacy:
    needs: [resolve-release, build-legacy]
    if: needs.resolve-release.outputs.package_kind == 'legacy'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/download-artifact@v4
      with:
        name: legacy-distributions
        path: dist
    - name: Publish legacy package to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        packages-dir: dist
        password: ${{ secrets.PYPI_API_TOKEN }}
        skip-existing: true

  # ── Create GitHub Release ──
  create-release:
    needs: [resolve-release, publish-packages, publish-legacy]
    if: >-
      always() &&
      needs.resolve-release.result == 'success' &&
      (
        (
          needs.resolve-release.outputs.package_kind == 'maturin' &&
          needs.publish-packages.result == 'success' &&
          needs.publish-legacy.result == 'skipped'
        ) ||
        (
          needs.resolve-release.outputs.package_kind == 'legacy' &&
          needs.publish-packages.result == 'skipped' &&
          needs.publish-legacy.result == 'success'
        )
      )
    runs-on: ubuntu-latest
    permissions:
      contents: write
    env:
      RELEASE_TAG: ${{ needs.resolve-release.outputs.tag_name }}
    steps:
    - name: Checkout release notes from the default branch
      uses: actions/checkout@v4
      with:
        ref: ${{ github.event.repository.default_branch }}

    - name: Download all artifacts
      uses: actions/download-artifact@v4
      with:
        path: all-artifacts

    - name: Prepare release assets
      run: |
        mkdir -p release-assets
        find all-artifacts -type f \( -name "*.whl" -o -name "*.tar.gz" \) -exec cp {} release-assets/ \;
        echo "Release assets prepared:"
        ls -lh release-assets/

    - name: Extract release notes for tag
      env:
        RELEASE_NOTES_FILE: docs/release-notes.md
      run: |
        python - <<'PY'
        import os
        import re
        import sys
        from pathlib import Path

        tag = os.environ["RELEASE_TAG"]
        source = Path(os.environ["RELEASE_NOTES_FILE"])
        if not source.exists():
            sys.exit(f"Release notes file not found: {source}")

        text = source.read_text(encoding="utf-8")
        heading = re.compile(
            rf"^##\s+(?:\[{re.escape(tag)}\]\([^)]+\)|{re.escape(tag)})\s*$",
            re.MULTILINE,
        )
        match = heading.search(text)
        if not match:
            sys.exit(f"No release notes section found for {tag} in {source}.")

        next_heading = re.search(r"^##\s+", text[match.end():], re.MULTILINE)
        end = match.end() + next_heading.start() if next_heading else len(text)
        body = text[match.end():end].strip()
        body = re.sub(r"\n---\s*$", "", body).strip()
        if not body:
            sys.exit(f"Release notes section for {tag} is empty.")

        Path("release-body.md").write_text(body + "\n", encoding="utf-8")
        print(f"Extracted release notes for {tag} from {source}")
        PY

    - name: Create GitHub Release with assets
      uses: softprops/action-gh-release@v2
      with:
        tag_name: ${{ env.RELEASE_TAG }}
        name: ApexBase ${{ env.RELEASE_TAG }}
        body_path: release-body.md
        files: release-assets/*
        fail_on_unmatched_files: true
        draft: false
        prerelease: ${{ contains(env.RELEASE_TAG, 'alpha') || contains(env.RELEASE_TAG, 'beta') || contains(env.RELEASE_TAG, 'rc') }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}