Bazelify ruby runtime (#10525)

This uses https://github.com/protocolbuffers/rules_ruby to fully Bazelify our ruby runtime code.  The Rakefile is left in place for now and is still used by our aarch64 tests.  With the current implementation ruby behaves similarly to our python wrapper, which selects whatever version is installed in the system.  Future enhancements will allow for more hermetic builds via Bazel flags to pin a specific version

Closes #10525

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/10525 from mkruskal-google:rules_ruby 97fa1f70abea77ed9792e88f77ce605c971f4871
PiperOrigin-RevId: 499283908
diff --git a/.gitignore b/.gitignore
index ea95cc9..749ccf9e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -174,10 +174,10 @@
 ruby/tests/multi_level_nesting_test_pb.rb
 ruby/tests/test_import_proto2_pb.rb
 ruby/tests/test_ruby_package_proto2_pb.rb
-ruby/Gemfile.lock
 ruby/compatibility_tests/v3.0.0/protoc
 ruby/compatibility_tests/v3.0.0/tests/generated_code_pb.rb
 ruby/compatibility_tests/v3.0.0/tests/test_import_pb.rb
+ruby/Gemfile.lock
 
 # IntelliJ CLion Config files and build output
 cmake/.idea
diff --git a/BUILD.bazel b/BUILD.bazel
index d51428c..49fba45 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -148,6 +148,7 @@
     name = "well_known_ruby_protos",
     srcs = [":well_known_protos"],
     includes = ["src"],
+    default_runtime = "",
     visibility = [
         "//conformance:__pkg__",
         "//ruby:__subpackages__",
@@ -519,7 +520,7 @@
         # The above must come first.
         "src",
     ],
-    proto_deps = [":well_known_protos"],
+    deps = [":well_known_ruby_protos"],
     visibility = [
         "//conformance:__pkg__",
         "//ruby:__subpackages__",
diff --git a/WORKSPACE b/WORKSPACE
index b10e0d0..36d4ee5 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -60,6 +60,9 @@
 load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
 rules_pkg_dependencies()
 
+load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies")
+apple_rules_dependencies()
+
 # For `kt_jvm_library`
 load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
 kotlin_repositories()
@@ -67,6 +70,17 @@
 load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
 kt_register_toolchains()
 
+load("@rules_ruby//ruby:defs.bzl", "ruby_runtime")
+ruby_runtime("system_ruby")
+register_toolchains("@system_ruby//:toolchain")
+
+load("@system_ruby//:bundle.bzl", "ruby_bundle")
+ruby_bundle(
+    name = "protobuf_bundle",
+    srcs = ["//ruby:google-protobuf.gemspec"],
+    gemfile = "//ruby:Gemfile",
+)
+
 load("@upb//bazel:workspace_deps.bzl", "upb_deps")
 upb_deps()
 
diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel
index 486d652..ac00795 100644
--- a/conformance/BUILD.bazel
+++ b/conformance/BUILD.bazel
@@ -1,6 +1,7 @@
 # Conformance testing for Protobuf.
 
 load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "objc_library")
+load("@rules_ruby//ruby:defs.bzl", "ruby_binary")
 load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library", "internal_ruby_proto_library")
 load("//build_defs:internal_shell.bzl", "inline_sh_binary")
 load(
@@ -317,19 +318,16 @@
     deps = [":conformance_objc_lib"],
 )
 
-inline_sh_binary(
+ruby_binary(
     name = "conformance_ruby",
-    testonly = 1,
+    testonly = True,
     srcs = ["conformance_ruby.rb"],
-    cmd = "RUBYLIB=ruby/lib:conformance:src $(rootpath conformance_ruby.rb)",
-    visibility = ["//ruby:__subpackages__"],
     deps = [
         ":conformance_ruby_proto",
         "//:test_messages_proto2_ruby_proto",
-        "//:test_messages_proto3_ruby_proto",
-        "//:well_known_ruby_protos",
-        "//ruby:protobuf",
+        "//:test_messages_proto3_ruby_proto",        
     ],
+    visibility = ["//ruby:__subpackages__"],
 )
 
 ################################################################################
diff --git a/conformance/conformance_ruby.rb b/conformance/conformance_ruby.rb
index 4af7659..0fa4339 100755
--- a/conformance/conformance_ruby.rb
+++ b/conformance/conformance_ruby.rb
@@ -30,7 +30,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-require 'conformance_pb'
+require 'conformance/conformance_pb'
 require 'google/protobuf/test_messages_proto3_pb'
 require 'google/protobuf/test_messages_proto2_pb'
 
diff --git a/kokoro/linux/jruby92/common.cfg b/kokoro/linux/jruby92/common.cfg
index 9db8f50..797011c 100644
--- a/kokoro/linux/jruby92/common.cfg
+++ b/kokoro/linux/jruby92/common.cfg
@@ -6,7 +6,7 @@
 
 env_vars {
   key: "CONTAINER_IMAGE"
-  value: "gcr.io/protobuf-build/ruby/linux:jruby-9.2.20.1-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
+  value: "gcr.io/protobuf-build/ruby/linux:jruby-9.2.20.1-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
 }
 
 env_vars {
@@ -14,11 +14,6 @@
   value: "//ruby/..."
 }
 
-env_vars {
-  key: "BAZEL_EXTRA_FLAGS"
-  value: "--define=ruby_platform=java"
-}
-
 action {
   define_artifacts {
     regex: "**/sponge_log.*"
diff --git a/kokoro/linux/jruby93/common.cfg b/kokoro/linux/jruby93/common.cfg
index a5a9bd2..8b70724 100644
--- a/kokoro/linux/jruby93/common.cfg
+++ b/kokoro/linux/jruby93/common.cfg
@@ -6,7 +6,7 @@
 
 env_vars {
   key: "CONTAINER_IMAGE"
-  value: "gcr.io/protobuf-build/ruby/linux:jruby-9.3.4.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
+  value: "gcr.io/protobuf-build/ruby/linux:jruby-9.3.4.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
 }
 
 env_vars {
@@ -14,11 +14,6 @@
   value: "//ruby/..."
 }
 
-env_vars {
-  key: "BAZEL_EXTRA_FLAGS"
-  value: "--define=ruby_platform=java"
-}
-
 action {
   define_artifacts {
     regex: "**/sponge_log.*"
diff --git a/kokoro/linux/ruby26/common.cfg b/kokoro/linux/ruby26/common.cfg
index d09a405..7db151c 100644
--- a/kokoro/linux/ruby26/common.cfg
+++ b/kokoro/linux/ruby26/common.cfg
@@ -6,7 +6,7 @@
 
 env_vars {
   key: "CONTAINER_IMAGE"
-  value: "gcr.io/protobuf-build/ruby/linux:ruby-2.6.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
+  value: "gcr.io/protobuf-build/ruby/linux:ruby-2.6.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
 }
 
 env_vars {
@@ -14,11 +14,6 @@
   value: "//ruby/..."
 }
 
-env_vars {
-  key: "BAZEL_EXTRA_FLAGS"
-  value: "--define=ruby_platform=c"
-}
-
 action {
   define_artifacts {
     regex: "**/sponge_log.*"
diff --git a/kokoro/linux/ruby27/common.cfg b/kokoro/linux/ruby27/common.cfg
index 51afa75..0b4f2e3 100644
--- a/kokoro/linux/ruby27/common.cfg
+++ b/kokoro/linux/ruby27/common.cfg
@@ -6,7 +6,7 @@
 
 env_vars {
   key: "CONTAINER_IMAGE"
-  value: "gcr.io/protobuf-build/ruby/linux:ruby-2.7.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
+  value: "gcr.io/protobuf-build/ruby/linux:ruby-2.7.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
 }
 
 env_vars {
@@ -14,11 +14,6 @@
   value: "//ruby/..."
 }
 
-env_vars {
-  key: "BAZEL_EXTRA_FLAGS"
-  value: "--define=ruby_platform=c"
-}
-
 action {
   define_artifacts {
     regex: "**/sponge_log.*"
diff --git a/kokoro/linux/ruby30/common.cfg b/kokoro/linux/ruby30/common.cfg
index 338505e..04ff4be 100644
--- a/kokoro/linux/ruby30/common.cfg
+++ b/kokoro/linux/ruby30/common.cfg
@@ -6,7 +6,7 @@
 
 env_vars {
   key: "CONTAINER_IMAGE"
-  value: "gcr.io/protobuf-build/ruby/linux:ruby-3.0.2-2f706fd1ab49f4e97af769388be486069b63efee"
+  value: "gcr.io/protobuf-build/ruby/linux:ruby-3.0.2-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
 }
 
 env_vars {
@@ -14,11 +14,6 @@
   value: "//ruby/..."
 }
 
-env_vars {
-  key: "BAZEL_EXTRA_FLAGS"
-  value: "--define=ruby_platform=c"
-}
-
 action {
   define_artifacts {
     regex: "**/sponge_log.*"
diff --git a/kokoro/linux/ruby31/common.cfg b/kokoro/linux/ruby31/common.cfg
index 588fe13..2de36aa 100644
--- a/kokoro/linux/ruby31/common.cfg
+++ b/kokoro/linux/ruby31/common.cfg
@@ -6,7 +6,7 @@
 
 env_vars {
   key: "CONTAINER_IMAGE"
-  value: "gcr.io/protobuf-build/ruby/linux:ruby-3.1.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
+  value: "gcr.io/protobuf-build/ruby/linux:ruby-3.1.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
 }
 
 env_vars {
@@ -14,11 +14,6 @@
   value: "//ruby/..."
 }
 
-env_vars {
-  key: "BAZEL_EXTRA_FLAGS"
-  value: "--define=ruby_platform=c"
-}
-
 action {
   define_artifacts {
     regex: "**/sponge_log.*"
diff --git a/kokoro/macos/ruby26/build.sh b/kokoro/macos/ruby26/build.sh
index ee815b6..a7a9e2d 100755
--- a/kokoro/macos/ruby26/build.sh
+++ b/kokoro/macos/ruby26/build.sh
@@ -5,9 +5,4 @@
 # Change to repo root
 cd $(dirname $0)/../../..
 
-# Prepare worker environment to run tests
-KOKORO_INSTALL_RVM=yes
-source kokoro/macos/prepare_build_macos_rc
-
-cd ruby
-./travis-test.sh ruby-2.6.0
+bash -l kokoro/macos/test_ruby.sh ruby-2.6.0
diff --git a/kokoro/macos/ruby27/build.sh b/kokoro/macos/ruby27/build.sh
index ca6badb..aa51776 100755
--- a/kokoro/macos/ruby27/build.sh
+++ b/kokoro/macos/ruby27/build.sh
@@ -5,9 +5,4 @@
 # Change to repo root
 cd $(dirname $0)/../../..
 
-# Prepare worker environment to run tests
-KOKORO_INSTALL_RVM=yes
-source kokoro/macos/prepare_build_macos_rc
-
-cd ruby
-./travis-test.sh ruby-2.7.0
+bash -l kokoro/macos/test_ruby.sh ruby-2.7.0
diff --git a/kokoro/macos/ruby30/build.sh b/kokoro/macos/ruby30/build.sh
index 01bdc47..1ccccb5 100755
--- a/kokoro/macos/ruby30/build.sh
+++ b/kokoro/macos/ruby30/build.sh
@@ -5,9 +5,4 @@
 # Change to repo root
 cd $(dirname $0)/../../..
 
-# Prepare worker environment to run tests
-KOKORO_INSTALL_RVM=yes
-source kokoro/macos/prepare_build_macos_rc
-
-cd ruby
-./travis-test.sh ruby-3.0.2
+bash -l kokoro/macos/test_ruby.sh ruby-3.0.2
diff --git a/kokoro/macos/ruby31/build.sh b/kokoro/macos/ruby31/build.sh
index 6e4beda..9f8bbd2 100644
--- a/kokoro/macos/ruby31/build.sh
+++ b/kokoro/macos/ruby31/build.sh
@@ -8,9 +8,4 @@
 # Fix locale issues in Monterey.
 export LC_ALL=en_US.UTF-8
 
-# Prepare worker environment to run tests
-KOKORO_INSTALL_RVM=yes
-source kokoro/macos/prepare_build_macos_rc
-
-cd ruby
-./travis-test.sh ruby-3.1.0
+bash -l kokoro/macos/test_ruby.sh ruby-3.1.0
diff --git a/kokoro/macos/test_ruby.sh b/kokoro/macos/test_ruby.sh
new file mode 100755
index 0000000..d6b9f4a
--- /dev/null
+++ b/kokoro/macos/test_ruby.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+set -eux
+
+VERSION=$1
+
+# Prepare worker environment to run tests
+KOKORO_INSTALL_RVM=yes
+source kokoro/macos/prepare_build_macos_rc
+
+# wget is required for ruby compatibility tests.
+brew install wget
+
+# Configure system ruby.
+# We need to disable unbound variable errors, due to a known issue described in
+# https://github.com/rvm/rvm/issues/4618.
+set +u
+rvm install $VERSION
+rvm use $VERSION
+rvm current | grep -qe "${RUBY_VERSION}.*" || exit 1;
+set -u
+
+# Run tests
+bazel test //ruby/... --test_env=KOKORO_RUBY_VERSION=$VERSION
diff --git a/protobuf.bzl b/protobuf.bzl
index 4ed08fc..f41b8f3 100644
--- a/protobuf.bzl
+++ b/protobuf.bzl
@@ -2,6 +2,7 @@
 load("@rules_cc//cc:defs.bzl", "objc_library")
 load("@rules_proto//proto:defs.bzl", "ProtoInfo")
 load("@rules_python//python:defs.bzl", "py_library")
+load("@rules_ruby//ruby:defs.bzl", "ruby_library")
 
 def _GetPath(ctx, path):
     if ctx.label.workspace_root:
@@ -485,6 +486,63 @@
         **kwargs
     )
 
+def internal_ruby_proto_library(
+        name,
+        srcs = [],
+        deps = [],
+        includes = ["."],
+        default_runtime = "@com_google_protobuf//ruby:protobuf",
+        protoc = "@com_google_protobuf//:protoc",
+        testonly = None,
+        visibility = ["//visibility:public"],
+        **kwargs):
+    """Bazel rule to create a Ruby protobuf library from proto source files
+
+    NOTE: the rule is only an internal workaround to generate protos. The
+    interface may change and the rule may be removed when bazel has introduced
+    the native rule.
+
+    Args:
+      name: the name of the ruby_proto_library.
+      srcs: the .proto files to compile.
+      deps: a list of dependency labels; must be a internal_ruby_proto_library.
+      includes: a string indicating the include path of the .proto files.
+      default_runtime: the RubyProtobuf runtime
+      protoc: the label of the protocol compiler to generate the sources.
+      testonly: common rule attribute (see:
+          https://bazel.build/reference/be/common-definitions#common-attributes)
+      visibility: the visibility of the generated files.
+      **kwargs: other keyword arguments that are passed to ruby_library.
+
+    """
+
+    # Note: we need to run the protoc build twice to get separate targets for
+    # the generated header and the source files.
+    _proto_gen(
+        name = name + "_genproto",
+        srcs = srcs,
+        deps = [s + "_genproto" for s in deps],
+        langs = ["ruby"],
+        includes = includes,
+        protoc = protoc,
+        testonly = testonly,
+        visibility = visibility,
+        tags = ["manual"],
+    )
+
+    deps = []
+    if default_runtime:
+        deps.append(default_runtime)
+    ruby_library(
+        name = name,
+        srcs = [name + "_genproto"],
+        deps = deps,
+        testonly = testonly,
+        visibility = visibility,
+        includes = includes,
+        **kwargs
+    )
+
 # When canonical labels are in use, use additional "@" prefix
 _canonical_label_prefix = "@" if str(Label("//:protoc")).startswith("@@") else ""
 
@@ -699,23 +757,6 @@
         **kwargs
     )
 
-def internal_ruby_proto_library(**kwargs):
-    """Bazel rule to create a Ruby protobuf library from proto source files
-
-    NOTE: the rule is only an internal workaround to generate protos. The
-    interface may change and the rule may be removed when bazel has introduced
-    the native rule.
-
-    Args:
-      **kwargs: arguments that are passed to unsupported_proto_library.
-
-    """
-
-    _source_proto_library(
-        lang = "ruby",
-        **kwargs
-    )
-
 def check_protobuf_required_bazel_version():
     """For WORKSPACE files, to check the installed version of bazel.
 
diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl
index 85c6b31..289a354 100644
--- a/protobuf_deps.bzl
+++ b/protobuf_deps.bzl
@@ -106,6 +106,14 @@
             url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.14.0.tar.gz",
         )
 
+    if not native.existing_rule("rules_ruby"):
+        _github_archive(
+            name = "rules_ruby",
+            repo = "https://github.com/protocolbuffers/rules_ruby",
+            commit = "59732544ce3a4bc4e8e4d4e8c8f318c931c17eae",
+            sha256 = "4fc45adf1056c824afde9a52b743b915eeada3633539eb1dbd641a66dc9f4c4f",
+        )
+
     if not native.existing_rule("rules_jvm_external"):
         _github_archive(
             name = "rules_jvm_external",
@@ -124,6 +132,13 @@
             sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2",
         )
 
+    if not native.existing_rule("build_bazel_rules_apple"):
+        http_archive(
+            name = "build_bazel_rules_apple",
+            sha256 = "f94e6dddf74739ef5cb30f000e13a2a613f6ebfa5e63588305a71fce8a8a9911",
+            url = "https://github.com/bazelbuild/rules_apple/releases/download/1.1.3/rules_apple.1.1.3.tar.gz",
+        )
+
     if not native.existing_rule("io_bazel_rules_kotlin"):
         http_archive(
             name = "io_bazel_rules_kotlin",
diff --git a/ruby/BUILD.bazel b/ruby/BUILD.bazel
index d4114b5..6095c1d 100644
--- a/ruby/BUILD.bazel
+++ b/ruby/BUILD.bazel
@@ -3,83 +3,19 @@
 # See also code generation logic under /src/google/protobuf/compiler/ruby.
 
 load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
-load("@upb//cmake:build_defs.bzl", "staleness_test")
-load("//build_defs:internal_shell.bzl", "inline_sh_test")
-load("//conformance:defs.bzl", "conformance_test")
+load("@rules_ruby//ruby:defs.bzl", "ruby_library")
 load("//:protobuf.bzl", "internal_ruby_proto_library")
-load(":internal.bzl", "internal_ruby_extension")
+load("//conformance:defs.bzl", "conformance_test")
 
 ################################################################################
 # Ruby Runtime
 ################################################################################
 
-config_setting(
-    name = "java_ruby",
-    values = {"define": "ruby_platform=java"},
-)
-
-config_setting(
-    name = "c_ruby",
-    values = {"define": "ruby_platform=c"},
-)
-
-filegroup(
-    name = "srcs",
-    srcs = glob([
-        "lib/**/*.rb",
-        "src/**/*.proto",
-    ]) + [
-        "Gemfile",
-        "Rakefile",
-        "google-protobuf.gemspec",
-        "pom.xml",
-    ],
-)
-
-internal_ruby_extension(
-    name = "protobuf_c_mac",
-    extension = "lib/google/protobuf_c.bundle",
-    deps = glob(["ext/google/protobuf_c/*"]),
-    target_compatible_with = select({
-        ":java_ruby": ["@platforms//:incompatible"],
-        "//conditions:default": ["@platforms//os:osx"],
-    }),
-)
-
-internal_ruby_extension(
-    name = "protobuf_c",
-    extension = "lib/google/protobuf_c.so",
-    deps = glob(["ext/google/protobuf_c/*"]),
-    target_compatible_with = select({
-        ":java_ruby": ["@platforms//:incompatible"],
-        "@platforms//os:osx": ["@platforms//:incompatible"],
-        "//conditions:default": [],
-    }),
-)
-
-internal_ruby_extension(
-    name = "protobuf_java",
-    extension = "lib/google/protobuf_java.jar",
-    deps = glob(["src/**/*.java"]),
-    target_compatible_with = select({
-        ":java_ruby": [],
-        "//conditions:default": ["@platforms//:incompatible"],
-    }),
-)
-
-filegroup(
+ruby_library(
     name = "protobuf",
-    srcs = [
-        ":srcs",
-        "@utf8_range//:utf8_range_srcs",
-    ] + select({
-        ":java_ruby": [":protobuf_java"],
-        "@bazel_tools//src/conditions:darwin": [":protobuf_c_mac"],
-        "//conditions:default": [":protobuf_c"],
-    }),
+    deps = ["//ruby/lib/google:protobuf_lib"],
     visibility = [
-        "//conformance:__subpackages__",
-        "//ruby:__subpackages__",
+        "//visibility:public",
     ],
 )
 
@@ -87,58 +23,17 @@
 # Tests
 ################################################################################
 
-filegroup(
-  name = "tests",
-  srcs = glob(["tests/*.rb"]),
-)
-
-filegroup(
-  name = "test_protos",
-  srcs = glob(["tests/*.proto"]),
-)
-
+# Define this here so the descriptor paths match what we get in Rake tests.
 internal_ruby_proto_library(
     name = "test_ruby_protos",
-    srcs = [":test_protos"],
-    proto_deps = ["//:well_known_protos"],
+    srcs = ["//ruby/tests:test_protos"],
+    deps = ["//:well_known_ruby_protos"],
     includes = [".", "src", "ruby/tests"],
+    visibility = [
+        "//ruby:__subpackages__",
+    ],
 )
 
-inline_sh_test(
-    name = "test",
-    srcs = [
-        "Rakefile",
-    ],
-    deps = [
-        ":protobuf",
-        ":test_ruby_protos",
-        ":tests",
-        "//:well_known_ruby_protos",
-    ],
-    cmd = """
-        pushd `dirname $(location Rakefile)`
-        RUBYLIB=../src:tests:. BAZEL=true rake test
-        popd
-    """,
-)
-
-inline_sh_test(
-    name = "gc_test",
-    srcs = [
-        "Rakefile",
-    ],
-    deps = [
-        ":protobuf",
-        ":test_ruby_protos",
-        ":tests",
-        "//:well_known_ruby_protos",
-    ],
-    cmd = """
-        pushd `dirname $(location Rakefile)`
-        RUBYLIB=../src:tests:. BAZEL=true rake gc_test
-        popd
-    """,
-)
 
 conformance_test(
     name = "conformance_test",
@@ -146,8 +41,8 @@
     testee = "//conformance:conformance_ruby",
     text_format_failure_list = "//conformance:text_format_failure_list_ruby.txt",
     target_compatible_with = select({
-        ":java_ruby": ["@platforms//:incompatible"],
-        "//conditions:default": [],
+        "@rules_ruby//ruby/runtime:config_ruby": [],
+        "//conditions:default": ["@platforms//:incompatible"],
     }),
 )
 
@@ -157,51 +52,27 @@
     testee = "//conformance:conformance_ruby",
     text_format_failure_list = "//conformance:text_format_failure_list_jruby.txt",
     target_compatible_with = select({
-        ":java_ruby": [],
+        "@rules_ruby//ruby/runtime:config_jruby": [],
         "//conditions:default": ["@platforms//:incompatible"],
     }),
 )
 
-genrule(
-    name = "copy_ruby_amalgamation_h",
-    srcs = ["@upb//:ruby-upb.h"],
-    outs = ["generated-in/ext/google/protobuf_c/ruby-upb.h"],
-    cmd = "cp $< $@",
-)
-
-genrule(
-    name = "copy_ruby_amalgamation_c",
-    srcs = ["@upb//:ruby-upb.c"],
-    outs = ["generated-in/ext/google/protobuf_c/ruby-upb.c"],
-    cmd = "cp $< $@",
-)
-
-staleness_test(
-    name = "test_amalgamation_staleness",
-    outs = [
-        "ext/google/protobuf_c/ruby-upb.h",
-        "ext/google/protobuf_c/ruby-upb.c",
-    ],
-    generated_pattern = "generated-in/%s",
-)
-
 ################################################################################
 # Distribution files
 ################################################################################
 
 pkg_files(
     name = "dist_files",
-    srcs = glob([
-        "tests/*.proto",
-        "tests/*.rb",
-    ]) + [
-        ":srcs",
+    srcs = [
+        "//ruby/ext/google/protobuf_c:dist_files",
+        "//ruby/lib/google:dist_files",
+        "//ruby/src/main/java:dist_files",
+        "//ruby/tests:dist_files",
         ".gitignore",
         "BUILD.bazel",
-        "internal.bzl",
         "Gemfile",
+        "Rakefile",
         "README.md",
-        "travis-test.sh",
     ],
     strip_prefix = strip_prefix.from_root(""),
     visibility = ["//pkg:__pkg__"],
diff --git a/ruby/compatibility_tests/v3.0.0/BUILD.bazel b/ruby/compatibility_tests/v3.0.0/BUILD.bazel
index b5fcede..d62c352 100644
--- a/ruby/compatibility_tests/v3.0.0/BUILD.bazel
+++ b/ruby/compatibility_tests/v3.0.0/BUILD.bazel
@@ -1,51 +1,29 @@
 load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
-load("//build_defs:internal_shell.bzl", "inline_sh_test")
-load("//:protobuf.bzl", "internal_ruby_proto_library")
-
-filegroup(
-  name = "tests",
-  srcs = glob(["tests/*.rb"]),
-)
-
-filegroup(
-  name = "test_protos",
-  srcs = glob(["tests/*.proto"]),
-)
-
-internal_ruby_proto_library(
-    name = "test_ruby_protos",
-    srcs = [":test_protos"],
-    includes = ["."],
-)
-
-inline_sh_test(
-    name = "test",
-    srcs = [
-        "Rakefile",
-    ],
-    deps = [
-        ":test_ruby_protos",
-        ":tests",
-        "//:well_known_ruby_protos",
-        "//ruby:protobuf",
-    ],
-    cmd = """
-        pushd `dirname $(location Rakefile)`
-        RUBYLIB=.:tests:../../lib:../../../src BAZEL=true rake test
-        popd
-    """,
-)
 
 ################################################################################
 # Distribution files
 ############################################################################
 
+genrule(
+    name = "protoc-compat-gen",
+    outs = ["protoc"],
+    cmd = """
+        PROTOC_BINARY_NAME="protoc-3.0.0-linux-x86_64.exe"
+        if [ `uname` = "Darwin" ]; then
+          PROTOC_BINARY_NAME="protoc-3.0.0-osx-x86_64.exe"
+        fi
+        wget https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0/$${PROTOC_BINARY_NAME} -O protoc
+        chmod +x protoc
+        mv protoc $@
+    """,
+    executable = True,
+    visibility = ["//ruby/compatibility_tests/v3.0.0:__subpackages__"],
+)
+
 pkg_files(
     name = "dist_files",
-    srcs = glob([
-        "**/*.rb",
-        "**/*.proto"
-    ]) + [
+    srcs = [
+        "//ruby/compatibility_tests/v3.0.0/tests:dist_files",
         "BUILD.bazel",
         "Rakefile",
         "README.md",
diff --git a/ruby/compatibility_tests/v3.0.0/Rakefile b/ruby/compatibility_tests/v3.0.0/Rakefile
index f72bed2..fee72b4 100644
--- a/ruby/compatibility_tests/v3.0.0/Rakefile
+++ b/ruby/compatibility_tests/v3.0.0/Rakefile
@@ -3,16 +3,14 @@
 # Proto for tests.
 genproto_output = []
 
-unless ENV['BAZEL'] == 'true'
-  genproto_output << "tests/generated_code.rb"
-  genproto_output << "tests/test_import.rb"
-  file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
-    sh "./protoc --ruby_out=. tests/generated_code.proto"
-  end
+genproto_output << "tests/generated_code.rb"
+genproto_output << "tests/test_import.rb"
+file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
+  sh "./protoc --ruby_out=. tests/generated_code.proto"
+end
 
-  file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
-    sh "./protoc --ruby_out=. tests/test_import.proto"
-  end
+file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
+  sh "./protoc --ruby_out=. tests/test_import.proto"
 end
 
 task :genproto => genproto_output
diff --git a/ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel b/ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel
new file mode 100644
index 0000000..e165b0b
--- /dev/null
+++ b/ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel
@@ -0,0 +1,64 @@
+load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
+load("@rules_ruby//ruby:defs.bzl", "ruby_test")
+load("//:protobuf.bzl", "internal_ruby_proto_library")
+
+internal_ruby_proto_library(
+    name = "test_ruby_protos",
+    srcs = glob(["*.proto"]),
+    includes = ["."],
+    protoc = "//ruby/compatibility_tests/v3.0.0:protoc",
+)
+
+ruby_test(
+    name = "basic",
+    deps = [
+        ":test_ruby_protos",
+        "//ruby:protobuf",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["basic.rb"],
+)
+
+ruby_test(
+    name = "generated_code_test",
+    deps = [
+        ":test_ruby_protos",
+        "//ruby:protobuf",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["generated_code_test.rb"],
+)
+
+ruby_test(
+    name = "repeated_field_test",
+    deps = [
+        ":test_ruby_protos",
+        "//ruby:protobuf",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["repeated_field_test.rb"],
+)
+
+ruby_test(
+    name = "stress",
+    deps = [
+        ":test_ruby_protos",
+        "//ruby:protobuf",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["stress.rb"],
+)
+
+################################################################################
+# Distribution files
+############################################################################
+
+pkg_files(
+    name = "dist_files",
+    srcs = glob([
+        "**/*.rb",
+        "**/*.proto"
+    ]),
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//ruby/compatibility_tests/v3.0.0:__pkg__"],
+)
diff --git a/ruby/ext/google/protobuf_c/BUILD.bazel b/ruby/ext/google/protobuf_c/BUILD.bazel
new file mode 100644
index 0000000..1a6b963
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/BUILD.bazel
@@ -0,0 +1,72 @@
+load("@build_bazel_rules_apple//apple:apple_binary.bzl", "apple_binary")
+load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
+load("@upb//cmake:build_defs.bzl", "staleness_test")
+
+package(default_visibility = ["//ruby:__subpackages__"])
+
+cc_library(
+    name = "protobuf_c",
+    srcs = glob([
+        "*.h",
+        "*.c",
+    ]),
+    deps = [
+        "@rules_ruby//ruby/runtime:headers",
+        "@utf8_range//:utf8_range",
+    ],
+    target_compatible_with = select({
+        "@rules_ruby//ruby/runtime:config_jruby": ["@platforms//:incompatible"],
+        "//conditions:default": [],
+    }),
+    linkstatic = True,
+    alwayslink = True,
+)
+
+apple_binary(
+    name = "bundle",
+    binary_type = "loadable_bundle",
+    linkopts = [
+        "-undefined,dynamic_lookup",
+        "-multiply_defined,suppress",
+    ],
+    platform_type = "macos",
+    minimum_os_version = "10.11",
+    tags = ["manual"],
+    deps = [
+        ":protobuf_c",
+    ],
+)
+
+pkg_files(
+    name = "dist_files",
+    srcs = glob([
+        "*.h",
+        "*.c",
+        "*.rb",
+    ]),
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//ruby:__pkg__"],
+)
+
+genrule(
+    name = "copy_ruby_amalgamation_h",
+    srcs = ["@upb//:ruby-upb.h"],
+    outs = ["generated-in/ruby-upb.h"],
+    cmd = "cp $< $@",
+)
+
+genrule(
+    name = "copy_ruby_amalgamation_c",
+    srcs = ["@upb//:ruby-upb.c"],
+    outs = ["generated-in/ruby-upb.c"],
+    cmd = "cp $< $@",
+)
+
+staleness_test(
+    name = "test_amalgamation_staleness",
+    outs = [
+        "ruby-upb.h",
+        "ruby-upb.c",
+    ],
+    generated_pattern = "generated-in/%s",
+)
diff --git a/ruby/google-protobuf.gemspec b/ruby/google-protobuf.gemspec
index a925fdc..a96b5e9 100644
--- a/ruby/google-protobuf.gemspec
+++ b/ruby/google-protobuf.gemspec
@@ -18,9 +18,6 @@
     s.files     += Dir.glob('ext/**/*')
     s.extensions= ["ext/google/protobuf_c/extconf.rb"]
     s.add_development_dependency "rake-compiler-dock", "= 1.2.1"  end
-  s.test_files  = ["tests/basic.rb",
-                  "tests/stress.rb",
-                  "tests/generated_code_test.rb"]
   s.required_ruby_version = '>= 2.3'
   s.add_development_dependency "rake-compiler", "~> 1.1.0"
   s.add_development_dependency "test-unit", '~> 3.0', '>= 3.0.9'
diff --git a/ruby/internal.bzl b/ruby/internal.bzl
deleted file mode 100644
index b7ec026..0000000
--- a/ruby/internal.bzl
+++ /dev/null
@@ -1,34 +0,0 @@
-def internal_ruby_extension(
-        name,
-        extension,
-        deps = [],
-        **kwargs):
-    """Bazel rule to wrap up a generated ruby extension.
-
-    NOTE: the rule is only an internal workaround. The interface may change and
-    the rule may be removed when everything is properly "Bazelified".
-
-    Args:
-      name: the name of the target.
-      extension: the path of the extension file.
-      deps: extra dependencies to add.
-      **kwargs: extra arguments to forward to the genrule.
-    """
-
-    native.genrule(
-        name = name,
-        srcs = deps + [
-            "Rakefile",
-            ":srcs",
-            ":test_ruby_protos",
-            ":tests",
-            "@utf8_range//:utf8_range_srcs",
-        ],
-        tags = ["manual"],
-        outs = [extension],
-        cmd = "pushd `dirname $(location Rakefile)`\n" +
-              "BAZEL=true rake\n" +
-              "popd\n" +
-              "cp `dirname $(location Rakefile)`/%s $(OUTS)\n" % extension,
-        **kwargs
-    )
diff --git a/ruby/lib/google/BUILD.bazel b/ruby/lib/google/BUILD.bazel
new file mode 100644
index 0000000..6c90959
--- /dev/null
+++ b/ruby/lib/google/BUILD.bazel
@@ -0,0 +1,62 @@
+load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
+load("@rules_ruby//ruby:defs.bzl", "ruby_library")
+
+config_setting(
+    name = "requires_bundle",
+    constraint_values = ["@platforms//os:osx"],
+)
+
+cc_binary(
+    name = "protobuf_c.so",
+    deps = ["//ruby/ext/google/protobuf_c"],
+    linkshared = 1,
+    tags = ["manual"],
+)
+
+# Move the bundle to the location expected by our Ruby files.
+genrule(
+    name = "copy_bundle",
+    srcs = ["//ruby/ext/google/protobuf_c:bundle"],
+    outs = ["protobuf_c.bundle"],
+    cmd = "cp $< $@",
+    tags = ["manual"],
+)
+
+java_binary(
+    name = "protobuf_java_bin",
+    runtime_deps = [
+        "//ruby/src/main/java:protobuf_java"
+    ],
+    create_executable = False,
+)
+
+# Move the jar to the location expected by our Ruby files.
+genrule(
+    name = "copy_jar",
+    srcs = ["protobuf_java_bin_deploy.jar"],
+    outs = ["protobuf_java.jar"],
+    cmd = "cp $< $@",
+    tags = ["manual"],
+)
+
+ruby_library(
+    name = "protobuf_lib",
+    srcs = glob([
+        "**/*.rb",
+    ]),
+    deps = ["//:well_known_ruby_protos"],
+    includes = ["ruby/lib"],
+    data = select({
+        "@rules_ruby//ruby/runtime:config_jruby": ["protobuf_java.jar"],
+        "@platforms//os:osx": ["protobuf_c.bundle"],
+        "//conditions:default": ["protobuf_c.so"],
+    }),
+    visibility = ["//ruby:__pkg__"],
+)
+
+pkg_files(
+    name = "dist_files",
+    srcs = glob(["**/*.rb"]),
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//ruby:__pkg__"],
+)
diff --git a/ruby/src/main/java/BUILD.bazel b/ruby/src/main/java/BUILD.bazel
new file mode 100644
index 0000000..34242d4
--- /dev/null
+++ b/ruby/src/main/java/BUILD.bazel
@@ -0,0 +1,43 @@
+load("@rules_java//java:defs.bzl", "java_library", "java_lite_proto_library", "java_proto_library")
+load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
+load("@rules_proto//proto:defs.bzl", "proto_library")
+
+java_library(
+    name = "protobuf_java",
+    srcs = glob([
+        "com/google/protobuf/jruby/*.java"
+    ]) + [
+        "google/ProtobufJavaService.java"
+    ],
+    deps = [
+        "@rules_ruby//ruby/runtime:jars",
+        "//java/core",
+        "//java/util",
+    ],
+    target_compatible_with = select({
+        "@rules_ruby//ruby/runtime:config_jruby": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    visibility = ["//ruby:__subpackages__"],
+)
+
+proto_library(
+    name = "sentinel_proto",
+    srcs = ["sentinel.proto"],
+)
+
+java_proto_library(
+    name = "sentinel_java_proto",
+    deps = [":sentinel_proto"],
+)
+
+
+pkg_files(
+    name = "dist_files",
+    srcs = glob([
+        "**/*.java",
+        "**/*.proto",
+    ]),
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//ruby:__pkg__"],
+)
diff --git a/ruby/src/main/java/sentinel.proto b/ruby/src/main/java/sentinel.proto
new file mode 100644
index 0000000..c25c1f3
--- /dev/null
+++ b/ruby/src/main/java/sentinel.proto
@@ -0,0 +1,47 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc.  All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto3";
+
+package com.google.protobuf.jruby;
+
+option optimize_for = CODE_SIZE;
+
+message Sentinel {
+  int32 default_int32 = 1;
+  int64 default_int64 = 2;
+  uint32 default_unit32 = 3;
+  uint64 default_uint64 = 4;
+  string default_string = 5;
+  bool default_bool = 6;
+  float default_float = 7;
+  double default_double = 8;
+  bytes default_bytes = 9;
+}
diff --git a/ruby/src/main/sentinel.proto b/ruby/src/main/sentinel.proto
deleted file mode 100644
index 722041b..0000000
--- a/ruby/src/main/sentinel.proto
+++ /dev/null
@@ -1,15 +0,0 @@
-syntax = "proto3";
-package com.google.protobuf.jruby;
-option optimize_for = CODE_SIZE;
-
-message Sentinel {
-  int32 default_int32 = 1;
-  int64 default_int64 = 2;
-  uint32 default_unit32 = 3;
-  uint64 default_uint64 = 4;
-  string default_string = 5;
-  bool default_bool = 6;
-  float default_float = 7;
-  double default_double = 8;
-  bytes default_bytes = 9;
-}
diff --git a/ruby/tests/BUILD.bazel b/ruby/tests/BUILD.bazel
new file mode 100644
index 0000000..220e691
--- /dev/null
+++ b/ruby/tests/BUILD.bazel
@@ -0,0 +1,138 @@
+load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
+load("@rules_ruby//ruby:defs.bzl", "ruby_library", "ruby_test")
+
+ruby_library(
+    name = "common_tests",
+    srcs = ["common_tests.rb"],
+)
+
+filegroup(
+  name = "test_protos",
+  srcs = glob(["*.proto"]),
+  visibility = [
+      "//ruby:__subpackages__",
+  ],
+)
+
+ruby_test(
+    name = "basic",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["basic.rb"],
+)
+
+ruby_test(
+    name = "basic_proto2",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["basic_proto2.rb"],
+)
+
+ruby_test(
+    name = "encode_decode_test",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["encode_decode_test.rb"],
+)
+
+ruby_test(
+    name = "gc_test",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["gc_test.rb"],
+)
+
+ruby_test(
+    name = "generated_code_test",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["generated_code_test.rb"],
+)
+
+ruby_test(
+    name = "multi_level_nesting_test",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["multi_level_nesting_test.rb"],
+)
+
+ruby_test(
+    name = "repeated_field_test",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["repeated_field_test.rb"],
+)
+
+ruby_test(
+    name = "ruby_version",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["ruby_version.rb"],
+)
+
+ruby_test(
+    name = "stress",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["stress.rb"],
+)
+
+ruby_test(
+    name = "type_errors",
+    deps = [
+        ":common_tests",
+        "//ruby:protobuf",
+        "//ruby:test_ruby_protos",
+        "@protobuf_bundle//:test-unit"
+    ],
+    srcs = ["type_errors.rb"],
+)
+
+pkg_files(
+    name = "dist_files",
+    srcs = glob([
+        "*.proto",
+        "*.rb",
+    ]) + [
+        "BUILD.bazel",
+    ],
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//ruby:__pkg__"],
+)
+
diff --git a/ruby/tests/common_tests.rb b/ruby/tests/common_tests.rb
index 9288425..09ea4d8 100644
--- a/ruby/tests/common_tests.rb
+++ b/ruby/tests/common_tests.rb
@@ -824,7 +824,7 @@
                                       :repeated_enum => [:A, :B, :C],
                                       :repeated_msg => [proto_module::TestMessage2.new(:foo => 1),
                                                         proto_module::TestMessage2.new(:foo => 2)])
-    if proto_module == ::BasicTest
+    if proto_module.class.name == "BasicTest"
       # For proto3 we can add an unknown enum value safely.
       m.repeated_enum << 100
     end
diff --git a/ruby/travis-test.sh b/ruby/travis-test.sh
deleted file mode 100755
index 39f58a2..0000000
--- a/ruby/travis-test.sh
+++ /dev/null
@@ -1,32 +0,0 @@
-#!/usr/bin/env bash
-
-# Exit on any error.
-set -ex
-
-test_version() {
-  version=$1
-  bazel_args="\
-    $(../kokoro/common/bazel_flags.sh) \
-    --action_env=PATH \
-    --action_env=GEM_PATH \
-    --action_env=GEM_HOME \
-    --test_env=KOKORO_RUBY_VERSION=$version"
-
-  if [[ $version == jruby-9* ]] ; then
-    bash --login -c \
-      "rvm install $version && rvm use $version && rvm get head && \
-       which ruby && \
-       git clean -f && \
-       gem install --no-document bundler && bundle && \
-       bazel test //ruby/... $bazel_args --define=ruby_platform=java"
-  else
-    bash --login -c \
-      "rvm install $version && rvm use $version && \
-       which ruby && \
-       git clean -f && \
-       gem install --no-document bundler -v 1.17.3 && bundle && \
-       bazel test //ruby/... $bazel_args --define=ruby_platform=c"
-  fi
-}
-
-test_version $1