name: Release Ruby

on:
  workflow_call:
    inputs:
      source_sha:
        description: Exact source commit selected by the release workflow.
        required: true
        type: string
      release_tag:
        description: Release tag when publishing (empty on PR and build-only runs).
        required: false
        default: ""
        type: string

permissions:
  contents: read

env:
  # Preserve the macOS 14 host floor when building arm64-darwin gems on macOS 15.
  MACOSX_DEPLOYMENT_TARGET: "14.0"
  CARGO_TERM_COLOR: always
  CARGO_NET_RETRY: "10"
  CARGO_HTTP_TIMEOUT: "120"
  CARGO_HTTP_MULTIPLEXING: "false"
  CARGO_INCREMENTAL: "0"

# These jobs validate source installation and produce shipped precompiled
# platform gems on the Cargo release profile.
jobs:
  source-check:
    name: Validate Ruby source gem
    runs-on: ubuntu-22.04
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          ref: ${{ inputs.source_sha }}

      - uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1
        with:
          ruby-version: "4.0"

      - uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable

      - name: Download release CI tools
        uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
        with:
          name: ci-release-tools
          path: ${{ runner.temp }}/ci-release-tools

      - name: Install build dependencies
        run: |
          bash "$RUNNER_TEMP/ci-release-tools/install-apt-packages.sh" libcap-ng-dev
          gem install rake rake-compiler rb_sys test-unit --no-document

      - name: Test release archive validation
        run: ruby scripts/ci/test-ruby-release.rb

      - name: Check the release version
        env:
          RELEASE_TAG: ${{ inputs.release_tag }}
        run: |
          ruby -r ./sdk/ruby/lib/microsandbox/version -e '
            tag = ENV.fetch("RELEASE_TAG")
            abort "release tag #{tag} differs from Ruby SDK version #{Microsandbox::VERSION}" if !tag.empty? && tag != "v#{Microsandbox::VERSION}"'

      - name: Download agentd
        uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
        with:
          name: agentd-x86_64-linux-musl
          path: build/

      # The release's Rust crates do not exist in the registry yet. This
      # external Cargo patch exercises source installation without putting a
      # workspace dependency into the gem that will eventually be published.
      - name: Use checkout Rust SDK
        run: scripts/ci/ruby-sdk-checkout.sh prepare

      - name: Build and install candidate source gem
        working-directory: sdk/ruby
        env:
          MSB_EMBED_ARTIFACTS_DIR: ${{ github.workspace }}/build
        run: |
          rake version_check
          gem build microsandbox.gemspec
          gem install --local microsandbox-*.gem --no-document
          ruby --disable-gems -e 'require "rubygems"; require "microsandbox"; abort "native version mismatch" unless Microsandbox.version == Microsandbox::VERSION'

      - name: Restore standalone Ruby lockfile
        if: always()
        run: scripts/ci/ruby-sdk-checkout.sh restore

  platform-gem:
    name: Ruby platform gem (${{ matrix.platform }})
    runs-on: ${{ matrix.runner }}
    timeout-minutes: 60
    defaults:
      run:
        shell: bash
    strategy:
      fail-fast: false
      matrix:
        include:
          # The platform label carries no glibc version, so the build host sets
          # the compatibility floor (glibc 2.35, audited below).
          - platform: x86_64-linux-gnu
            runner: ubuntu-22.04
            agentd: x86_64
          - platform: aarch64-linux-gnu
            runner: ubuntu-22.04-arm
            agentd: aarch64
          - platform: arm64-darwin
            runner: macos-15
            agentd: aarch64
          # RubyInstaller uses MinGW-ucrt, so this needs the windows-gnu Rust
          # target rather than the MSVC target used by the rest of Windows CI.
          - platform: x64-mingw-ucrt
            runner: windows-latest
            agentd: x86_64
    env:
      RB_SYS_CARGO_PROFILE: release
      CARGO_TARGET_DIR: ${{ github.workspace }}/target/ruby-platform-${{ matrix.platform }}
      RB_SYS_CARGO_TARGET_DIR: ${{ github.workspace }}/target/ruby-platform-${{ matrix.platform }}
      MSB_EMBED_ARTIFACTS_DIR: ${{ github.workspace }}/build
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          ref: ${{ inputs.source_sha }}

      - uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable

      - uses: Swatinem/rust-cache@c8037a86dde8214a8310e9f54a8fc61ab362aa31 # v2
        with:
          workspaces: . -> target/ruby-platform-${{ matrix.platform }}
          shared-key: release-ruby-platform-${{ matrix.platform }}
          cache-bin: false
          cache-targets: true

      - name: Download release CI tools
        if: contains(matrix.platform, 'linux')
        uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
        with:
          name: ci-release-tools
          path: ${{ runner.temp }}/ci-release-tools

      - name: Install system dependencies
        if: contains(matrix.platform, 'linux')
        run: bash "$RUNNER_TEMP/ci-release-tools/install-apt-packages.sh" libcap-ng-dev

      - name: Add the windows-gnu Rust target
        if: contains(matrix.platform, 'mingw')
        run: rustup target add x86_64-pc-windows-gnu

      - name: Download agentd
        uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
        with:
          name: agentd-${{ matrix.agentd }}-linux-musl
          path: build/

      # Compile all supported Ruby ABIs in one job so shared Rust dependencies
      # build once per target platform.
      - uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1
        if: "!contains(matrix.platform, 'mingw')"
        with:
          ruby-version: "3.3"

      - uses: oxidize-rb/actions/setup-ruby-and-rust@ec90b4aa48795518d79be3f5cc478d1d7ccd02a8 # v1.5.0
        if: contains(matrix.platform, 'mingw')
        with:
          ruby-version: "3.3"
          cargo-cache: false

      - name: Build and stage Ruby 3.3 extension
        working-directory: sdk/ruby
        run: |
          gem install rake rake-compiler rb_sys --no-document
          rake cargo:patch_workspace version_check gem:stage

      - name: Assert the extension uses the checkout Rust SDK
        working-directory: sdk/ruby
        run: cargo tree --manifest-path ext/microsandbox/Cargo.toml --depth 1 | grep "sdk.rust)"

      - uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1
        if: "!contains(matrix.platform, 'mingw')"
        with:
          ruby-version: "3.4"

      - uses: oxidize-rb/actions/setup-ruby-and-rust@ec90b4aa48795518d79be3f5cc478d1d7ccd02a8 # v1.5.0
        if: contains(matrix.platform, 'mingw')
        with:
          ruby-version: "3.4"
          cargo-cache: false

      - name: Build and stage Ruby 3.4 extension
        working-directory: sdk/ruby
        run: |
          rm -f lib/microsandbox/microsandbox.*
          gem install rake rake-compiler rb_sys --no-document
          rake version_check gem:stage

      - uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1
        if: "!contains(matrix.platform, 'mingw')"
        with:
          ruby-version: "4.0"

      - uses: oxidize-rb/actions/setup-ruby-and-rust@ec90b4aa48795518d79be3f5cc478d1d7ccd02a8 # v1.5.0
        if: contains(matrix.platform, 'mingw')
        with:
          ruby-version: "4.0"
          cargo-cache: false

      - name: Build and stage Ruby 4.0 extension
        working-directory: sdk/ruby
        run: |
          rm -f lib/microsandbox/microsandbox.*
          gem install rake rake-compiler rb_sys --no-document
          rake version_check gem:stage

      - name: Restore standalone Ruby lockfile
        if: always()
        working-directory: sdk/ruby
        run: rake cargo:unpatch_workspace

      - name: Audit the Linux glibc symbol floor
        if: contains(matrix.platform, 'linux')
        run: |
          python3 scripts/ci/validate_linux_glibc.py \
            --max-version 2.35 \
            sdk/ruby/lib/microsandbox/3.3 \
            sdk/ruby/lib/microsandbox/3.4 \
            sdk/ruby/lib/microsandbox/4.0

      - name: Package the platform gem
        working-directory: sdk/ruby
        env:
          GEM_PLATFORM: ${{ matrix.platform }}
        run: |
          export SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
          rake gem:platform

      - name: Validate the packaged platform gem
        run: |
          VERSION=$(ruby -r ./sdk/ruby/lib/microsandbox/version -e 'print Microsandbox::VERSION')
          ruby scripts/ci/ruby-release.rb validate sdk/ruby/pkg "$VERSION" "${{ matrix.platform }}"

      - name: Smoke test platform gem (Ruby 4.0)
        run: |
          gem install --local sdk/ruby/pkg/microsandbox-*.gem --no-document
          # shellcheck disable=SC2016
          ruby -e 'require "microsandbox"
            loaded = $LOADED_FEATURES.grep(%r{microsandbox/\d+\.\d+/microsandbox\.(so|bundle)$})
            abort "precompiled ABI binary not used: #{$LOADED_FEATURES.grep(/microsandbox/).inspect}" if loaded.empty?
            abort "native/ruby version mismatch" unless Microsandbox.version == Microsandbox::VERSION'

      - uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1
        with:
          ruby-version: "3.4"

      - name: Smoke test platform gem (Ruby 3.4)
        run: |
          gem install --local sdk/ruby/pkg/microsandbox-*.gem --no-document
          # shellcheck disable=SC2016
          ruby -e 'require "microsandbox"
            loaded = $LOADED_FEATURES.grep(%r{microsandbox/\d+\.\d+/microsandbox\.(so|bundle)$})
            abort "precompiled ABI binary not used: #{$LOADED_FEATURES.grep(/microsandbox/).inspect}" if loaded.empty?
            abort "native/ruby version mismatch" unless Microsandbox.version == Microsandbox::VERSION'

      - uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1
        with:
          ruby-version: "3.3"

      - name: Smoke test platform gem (Ruby 3.3)
        run: |
          gem install --local sdk/ruby/pkg/microsandbox-*.gem --no-document
          # shellcheck disable=SC2016
          ruby -e 'require "microsandbox"
            loaded = $LOADED_FEATURES.grep(%r{microsandbox/\d+\.\d+/microsandbox\.(so|bundle)$})
            abort "precompiled ABI binary not used: #{$LOADED_FEATURES.grep(/microsandbox/).inspect}" if loaded.empty?
            abort "native/ruby version mismatch" unless Microsandbox.version == Microsandbox::VERSION'

      - name: Upload Ruby platform gem
        uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
        with:
          name: ruby-sdk-${{ matrix.platform }}
          path: sdk/ruby/pkg/*.gem
          if-no-files-found: error
          compression-level: 0

  platform-smoke-clean:
    name: Smoke Ruby platform gem (clean Linux container)
    needs: platform-gem
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
        with:
          name: ruby-sdk-x86_64-linux-gnu

      - name: Install and load the gem in a stock Ruby image
        run: |
          docker run --rm -v "$PWD:/gems" ruby:3.4-slim bash -ec 'gem install --local /gems/microsandbox-*.gem --no-document && ruby -e "require \"microsandbox\"
          loaded = \$LOADED_FEATURES.grep(%r{microsandbox/\d+\.\d+/microsandbox\.(so|bundle)\$})
          abort \"precompiled ABI binary not used: #{\$LOADED_FEATURES.grep(/microsandbox/).inspect}\" if loaded.empty?
          abort \"native/ruby version mismatch\" unless Microsandbox.version == Microsandbox::VERSION"'
