From d0f8fda3b538096bc6b86b82cbff35a7c5907b89 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Tue, 21 Jan 2025 15:33:56 -0500 Subject: [PATCH] feat: allow easier selection of the ruby run in the container --- README.md | 36 ++++++++++++++++++++++++++++++ build/sudoers | 2 +- lib/rake_compiler_dock/starter.rb | 6 +++++ test/test_environment_variables.rb | 13 +++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cd36cd77..9dc70faf 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,42 @@ Other environment variables can be set or passed through to the container like t RakeCompilerDock.sh "rake cross native gem OPENSSL_VERSION=#{ENV['OPENSSL_VERSION']}" ``` +### Choosing the version of ruby to run in the build container + +There are [multiple versions of Ruby installed in the build container](https://github.com/rake-compiler/rake-compiler-dock/blob/d3e6b44916d9d63d4c431b8c749a2c58bf1fe8e8/Dockerfile.mri.erb#L77). The latest version will be the default, but you may need to choose a different version to run during your build process. + +You can do this by passing a `ruby:` keyword argument to `RakeCompilerDock.sh`: + +```ruby +RakeCompilerDock.sh "ruby -v" +# => ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux] + +RakeCompilerDock.sh "ruby -v", ruby: "3.1.6" +# => ruby 3.1.6p260 (2024-05-29 revision a777087be6) [x86_64-linux]48d4efcb85) +PRISM [x86_64-linux] +``` + +Or by setting the environment variable `RCD_RUBY_VERSION`: + +``` sh +$ rake-compiler-dock bash -c "ruby -v" +ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux] + +$ RCD_RUBY_VERSION=3.1.6 rake-compiler-dock bash -c "ruby -v" +ruby 3.1.6p260 (2024-05-29 revision a777087be6) [x86_64-linux] +``` + +Or by running `rbenv shell` in the command: + +``` ruby +RakeCompilerDock.sh "rbenv shell 3.1.6 && ruby -v", platform: "x86_64-linux-gnu" +# => ruby 3.1.6p260 (2024-05-29 revision a777087be6) [x86_64-linux] +``` + +``` sh +$ rake-compiler-dock bash -c "rbenv shell 3.1.6 && ruby -v" +ruby 3.1.6p260 (2024-05-29 revision a777087be6) [x86_64-linux] +``` + ### Choosing specific Ruby versions to support If you only want to precompile for certain Ruby versions, you can specify those versions by overwriting the `RUBY_CC_VERSION` environment variable. diff --git a/build/sudoers b/build/sudoers index bfdeee9f..f627886e 100644 --- a/build/sudoers +++ b/build/sudoers @@ -1 +1 @@ -Defaults env_keep += "http_proxy https_proxy ftp_proxy GEM_PRIVATE_KEY_PASSPHRASE RCD_HOST_RUBY_PLATFORM RCD_HOST_RUBY_VERSION RCD_IMAGE RUBY_CC_VERSION LD_LIBRARY_PATH DEVTOOLSET_ROOTPATH SOURCE_DATE_EPOCH" +Defaults env_keep += "http_proxy https_proxy ftp_proxy GEM_PRIVATE_KEY_PASSPHRASE RCD_HOST_RUBY_PLATFORM RCD_HOST_RUBY_VERSION RCD_IMAGE RUBY_CC_VERSION LD_LIBRARY_PATH DEVTOOLSET_ROOTPATH SOURCE_DATE_EPOCH RBENV_VERSION" diff --git a/lib/rake_compiler_dock/starter.rb b/lib/rake_compiler_dock/starter.rb index 7ed229d5..ba3fe857 100644 --- a/lib/rake_compiler_dock/starter.rb +++ b/lib/rake_compiler_dock/starter.rb @@ -41,6 +41,11 @@ def exec(*args) user = options.fetch(:username){ current_user } group = options.fetch(:groupname){ current_group } + ruby_version = options[:ruby] || ENV['RCD_RUBY_VERSION'] + rbenv_opts = if ruby_version + ["-e", "RBENV_VERSION=#{ruby_version}"] + end + platforms(options).split(" ").each do |platform| image_name = container_image_name(options.merge(platform: platform)) @@ -73,6 +78,7 @@ def exec(*args) "-e", "RCD_HOST_RUBY_PLATFORM=#{RUBY_PLATFORM}", "-e", "RCD_HOST_RUBY_VERSION=#{RUBY_VERSION}", "-e", "RCD_IMAGE=#{image_name}", + *rbenv_opts, "-w", make_valid_path(workdir), *docker_opts, image_name, diff --git a/test/test_environment_variables.rb b/test/test_environment_variables.rb index d6a18eea..6c9d8b7f 100644 --- a/test/test_environment_variables.rb +++ b/test/test_environment_variables.rb @@ -85,6 +85,19 @@ def test_SOURCE_DATE_EPOCH end end + class UsingWrapperSpecifyingRuby < UsingWrapper + include Common + + def invocation(command) + idir = File.join(File.dirname(__FILE__), '../lib') + "RCD_RUBY_VERSION=3.1.6 RCD_PLATFORM=#{TEST_PLATFORM} RCD_RUBYVM=#{IS_JRUBY ? 'jruby' : 'mri'} #{RbConfig::CONFIG['RUBY_INSTALL_NAME']} -I#{idir.inspect} bin/rake-compiler-dock bash -c '#{command}'" + end + + def test_RUBY_VERSION + assert_equal "3.1.6", rcd_env['RBENV_VERSION'] + end + end + class AsIfContinuousIntegration < Test::Unit::TestCase include Common