name: Ruby Tests

on:
  workflow_call:
    inputs:
      continuous-run:
        required: true
        description: "Boolean string denoting whether this run is continuous --
          empty string for presubmit, non-empty string for continuous."
        type: string
      safe-checkout:
        required: true
        description: "The SHA key for the commit we want to run over"
        type: string
      continuous-prefix:
        required: true
        description: "The string continuous-only tests should be prefixed with when displaying test
          results."
        type: string


permissions:
  contents: read

jobs:
  linux:
    strategy:
      fail-fast: false
      matrix:
        include:
          # Test both FFI and Native implementations on the highest and lowest
          # Ruby versions for CRuby and JRuby
          - { name: Ruby 3.2, ruby: ruby-3.2.6, ffi: NATIVE }
          - { name: Ruby 3.2, ruby: ruby-3.2.6, ffi: FFI }
          - { name: Ruby 3.3, ruby: ruby-3.3.6, continuous-only: true }
          - { name: Ruby 3.4, ruby: ruby-3.4.1, continuous-only: true }
          - { name: Ruby 4.0, ruby: ruby-4.0.0, ffi: NATIVE }
          - { name: Ruby 4.0, ruby: ruby-4.0.0, ffi: FFI }
          - { name: Ruby 4.0 bazel 8, ruby: ruby-4.0.0, ffi: NATIVE, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-ruby-4.0.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }
          - { name: Ruby 4.0 bazel 8, ruby: ruby-4.4.4, ffi: FFI, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-ruby-4.0.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }
          - { name: JRuby 9.4, ruby: jruby-9.4.9.0, ffi: NATIVE }
          - { name: JRuby 9.4, ruby: jruby-9.4.9.0, ffi: FFI }
          - { name: JRuby 9.4 bazel 8, ruby: jruby-9.4.9.0, ffi: NATIVE, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-jruby-9.4.9.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }
          - { name: JRuby 9.4 bazel 8, ruby: jruby-9.4.9.0, ffi: FFI, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-jruby-9.4.9.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }

    name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} Linux ${{ matrix.name }} ${{ matrix.ffi == 'FFI' && ' FFI' || '' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/checkout@v5
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Run tests
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/bazel-docker@v5
        with:
          image: ${{ matrix.image || format('us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:9.2.0-{0}-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ', matrix.ruby) }}
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: ruby_linux/${{ matrix.ruby }}
          bazel: test //ruby/... //ruby/tests:ruby_version --test_env=KOKORO_RUBY_VERSION --test_env=BAZEL=true ${{ matrix.ffi == 'FFI' && '--//ruby:ffi=enabled --test_env=PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }}
# Useful tool for troubleshooting, but the action introduces flakes as well,
# e.g. https://github.com/actions/upload-artifact/issues/569
#      - name: Archive log artifacts
#        if: ${{ matrix.presubmit || inputs.test-type == 'continuous' }}
#        uses: actions/upload-artifact@v6
#        with:
#          name: test-logs-${{ matrix.ruby }}_${{ matrix.ffi || 'NATIVE' }}
#          path: logs

  linux-32bit:
    name: Linux 32-bit
    runs-on: ubuntu-22-4core
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v5
        with:
          ref: ${{ inputs.safe-checkout }}

      - name: Cross compile protoc for i386
        id: cross-compile
        uses: protocolbuffers/protobuf-ci/cross-compile-protoc@v5
        with:
          image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:8.7.0-4d8e80ef93b0219fb907af9dd4596b92946995d8
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          architecture: linux-i386

      - name: Run tests
        uses: protocolbuffers/protobuf-ci/docker@v5
        with:
          image: i386/ruby:3.2.11-bookworm
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          command: >-
            /bin/bash -cex '
            gem install bundler -v 2.6.6;
            cd /workspace/ruby;
            bundle;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake;
            rake clobber_package gem;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake test'

  linux-aarch64:
    name: ${{ inputs.continuous-prefix || '' }} Linux aarch64
    runs-on: ubuntu-22-4core
    steps:
      - name: Checkout pending changes
        if: ${{ inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/checkout@v5
        with:
          ref: ${{ inputs.safe-checkout }}

      - name: Cross compile protoc for aarch64
        if: ${{ inputs.continuous-run }}
        id: cross-compile
        uses: protocolbuffers/protobuf-ci/cross-compile-protoc@v5
        with:
          image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:8.7.0-4d8e80ef93b0219fb907af9dd4596b92946995d8
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          architecture: linux-aarch64

      - name: Run tests
        if: ${{ inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/docker@v5
        with:
          image: ruby:3.2.11-bookworm
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          platform: linux/arm64
          command: >-
            /bin/bash -cex '
            gem install bundler -v 2.6.6;
            cd /workspace/ruby;
            bundle;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake;
            rake clobber_package gem;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake test'

  macos:
    strategy:
      fail-fast: false   # Don't cancel all jobs if one fails.
      matrix:
        include:
        # Test both FFI and Native implementations on the highest and lowest
        # Ruby versions for CRuby.
        # Quote versions numbers otherwise 3.0 will render as 3
        - { version: "3.2", ffi: NATIVE }
        - { version: "3.2", ffi: FFI }
        - { version: "3.3", continuous-only: true }
        - { version: "3.4", ffi: NATIVE }
        - { version: "3.4", ffi: FFI }
        - { version: "4.0", ffi: NATIVE }
        - { version: "4.0", ffi: FFI }

    name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} MacOS Ruby ${{ matrix.version }}${{ matrix.ffi == 'FFI' && ' FFI' || '' }}
    runs-on: macos-14
    steps:
      - name: Checkout pending changes
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/checkout@v5
        with:
          ref: ${{ inputs.safe-checkout }}

      - name: Pin Ruby version
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        uses: ruby/setup-ruby@6e5d382445ae5590b7449d8b3bc8cb1c2c27f617 # v1.317.0
        with:
          ruby-version: ${{ matrix.version }}

      - name: Validate version
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        run: ruby --version | grep ${{ matrix.version }} || (echo "Invalid Ruby version - $(ruby --version)" && exit 1)

      - name: Run tests
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/bazel@v5
        with:
          version: 9.2.0 # Bazel version
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: ruby_macos/${{ matrix.version }}
          bazel: test //ruby/... --test_env=KOKORO_RUBY_VERSION=${{ matrix.version }} --test_env=BAZEL=true ${{ matrix.ffi == 'FFI' && '--//ruby:ffi=enabled --test_env=PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }}

  # This test should always be skipped on presubmit
  test_ruby_gems:
    strategy:
      fail-fast: false
      matrix:
        include:
          # Test both FFI and Native implementations on the highest and lowest
          # Ruby versions for CRuby and JRuby.
          - { name: Ruby 3.2, ruby: ruby-3.2.6, ffi: NATIVE }
          - { name: Ruby 3.2, ruby: ruby-3.2.6, ffi: FFI }
          - { name: Ruby 3.3, ruby: ruby-3.3.6, continuous-only: true }
          - { name: Ruby 3.4, ruby: ruby-3.4.1, continuous-only: true }
          - { name: Ruby 4.0, ruby: ruby-4.0.0, ffi: NATIVE }
          - { name: Ruby 4.0, ruby: ruby-4.0.0, ffi: FFI }
          - { name: Ruby 4.0 bazel 8, ruby: ruby-4.0.0, ffi: NATIVE, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-ruby-4.0.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }
          - { name: Ruby 4.0 bazel 8, ruby: ruby-4.4.4, ffi: FFI, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-ruby-4.0.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }
          - { name: JRuby 9.4, ruby: jruby-9.4.9.0, ffi: NATIVE }
          - { name: JRuby 9.4, ruby: jruby-9.4.9.0, ffi: FFI, continuous-only: true }
          - { name: JRuby 9.4 bazel 8, ruby: jruby-9.4.9.0, ffi: NATIVE, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-jruby-9.4.9.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }
          - { name: JRuby 9.4 bazel 8, ruby: jruby-9.4.9.0, ffi: FFI, image: 'us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:8.7.0-jruby-9.4.9.0-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ' }
    name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} Install ${{ matrix.name }}${{ matrix.ffi == 'FFI' && ' FFI' || '' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/checkout@v5
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Run tests
        if: ${{ !matrix.continuous-only || inputs.continuous-run }}
        uses: protocolbuffers/protobuf-ci/bazel-docker@v5
        with:
          image: ${{ matrix.image || format('us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:9.2.0-{0}-5bb1a8fdfc30f8c21c1b38cf053d2db9fca865a7 ', matrix.ruby) }}
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: ruby_install/${{ matrix.ruby }}_${{ matrix.bazel }}
          bash: >
            bazel --version;
            ruby --version;
            ./regenerate_stale_files.sh $BAZEL_FLAGS;
            # TODO: Add --action_env=PATH to release builds before upgrade them to Bazel 9
            bazel build //ruby:release //:protoc ${{ matrix.ffi == 'FFI' && '--//ruby:ffi=enabled' || '' }} --action_env=PATH $BAZEL_FLAGS;
            gem install bazel-bin/ruby/google-protobuf-*;
            bazel-bin/protoc --proto_path=src --proto_path=ruby/tests --proto_path=ruby --ruby_out=ruby tests/test_import_proto2.proto;
            bazel-bin/protoc --proto_path=src --proto_path=ruby/tests --proto_path=ruby --ruby_out=ruby tests/basic_test.proto;
            ${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/basic.rb;
            ${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/implementation.rb
