RVM and Jenkins setup

2019-01-17 08:11发布

问题:

I am new to Jenkins CI. I'm install RVM in my remote Jenkins and when I execute below shell.

#!/bin/bash -x
source ~/.bashrc
rvm use 1.9.3@rails-3.2.3

I get following errors.

+ source /var/lib/jenkins/.bashrc
++ PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/var/lib/jenkins/.rvm/bin:/var/lib/jenkins/.rvm/bin
+ rvm use 1.9.3@rails-3.2.3

RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal settings to allow shell login.
Please visit https://rvm.io/workflow/screen/ for example.

What does it mean? I don't have any idea. Please help me.

UPDATED: I'm tried below script but I still get errors:

#!/bin/bash -x
source /home/zeck/.bashrc
[[ -s ".rvmrc" ]] && source .rvmrc
export RAILS_ENV=test
bundle install

Errors:

/tmp/hudson457106939700368111.sh: line 5: bundle: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Jenkins build shell can't detect RVM, gemsets and gems. What should I do?

UPDATED 2: Therefore jenkins can't detect ruby.

+ ruby -v
/tmp/hudson2505951775163045158.sh: line 5: ruby: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILUR

I'm not using any jenkins plugn and I'm just run script from Build->Execute shell section.

回答1:

try:

. $(/home/RVM_USER/.rvm/bin/rvm env 1.9.3@rails-3.2.3 --path)

make sure you run the stable RVM:

rvm get stable

NOTE: Last Jenkins version does not always accept "source", but ".". RVM_USER is the user that installed RVM. Alternatively you can also export the RVM command in the main PATH.



回答2:

As the error message suggests, RVM expects an login shell. Changing the hashbang line to #!/bin/bash -xl should resolve this.



回答3:

Yes, apparently you miss the $HOME/.rvm/bin in your PATH. I am using rvm successfully from Hudson on Mac OS X. First thing to notice is that, unless you define BASH_ENV environment variable (ENV for sh), .bashrc is called automatically only with interactive non-login shell. Such a shell is started when you do - for example - the following from the command line:

$ /bin/bash

When you use #!/bin/bash in your script, .bashrc will not be called.

To make rvm work with Hudson, I have the following in my .bash_profile:

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
export PATH

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Thanks to [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" I have rvm enabled every time I start new terminal window (interactive, login shell).

I do not put anything in my .bashrc, especially I am not sourcing rvm scripts there. Nothing wrong with that, but if any other scripts makes something stupid like setting `export BASH_ENV=$HOME/.bashrc' and then invoke non-interactive shell, you see what may happen - it is actually easy to forget.

Therefore, instead of loading things to your .bashrc, it is better to keep your script independent from any shell startup file and make sure that the correct environment is set up within the script. I still keep $HOME/.rvm/bin in my .bash_profile, but then I include the following at the beginning of my script:

#!/bin/bash

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

rvm use 1.9.3-head@MyGemSet

set -ex
cucumber # just an example

Notice the -e option which forces the script to exit with error code if any command following set -ex fails. This is behavior you may want when using the script with Hudson. It is incorrect to say that RVM expects a login shell. Although using #!/bin/bash -l in your script will work, it does not seem like the best approach.



回答4:

Just add this code in your shell script, i think rvm is loading from your source so it should work else need to export PATH variable

#!/bin/bash -l
source ~/.bashrc
rvm use 1.9.3@rails-3.2.3

l is for login shel, if you include x then it would be for debugging too.



回答5:

adding a shebang to the build commands in jenkins fixed this for me

    #!/usr/bin/env bash

    rvm use 2.0.0
    bundle install
    rake test
    ...


回答6:

Jenkins nodes don't load paths the same way, so it's not using the proper path to find rvm's version of ruby. You can set the path for a given agent.

  1. Find your current PATH by doing echo $PATH
  2. Assuming you've set up rvm properly, find where rvm's version of ruby is located by running which ruby
  3. There's a setting in the Configuration of your agent where you can set environmental variables. Set PATH to be 1 and 2 concatenated.