Jenkins uses default ruby but not from RVM

3.2k Views Asked by At

I have Jenkins automation server that connects to my machine through ssh with user snaggs.

My machine has 2 versions of ruby:

  • default version comes from OS /System/Library/Frameworks/ ..... /usr/bin/ruby

  • ruby v2.3.3 installed with RVM

    On machine the automation runs some Perl script with some commands. whoami from jenkins gives snaggs

When Perl script runs command gem env the output is:

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.14.1
  - RUBY VERSION: 2.0.0 (2015-12-16 patchlevel 648) [universal.x86_64-darwin16]
  - INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.0.0
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-16
  - GEM PATHS:
     - /Library/Ruby/Gems/2.0.0
     - /Users/snaggs/.gem/ruby/2.0.0
     - /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["http://rubygems.org"]
  - REMOTE SOURCES:
     - http://rubygems.org

However when I enter to the same machine with ssh to the same user snaggs I have ruby version installed with rvm:

When I run $ gem env I get output:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.6.8
  - RUBY VERSION: 2.3.3 (2016-11-21 patchlevel 222) [x86_64-darwin16]
  - INSTALLATION DIRECTORY: /Users/snaggs/.rvm/gems/ruby-2.3.3
  - USER INSTALLATION DIRECTORY: /Users/snaggs/.gem/ruby/2.3.0
  - RUBY EXECUTABLE: /Users/snaggs/.rvm/rubies/ruby-2.3.3/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/snaggs/.rvm/gems/ruby-2.3.3/bin
  - SPEC CACHE DIRECTORY: /Users/snaggs/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/snaggs/.rvm/rubies/ruby-2.3.3/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-16
  - GEM PATHS:
     - /Users/snaggs/.rvm/gems/ruby-2.3.3
     - /Users/snaggs/.rvm/gems/ruby-2.3.3@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["http://rubygems.org"]
  - REMOTE SOURCES:
     - http://rubygems.org
  - SHELL PATH:
     - /Users/snaggs/.rvm/gems/ruby-2.3.3/bin
     - /Users/snaggs/.rvm/gems/ruby-2.3.3@global/bin
     - /Users/snaggs/.rvm/rubies/ruby-2.3.3/bin
     - /usr/local/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /Users/snaggs/.rvm/bin

How to tell to Jenkins to use other version located under /Users/snaggs/.rvm/gems/ruby-2.3.3?

[EDIT]

I don't run script as super user

1

There are 1 best solutions below

3
On BEST ANSWER

I had a similar problem when tried to configure Java version.

Take a look at Jenkins wiki:

When the SSH slaves plugin connects to a slave, it does not run an interactive shell. Instead, it does the equivalent of your running "ssh slavehost command..."

So suppose when you will run gem env from ssh, you should get the same result like Jenkins gets: ssh [email protected] "gem env"

On machine the automation runs some Perl script with some commands.

You can try to create some custom .bash_profile_CUSTOM that equals to your original .bash_profile You need to call .bash_profile_CUSTOM because Jenkins uses own .bash_profile by default .Run your perl script like:

ssh [email protected] "source ~/.bash_profile_CUSTOM && perl some_perl_script.pl"

Just remember that you need to bind bash_profile_CUSTOM file each time Jenkins connects to your build machine. So from Jenkins the command executable should be something like:

source ~/.bash_profile_CUSTOM && perl some_perl_script.pl

let me know if it works or give you different output


Edit: (25/12/2016)

If you don't want to install plugins for Jenkins (like EnvInject Plugin for Prefix Start Slave Command) you can wrap Perl script with bash script, like:

#!/bin/bash

source ~/. bash_profile_CUSTOM;

perl some_perl_script.pl;

So Perl script will start with profile you defined with .bash_profile_CUSTOM.