RVM not loaded in Jenkins Shell Environment - ruby-on-rails

I want to run bundle install on my Jenkins server in the context of a given RVM Ruby Installation.
When I sign in via SSH it all works fine:
which bundle
# /Users/me/.rvm/gems/ruby-2.1.0#global/bin/bundle
ruby -v
# ruby 2.1.0p0 ...
But when I do this via Jenkins I get this:
which bundle
# /usr/bin/bundle
So I added source ~/.bash_profile to the top of my script which should load the RVM context but for this I get the following output and the path of bundle is still wrong:
+ source /Users/me/.bash_profile
++ [[ -s /Users/me/.profile ]]
++ source /Users/me/.profile
+++ PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Users/me/.rvm/bin
++ [[ -s /Users/me/.rvm/scripts/rvm ]]
++ source /Users/me/.rvm/scripts/rvm
+++ __rvm_has_opt posix
+++ [[ -n '' ]]
+++ [[ -n 3.2.51(1)-release ]]
+++ [[ :braceexpand:errexit:hashall:interactive-comments:posix:xtrace: =~ :posix: ]]
+++ return 0
+ which bundle
/usr/bin/bundle
Now how do I tell Jenkins to always load the RVM Environment. Thanks :)

When I run a shell script in Jenkins, I do something like
#!/bin/bash
echo '##################### BUNDLE/MIGRATION #####################'
source ~/.bashrc
rvm use 2.1#gemset
bundle install
bundle exec rake db:schema:load RAILS_ENV=test
bundle exec rake db:test:prepare
I my .bashrc I have the lines
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
PATH=$PATH:$HOME/.rvm/bin
I do not know if this is the state of the art, but it works :-)

Try this: rvm 2.1 do which bundle

Related

Deploy rails to beanstalk error

When deploying standard rails api app to beanstalk, it was failing with following log :
++ export RUBY_ROOT=/opt/rubies/ruby-2.4.3
++ RUBY_ROOT=/opt/rubies/ruby-2.4.3
++ export RUBYOPT=
++ RUBYOPT=
++ export PATH=/opt/rubies/ruby-2.4.3/bin:/opt/elasticbeanstalk/lib/ruby/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
++ PATH=/opt/rubies/ruby-2.4.3/bin:/opt/elasticbeanstalk/lib/ruby/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
+++ /opt/rubies/ruby-2.4.3/bin/ruby -
++ eval 'export RUBY_ENGINE=ruby;
export RUBY_VERSION=2.4.3;
export GEM_ROOT="/opt/rubies/ruby-2.4.3/lib/ruby/gems/2.4.0";'
+++ export RUBY_ENGINE=ruby
+++ RUBY_ENGINE=ruby
+++ export RUBY_VERSION=2.4.3
+++ RUBY_VERSION=2.4.3
+++ export GEM_ROOT=/opt/rubies/ruby-2.4.3/lib/ruby/gems/2.4.0
+++ GEM_ROOT=/opt/rubies/ruby-2.4.3/lib/ruby/gems/2.4.0
++ (( 0 != 0 ))
+ cd /var/app/ondeck
+ su -s /bin/bash -c 'bundle exec /opt/elasticbeanstalk/support/scripts/check-for-rake-task.rb db:migrate' webapp
`/home/webapp` is not a directory.
Bundler will use `/tmp/bundler/home/webapp' as your home directory temporarily.
+ '[' false = true ']'
+ su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp
`/home/webapp` is not a directory.
Bundler will use `/tmp/bundler/home/webapp' as your home directory temporarily.
rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'rb-readline'.
Gem Load Error is: HOME environment variable (or HOMEDRIVE and HOMEPATH) must be set and point to a directory
I am new to Beanstalk, may someone point out what is the issue?
It seems it was trying to use "/home/webapp" but the directory was not created somehow.
Thanks a lot!
I found the issue to be resolved by deleting rb-readline gem from the build. Not a best fix, but for now it works.

bundle not found when running services using upstart/foreman

I'm setting up a staging environment for a Rails 4 app on Ubuntu 12.04.
I use:
- rbenv for managing rubies
- capistrano for deployemnt
- foreman for managing services
In particular, I want to run que as a service. My Procfile looks like
que: bundle exec rake que:work
I have exported the Procfile for upstart successfully. My sapp-que-1.conf looks like
start on starting ft-id-que
stop on stopping ft-id-que
respawn
exec su - deployer -c 'cd /home/deployer/apps/sapp/releases/20140307080502; export PORT=5000; bundle exec rake que:work >> /home/deployer/apps/sapp/current/log/que-1.log 2>&1'
When I try to start it (sudo start sapp) I get the following error:
-su: bundle: command not found
However if I cd into /home/deployer/apps/sapp/releases/20140307080502 and I manually run bundle exec rake que:work the rake is executed.
Am I missing anything here?
Thanks.
Could you have .bash_profile ?
The su does not read .bashrc.
[SOLVED] Special user, .bashrc not being executed upon login
I create .bash_profile with
# Load the default .profile
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile"
# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Error while installing ruby on rails on server

After installing rvm when i run this
source $HOME/rvm/scripts/rvm
i get error
-bash: /root/rvm/scripts/rvm: No such file or directory
reference: Problem deploying Ruby+RVM and daemontools
I found the answer but looking at the rvmsudo script installed with
rvm, here is a working run script:
#!/bin/sh
# redirect stderr to stdout
exec 2>&1
cd /app
# load rvm
. /usr/local/rvm/scripts/rvm
# select ruby version for this application
rvm use 1.9.1
# # depending on your configuration you may need to provide the absolute path to rvm, like that:
# /usr/local/bin/rvm use 1.9.1
# build the exec command line preserving the rvm environment
command="exec sudo -u app_user /usr/bin/env PATH='$PATH'"
[[ -n "${GEM_HOME:-}" ]] && command="${command} GEM_HOME='$GEM_HOME' "
[[ -n "${GEM_PATH:-}" ]] && command="${command} GEM_PATH='$GEM_PATH' "
# this is where your real command line goes
command="${command} ruby main.rb"
# run the application
eval "${command}"
Try source ~/.rvm/scripts/rvm. Exit out of your current shell and then try again it should work.

.rvmrc syntax error

I am trying to work with a web app made by someone else. I cloned that repo and when I cd'ed into it, the .rvmrc executed. But I am getting the following error in it:
: command not found
: command not found
: command not found
: command not found
-bash: /home/sleepingdragon/Desktop/sharedcab/main-apps/ytaxi-apps/yTaxi/.rvmrc: line 22: syntax error in conditional expression
-bash: /home/sleepingdragon/Desktop/sharedcab/main-apps/ytaxi-apps/yTaxi/.rvmrc:'line 23: syntax error near `]]
-bash: /home/sleepingdragon/Desktop/sharedcab/main-apps/ytaxi-apps/yTaxi/.rvmrc:'line 23: ` && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
The .rvmrc file is this:
#!/usr/bin/env bash
# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory
# First we specify our desired <ruby>[#<gemset>], the #gemset name is optional,
# Only full ruby name is supported here, for short names use:
# echo "rvm use 1.9.3" > .rvmrc
environment_id="ruby-1.9.3-p194#ytaxi"
# Uncomment the following lines if you want to verify rvm version per project
# rvmrc_rvm_version="1.14.2 (stable)" # 1.10.1 seams as a safe start
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
# return 1
# }
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
if [[ $- == *i* ]] # check for interactive shells
then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
rvm --create use "$environment_id" || {
echo "Failed to create RVM environment '${environment_id}'."
return 1
}
fi
# If you use bundler, this might be useful to you:
# if [[ -s Gemfile ]] && {
# ! builtin command -v bundle >/dev/null ||
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
# }
# then
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
# gem install bundler
# fi
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
# then
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
# fi
I have not worked with .rvmrc before and am a Rails noob. Could you help me find the error?
Just create .ruby-version file containing
ruby-1.9.3-p194#ytaxi
and delete the .rvmrc file.
You are using minimal version of .rvmrc which also is buggy, you could either fix it:
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" &&
-s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
and you can switch to the new .ruby-version file:
rvm get stable
rvm rvmrc to ruby-version

Installed RVM, rvm commands do not work

I just went through the RVM install.
I did this command:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
and everything ran fine. Then in my user home directory, in .bash_profile I added
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Then I started a new shell just to be sure. And then I tried
rvm
and got the error that
No command 'rvm' found
Any ideas why that might happen?
maybe it is because .bash_profile only runs on login, if you run a new shell it only executes .bashrc
You could test using
> bash -l
Did you see the "Troubleshooting your Install" section on this page.
It mentions that you need to remove return from your .bashrc. I often forget to do this on a fresh ubuntu install.
The example they give for ubuntu is to change this:
[ -z "$PS1" ] && return
# Some code here... e.g.
export HISTCONTROL=ignoreboth
to this:
if [[ -n "$PS1" ]]; then
# Some code here... e.g.
export HISTCONTROL=ignoreboth
fi
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session

Resources