I'm trying to deploy an experimental Rails app within my enterprise. Everything here is setuo for Java development so I'm having to break new ground to gain acceptance for Rails in the company.
I need to use Jenkins CI (v. 1.580) to deploy my Rails 4.1.4 app (ruby-2.1.2) on a CentOS (release 5.9) staging server here in the office, as a proof of concept before my app will be allowed near a production server.
I've installed the following plugins in Jenkins and restarted: Hudson Ruby Plugin, rvm, Rake plugin, ruby-runtime, RubyMetrics plugin for Jenkins, GIT client plugin, GIT plugin, GitHub API Plugin, GitHub plugin
The Jenkins build seems to be failing during an RVM initialisation script, output below.
My Jenkins .bashrc file looks like this:
#/var/lib/jenkins/.bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
And .rvmrc:
#/var/lib/jenkins/.rvmrc
export rvm_trust_rvmrcs_flag=1 # always trust project .rvmrc
export rvm_install_on_use_flag=1 # auto install rubies
export rvm_gemset_create_on_use_flag=1 # auto install gemsets
I've tried nuking RVM and reinstalling using the multi-user instructions and have ensured that my jenkins user is a member of the rvm group.
My build script looks like:
source ~/.bashrc # Loads RVM
cd . # Loads the RVM environment set in the .rvmrc file
# set env vars
#export GEM_PATH=/usr/local/lib/ruby/gems/1.2/gems
#export CI_REPORTS=results
export RAILS_ENV=test
#cp config/database.yml.example config/database.yml
# creates a database.yml
bundle install # Installs gems
rake db:schema:load # Loads all the database schema
rake # Runs RSpec tests
Here's the complete Console Output from Jenkins
Started by user Pete
Building in workspace /var/lib/jenkins/jobs/MerchantManager/workspace
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/***/epgDataPoc # timeout=10
Fetching upstream changes from https://github.com/***/epgDataPoc
> git --version # timeout=10
using .gitcredentials to set credentials
> git config --local credential.helper store --file=/tmp/git5734173224303904567.credentials # timeout=10
> git fetch --tags --progress https://github.com/***/epgDataPoc +refs/heads/*:refs/remotes/origin/*
> git config --local --remove-section credential # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8bced4d8fecd4135bcb4868aa4011e140adf46d3 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 8bced4d8fecd4135bcb4868aa4011e140adf46d3
> git rev-list 8bced4d8fecd4135bcb4868aa4011e140adf46d3 # timeout=10
[workspace] $ /bin/bash -xe /tmp/hudson1400066644804618273.sh
+ source /var/lib/jenkins/.bashrc
++ [[ -s /var/lib/jenkins/.rvm/scripts/rvm ]]
++ source /var/lib/jenkins/.rvm/scripts/rvm
+++ builtin test -n '3.2.25(1)-release' -o -n ''
+++ case "`uname`" in
++++ uname
++++ command ps -p 29628 -o ucomm=
++++ ps -p 29628 -o ucomm=
+++ __shell_name=bash
+++ case "$__shell_name" in
+++ unset __shell_name
+++ __rvm_has_opt posix
+++ [[ -n '' ]]
+++ [[ -n 3.2.25(1)-release ]]
+++ [[ :braceexpand:errexit:hashall:interactive-comments:xtrace: =~ :posix: ]]
+++ return 1
+++ export HOME rvm_prefix rvm_user_install_flag rvm_path
+++ HOME=/var/lib/jenkins
+++ [[ -n '' ]]
++++ umask
+++ export rvm_stored_umask=0022
+++ rvm_stored_umask=0022
+++ (( 0 == 0 ))
+++ rvm_rvmrc_files=("/etc/rvmrc" "$HOME/.rvmrc")
+++ [[ -n '' ]]
+++ for rvmrc in '"${rvm_rvmrc_files[#]}"'
+++ [[ -f /etc/rvmrc ]]
+++ GREP_OPTIONS=
+++ command grep '^\s*rvm .*$' /etc/rvmrc
Build step 'Execute shell' marked build as failure
Publishing rails stats report...
hudson.model.StreamBuildListener#1bb557c8
Finished: FAILURE
I fixed this by trial and error:
By swapping these lines in .bashrc:
#/var/lib/jenkins/.bashrc
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
And making the following changes to my build script:
#!/bin/bash
source $JENKINS_HOME/.bashrc
rvm use ruby-2.1.2#datapoc
# Set "fail on error" in bash
set -e
# Do any setup
# e.g. possibly do 'rake db:migrate db:test:prepare' here
#cp config/database.example.yml config/database.yml
bundle install
rake db:schema:load
rake
Related
I have a rails site, which I deploy via ssh using a git post-receive hook. When I ssh into the server and run bundle install it runs correctly under the specified ruby version of 2.2.2. However, when I push to the server from my local machine and it hits the 'bundle install command', I get the following:
hooks/post-receive: /usr/local/bin/bundle: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory
I can't find for the life of me why it is pointing to ruby1.9.1. This directory does not exist. I do see a directory for ruby2.3 in that directory, but not ruby2.2.2 which is the correct directory. Something is quite fouled up, but I can't figure how to fix it. Anyone seen anything like this?
UPDATE: Here is my post-receive hook, as per the request below...
#!/bin/bash
GIT_DIR=/home/deploy/www_production
WORK_TREE=/home/deploy/www
export MGOTS_DATABASE_USER='user'
export MGOTS_DATABASE_PASSWORD='pass'
export RAILS_ENV="production"
. ~/.bash_profile
while read oldrev newrev ref
do
if [[ $ref = refs/heads/master ]];
then
echo "Master ref received. Deploying master branch to production..."
mkdir -p $WORK_TREE
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log
# start deploy tasks
cd $WORK_TREE
bundle install
rake db:create
rake db:migrate
rake assets:precompile
rake requests:cleanup
sudo restart puma-manager
sudo service nginx restart
# end deploy tasks
echo "Git hooks deploy complete"
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
UPDATE: For the sake of clarity, as the answer points to the correct place to find the answer, but doesn't state it exactly, I am posting my updated hook file here. You can see the difference between this one and the one above, and that is what solved the problem. Please note that the path to the rvm directory can be found by typing the command: which rvm - that's the one you want to point to.
#!/bin/bash
GIT_DIR=/home/deploy/www_production
WORK_TREE=/home/deploy/www
export MGOTS_DATABASE_USER='user'
export MGOTS_DATABASE_PASSWORD='pass'
export RAILS_ENV="production"
export RUBYGEMS_GEMDEPS="/home/deploy/.rvm/ruby-2.2.2#www/gems"
. ~/.bash_profile
[[ -s "/usr/share/rvm/bin/rvm" ]] && source "/usr/share/rvm/bin/rvm"
while read oldrev newrev ref
do
if [[ $ref = refs/heads/master ]];
then
echo "Master ref received. Deploying master branch to production..."
mkdir -p $WORK_TREE
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log
# start deploy tasks
cd $WORK_TREE
bundle install
rake db:create
rake db:migrate
rake assets:precompile
rake requests:cleanup
sudo restart puma-manager
sudo service nginx restart
# end deploy tasks
echo "Git hooks deploy complete"
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
You need to load RVM functions to the shell script. link
Or just switch to Rbenv :)
First, set your default ruby to use the version 2.2.2
Are you using RVM? For RVM its: rvm use --default 2.2.2
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
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.
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
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