RVM and Jenkins setup - ruby

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.

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

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.

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.

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.

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

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

Related

Ruby and Xcode - wrong ruby version [duplicate]

Outside of Xcode I use a specific version of Ruby, using RVM to manage multiple Ruby installations.
Apple's command line dev tools install Ruby at /usr/bin/ruby and is version 1.8.7.
I use 1.9.3 through RVM.
Is there a way to force Xcode to use my 1.9.3 installation when running its Run Script build phases?
I already tried setting the Shell path to the full path of my specific Ruby, but that didn't seem to make a difference, by which I mean that the particular Gems I have installed in my 1.9.3 weren't available/visible to the script when run within Xcode.
If I run my project through xcodebuild on the command line, the Run Script phase uses my specific Ruby because it's being run from within my shell environment (even if the Shell path in the project file is set to /usr/bin/ruby, it still uses my 1.9.3).
What can I do to make the IDE use my 1.9.3 Ruby install?
I had the same (well, worse) problem, and the code that follows worked for me.
The key thing to realize is that, on the command line, you are using <something>/bin/rvm, but in a shell script, in order for rvm to change that environment, you must use a function, and you must first load that function to your shell script by calling source <something>/scripts/rvm. More on all this here.
This code is also gisted.
#!/usr/bin/env bash
# Xcode scripting does not invoke rvm. To get the correct ruby,
# we must invoke rvm manually. This requires loading the rvm
# *shell function*, which can manipulate the active shell-script
# environment.
# cf. http://rvm.io/workflow/scripting
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else
printf "ERROR: An RVM installation was not found.\n"
exit 128
fi
# rvm will use the controlling versioning (e.g. .ruby-version) for the
# pwd using this function call.
rvm use .
As a protip, I find embedding shell code in a project.pbxproj file yucky. For all but the most trivial stuff, my actual run script step is usually just a one-line call out to an external script:
Try this at the beginning of your script in Xcode:
source "$HOME/.rvm/scripts/rvm"

Use rvm to force specific Ruby in Xcode Run Script build phase

Outside of Xcode I use a specific version of Ruby, using RVM to manage multiple Ruby installations.
Apple's command line dev tools install Ruby at /usr/bin/ruby and is version 1.8.7.
I use 1.9.3 through RVM.
Is there a way to force Xcode to use my 1.9.3 installation when running its Run Script build phases?
I already tried setting the Shell path to the full path of my specific Ruby, but that didn't seem to make a difference, by which I mean that the particular Gems I have installed in my 1.9.3 weren't available/visible to the script when run within Xcode.
If I run my project through xcodebuild on the command line, the Run Script phase uses my specific Ruby because it's being run from within my shell environment (even if the Shell path in the project file is set to /usr/bin/ruby, it still uses my 1.9.3).
What can I do to make the IDE use my 1.9.3 Ruby install?
I had the same (well, worse) problem, and the code that follows worked for me.
The key thing to realize is that, on the command line, you are using <something>/bin/rvm, but in a shell script, in order for rvm to change that environment, you must use a function, and you must first load that function to your shell script by calling source <something>/scripts/rvm. More on all this here.
This code is also gisted.
#!/usr/bin/env bash
# Xcode scripting does not invoke rvm. To get the correct ruby,
# we must invoke rvm manually. This requires loading the rvm
# *shell function*, which can manipulate the active shell-script
# environment.
# cf. http://rvm.io/workflow/scripting
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else
printf "ERROR: An RVM installation was not found.\n"
exit 128
fi
# rvm will use the controlling versioning (e.g. .ruby-version) for the
# pwd using this function call.
rvm use .
As a protip, I find embedding shell code in a project.pbxproj file yucky. For all but the most trivial stuff, my actual run script step is usually just a one-line call out to an external script:
Try this at the beginning of your script in Xcode:
source "$HOME/.rvm/scripts/rvm"

After Installation RVM and Ruby 1.9.3 every Bash path change RVM Initialization Script is printed

I installed the latest RVM - Ruby Version Manager - and installed Ruby 1.8.7 and 1.9.3 on OSX Lion with XCode 4.3.3 using clang rvm install 1.9.3 --reconfigure --debug -C --enable-pthread --with-gcc=clang as the regular way did not work due to a GCC error. I did get one error clang: error: unsupported option '--with-libyaml, but Ruby 1.9.3 worked and I could run WPScan that needs at least 1.9.2 .
But now every time I run a command to change folder such as cd I get a long Bash script printed related to RVM - see http://pastebin.com/UAm38Vcm and:
.
How can I stop it from doing that?
Update I
Added a comment at RVM at Github as well https://github.com/wayneeseguin/rvm/issues/1039 , but as that issue is not 100% related and as I need this solved as soon as possible I opened a thread here with more data.
Update II
I realized the RVM Initialization script is the one that is being printed: https://github.com/wayneeseguin/rvm/blob/master/scripts/initialize . No idea why though..
Update IV
My .bashrc
# define aliases
alias sudo='sudo '
#alias ruby='ruby1.9'
alias apacherestart='sudo apachectl -k restart'
# define hist properties
HISTFILESIZE=1000000000
HISTSIZE=1000000
# define path to programs
PATH=/opt/local/bin:opt/local/sbin:/opt/subversion/bin:/opt/local/apache2/bin/:/opt/local/lib/python2.4/site-packages/django/bin:$PATH
# define manpath
MANPATH=/opt/local/share/man:$MANPATH
# export env vars
export HISTFILESIZE HISTSIZE PATH MANPATH
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
My .bash_profile
source ~/.bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Your cd command has somehow become aliased or a function has been created named "cd".
You can undo those with either:
unalias cd
or
unset -f cd
You should be able to revert temporarily with
unalias cd
It appears that you will need to figure out what was changed in your .bashrc and revert all those changes. If the installation script is at all sanely written, it should have made backups, or document somehow what was changed.

"RVM is not a function" error

RVM is installed on my machine (running Mac OSX 10.6.8), correctly and it runs fine. The odd thing is that to run it, I have to use source ~/.rvm/scripts/rvm for every new session. I tried making a symlink from it to /opt/local/bin/rvm, but when it runs it does nothing. I also tried creating a symlink from ~/.rvm/bin/rvm to /opt/local/bin/rvm, and when I run rvm in the Terminal it displays the help page, as expected. But when I try rvm use some_ruby_version it always displays "RVM is not a function, selecting rubies with 'rvm use ...' will not work.". How can I fix this?
My goal is to get it to the the point that I don't have to type the source command every session, and for some reason ~/.profile does not execute.
You have to source the RVM script into the current session because it makes changes to the shell environment - and it is absolutely impossible for that to be done from a child process. Your efforts at running RVM as an external command cannot succeed.
To actually fix this you have two choices:
Configure your terminal emulator to start a login shell, rather than a non-login shell, so that your .profile is loaded.
Modify .bashrc to source RVM instead, which works for non-login shells as well.
To do the second you can just add to ~/.bashrc:
if test -f ~/.rvm/scripts/rvm; then
[ "$(type -t rvm)" = "function" ] || source ~/.rvm/scripts/rvm
fi
If you are using zsh as shell instead bash, you have to:
1.
vi ~/.zshrc
2.
Like Matt said, add:
if test -f ~/.rvm/scripts/rvm; then
[ "$(type -t rvm)" = "function" ] || source ~/.rvm/scripts/rvm
fi
3. Restart Terminall
4. Done!
rvm use 1.9.3
Wil work
I didn't understand what ~/.profile does correctly; I needed to change ~/.bash_profile instead. Problem solved!
Well, with mountain lion (10.8.3) what worked for me was editing /etc/profile
and adding the line mentioned before at the bottom of the file:
if test -f ~/.rvm/scripts/rvm; then
[ "$(type -t rvm)" = "function" ] || source ~/.rvm/scripts/rvm
fi
I had the same issue. I found the .profile file was not getting updated, so i added the same command that was added into .bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
I don't know if this is the right way, but it worked...
You shouldn't need to edit anything as others suggest. Just go into your terminal's settings and select the "Run command as login shell". This will cause .profile to run on the next terminal instance. Reopen your terminal and you should be able to use rvm use 1.9.3 (or whatever version you installed).
More info found on rvm.io (which is also a great place for answers)
https://rvm.io/integration/gnome-terminal
You have to make some settings.
Open terminal and run this command.
source ~/.rvm/scripts/rvm
and then go to edit > Title and command and check Run command as login shell
and you are done. Now you don't need to specify source everytime.
What was screwing me up was assuming my path was correct since I was using one I can run manually.
Apparently there are different executables or scripts that can be used and are located in different places.
I thought that the path Mina should use was this:
/usr/local/rvm/bin/rvm
When in reality it was this:
/usr/local/rvm/scripts/rvm
I had this issue when I became root. I tried many of the solutions above. What finally worked was exiting from root and being a regular user. Which is what I needed anyway.
None of these solutions seemed to redeem my problem which was on Ubuntu 12.04 LTS.
What I did is the following:
rvm get stable --auto-dotfiles as outlined in the RVM documentation here
Added source ~/.profile as the first line of: ~/.bash_profile
I will not all of these steps were documented as errors from the RVM command line:
RVM is not a function, selecting rubies with 'rvm use ...' will not
work. You need to change your terminal emulator preferences to allow
login shell. Sometimes it is required to use /bin/bash --login as
the command. Please visit https://rvm.io/integration/gnome-terminal/
for a example.
and
WARNING: You have '~/.profile' file, you might want to load it,
to do that add the following line to '/home/user_name/.bash_profile':
source ~/.profile

rvm installation not working: "RVM is not a function"

I just installed RVM, but can't make it work. I have such line at the end of my .profile file:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
I tried to run source .profile and restarting terminal, but still, when I run rvm use 1.9.2 I'm getting:
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
My system is Ubuntu 11.10.
You need to run the following
$ source ~/.rvm/scripts/rvm
then run this
$ type rvm | head -n 1
and if you get
rvm is a function
the problem is solved.
You also need to run user$ rvm requirements to see dependency requirements for your operating system
Source: https://rvm.io/rvm/install/
I forget mention that you need to put this code into you ~/.bashrc or ~/.zshrc file and you will not need to write this code again.
You are not using an login shell.
The process of enabling the login flag is described here, also some details on what a login shell is can be found here.
Thus, you need to check the option "Run as login shell" in the Gnome terminal's settings. It is required to open new terminal after this setting the flag.
Sometimes it is required to set the command to /bin/bash --login.
For remote connections it is important to understand the differene between running interactive ssh session and executing single commands.
While running ssh server and then working with the server interactively you are using login shell by default and it's all fine, but for ssh server "command" you are not using login shell and it would be required to run it with ssh server 'bash -lc "command"'.
Any remote invocation can have the same problem as executing single command with ssh.
To permanently resolve this just cut/paste following line:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
From: ~/.bash_profile file
To: ~/.bashrc file
Reason this works is that .bashrc is executed each time you enter the terminal, and .bash_profile each time you login. That is why solution /bin/bash --login works, but you have to do that each time you enter the terminal. This way you are set until your next format, and you will forget all this by than :)
I too faced this problem. Finally i executed this line on terminal.
source ~/.rvm/scripts/rvm
Problem is fixed. Because this line will make the RVM instance a function for a particular time.
The latest RVM (rvm 1.11.6 (stable)) stopped working on Ubuntu (10.10 - 64 bit - nerdy gnat or whatever) - I kept getting
"RVM is not a function, selecting rubies with 'rvm use ...' will not work."
Before, I got the message, but 'rvm 1.9.3-p0#rails321' would work. Now, it wouldn't work - you couldn't change gemsets at all.
Nothing worked, until I found this - make this the LAST line in /home/your-name/.bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
As you said, the error shown could be the following one.
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.
As said above, just type '/bin/bash --login' in your terminal (after restarting your terminal), then type the comand 'rvm use 1.9.3' (for e.g.) and it will start using the same version.
Just execute the command 'ruby -v' to confirm that the RVM is using the updated version of Ruby.
I had this problem too on a fresh rvm installation, and non of the answers here fixed it. Going into the official rvm site, on the basics section, they have this command:
# from http://rvm.io/rvm/basics
source $(rvm 1.9.3 do rvm env --path)
You should change 1.9.3 for the ruby version that you actually want, and it'll make rvm a function regardless of the shell type.
even though you accepted an answer, i'd like to suggest another way .. ~/.bashrc is loaded before any shell is opened. Add that line at the end of that, and you don't need any of that login shell thing
Maybe you can try belows:
Your Terminal ->
Edit ->
Profile Preferences ->
Title and Command ->
Check the "Run command as a login shell"
Done
Run bash --login and then run rvm use 2.0.0.
Open Up the Terminal and then Go to Edit > Profile Preferences and then go to the Tab "Title and Command" and Check "Run Command as Login Shell".
Boot Up a Bash and Now you can install Gems directly from the terminal without the use of sudo and the error "RVM is not a function, selecting rubies with 'rvm use ...' will not work." will be eliminated.
Cheers.
All the above answers are valid. But when i faced the same issue, the solution was the following:
Update ZSH. (Tried to update directly din't work for some reason. So uninstalled and reinstalled updated version from here)
Set default shell as zsh (i.e. if you prefer zsh) using sudo chsh -s $(which zsh) $USER
Ensure that the following code is at the bottom of your .zshrc after you have installed the latest RVM probably using CURL from official RVM site
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
source ~/.profile
MOST IMPORTANT POINT: Ensure that in your .zshrc file every export to PATH is appended with :$PATH. Which i believe was the root of my problems even after following the above steps.
post this all my problems of RVM Not being a function went away. If it still does not work, give some error trace over here. After a few hours of struggle to solve this issue, i'm sure i must have seen all related errors.
Hope it helps. Cheers!
Procedure for installing Ruby 1.9.3-p125 on Mac OSX 10.8 Mountain Lion
You've already installed the latest XCode (>= 4.3) and and the command line Objective-C
compiler "clang".
You must run the "bash" shell for this procedure to work.
Go to System Preferences
Click on "Users & Groups"
Click the lock on the bottom left of the panel and enter your password to unlock it.
"Ctrl-Click" on your user icon in the left pane of the panel and choose "Advanced Options..."
Change the Login Shell to "/bin/bash"
Close the preferences
Open a terminal window (press command+spacebar and type in "terminal")
Follow the instructions at:
http://www.frederico-araujo.com/2011/07/30/installing-rails-on-os-x-lion-with-homebrew-rvm-and-mysql/
Notes:
To install ruby, you may need to specify the clang compiler:
$ rvm install 1.9.3p125 --with-gcc=clang
If RVM gripes about /usr/local/rvm not found, you need to create a link:
$ ln -s /Users/[your user name]/.rvm /usr/local/rvm
source ~/.bash_profile
... should do the trick ..., probably need to logout and login again.
How to reload .bash_profile from the command line?
I'd got the same error because I'd ever installed the old rvm version ruby-rvm with the apt-get command.
I solved the problem by remove the script line to config the old rvm in .bashrc file.
Check the old rvm config script and then run source .profile
« Official » instructions are there: https://rvm.io/integration/gnome-terminal/
I fixed it by adding this line to .bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
I had the same error, but none of the solutions on this page seemed to work. For me it was enough to add the rvm executable to my path:
PATH=$PATH:/usr/local/rvm/bin/
Et voila!
I had the same issue and I did this in my .bash_profile and it worked.
source "$HOME/.rvm/scripts/rvm".
For those who comes with same issue and they are using lubuntu like me I followed this link :
You start your terminal with
lxterminal -e "bash -il"
Thank to #mpapsis who pointed me to the right direction
My unclean way to change of ruby version is
rvm alias create default ruby-2.2.3 && source ~/.bashrc && rvm list
it works because I have the line bellow in my ~/.bashrc but strangely it don't do the job automatically.
[[ -s "/usr/local/rvm/bin/rvm" ]] && source "/usr/local/rvm/bin/rvm"
I tried to connect with --login to my docker container
docker run -it imagename `/bin/bash --login`
but in this case the container stay open in background and I can't enter commands.
I tried both zhc and terminal with the option "open with /bin/bash --login"
$ source ~/.rvm/scripts/rvm
if you don't want to do it again and again for every terminal tab enable the login shell by following these steps.
got to preferences
enter image description here
enable run command as a login shell
enter image description here

Resources