RVM and Jenkins setup

28,451

Solution 1

try:

. $(/home/RVM_USER/.rvm/bin/rvm env [email protected] --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.

Solution 2

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

Solution 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.

Solution 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 [email protected]

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

Solution 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
    ...
Share:
28,451
Zeck
Author by

Zeck

Updated on July 25, 2020

Comments

  • Zeck
    Zeck almost 4 years

    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 [email protected]
    

    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 [email protected]
    
    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.

  • Zeck
    Zeck about 12 years
    Very first thank you for trying to help. But I get 'rvm: command not found'. Please see my updates
  • mpapis
    mpapis about 12 years
    it's because you use different users for one rvm installation, make sure the second user zeck uses the same RVM - in /home/zeck/.bashrc add: export PATH=$PATH:/var/lib/jenkins/.rvm/bin
  • disrupt
    disrupt about 12 years
    By the way, sourcing .rvmrc can print a lot to the terminal. You may want to instead use a #!/bin/bash -l, then do set -x after sourcing .rvmrc.
  • 0x4a6f4672
    0x4a6f4672 over 11 years
    Yes, this helped. The location of RVM may differ, in my case it was [[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" which worked, together with rvm use ruby_name@gemset_name
  • ncuesta
    ncuesta over 10 years
    You sir, made my day :)
  • A-letubby
    A-letubby about 10 years
    Any explanation of what are -l and -x using for?
  • Shikhar Upadhyay
    Shikhar Upadhyay over 9 years
    The -l option gives you a login shell, and -x starts bash in debug mode. I was able to use just -l so that the Jenkins output wasn't as verbose.
  • Genki
    Genki about 4 years
    . $($HOME/.rvm/bin/rvm env --path) worked for me as well.