How to set up Rails build on Cloudbees? - ruby-on-rails

I am trying to set up a Rails job on Cloudbees and I'm running into problems that I can't solve anymore. I tried to follow the instructions but didn't find enough details to find out what's wrong myself.
This is how the Shell execution section of my job currently looks like:
curl -s -o use-ruby https://repository-cloudbees.forge.cloudbees.com/distributions/ci-addons/ruby/use-ruby
RUBY_VERSION=1.9.3-p327 source ./use-ruby
bundle install --path vendor/bundle
bundle exec rake db:test:create
bundle exec rake test
I'm actually not sure yet about the last 2 lines, since I never made it past line #3 ;)
When I run this, I get the following result:
(...)
Commencing build of Revision 2e1549004e2b49903d4d006ad8bfa7e4b4bfda08 (origin/master)
Checking out Revision 2e1549004e2b49903d4d006ad8bfa7e4b4bfda08 (origin/master)
[API Continuous Testing] $ /bin/sh -xe /tmp/hudson4854632522106638677.sh
+ curl -s -o use-ruby https://repository-cloudbees.forge.cloudbees.com/distributions/ci-addons/ruby/use-ruby
+ RUBY_VERSION=1.9.3-p327
+ source ./use-ruby
++ [[ -z 1.9.3-p327 ]]
++ ruby187='1.8.7-*'
++ [[ 1.9.3-p327 == 1.8.7-* ]]
++ '[' '!' -f /scratch/jenkins/use.sh ']'
++ . /scratch/jenkins/use.sh
++ mkdir -p /scratch/jenkins/ruby
+++ addon_architecture
+++ uname -m
++ ruby_name=ruby-1.9.3-p327-x86_64
++ '[' '!' -f /scratch/jenkins/ruby/ruby-1.9.3-p327-x86_64.tar.bz2 ']'
++ '[' '!' -f /scratch/jenkins/ruby/ruby-1.9.3-p327-x86_64/bin/ruby ']'
++ export PATH=/scratch/jenkins/ruby/ruby-1.9.3-p327-x86_64/bin:/opt/jdk/jdk1.6.latest/bin:/opt/jdk/latest/bin:/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/opt/maven/latest/bin:/opt/ant/latest/bin:/opt/android/android-sdk-linux/tools:/opt/gradle/latest/bin:/home/jenkins/bin:/opt/jdk/jdk.latest/bin
++ PATH=/scratch/jenkins/ruby/ruby-1.9.3-p327-x86_64/bin:/opt/jdk/jdk1.6.latest/bin:/opt/jdk/latest/bin:/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/opt/maven/latest/bin:/opt/ant/latest/bin:/opt/android/android-sdk-linux/tools:/opt/gradle/latest/bin:/home/jenkins/bin:/opt/jdk/jdk.latest/bin
++ unset GEM_PATH
++ unset GEM_HOME
++ unset BUNDLE_PATH
+ bundle install --path vendor/bundle
/tmp/hudson4854632522106638677.sh: line 5: bundle: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I assumed that after the Cloudbees installer is finished, "bundle" would be available for me to execute. Is that not right? Or am I missing anything?

Bundler is not included by default as there are many versions (and some people prefer a newer / older / pre-release version).
In regards the following bcrypt issue, it's caused by spaces in the job name. I've renamed the job in question and it now passes.
I've also added the PostgreSQL installer so your full install now completes successfully.
You should refer to http://wiki.cloudbees.com/bin/view/DEV/PostgreSQL for more information on PostgreSQL configuration (preparing the users and databases).

I think you may need to install bundler, as if it was a "normal" ruby installation:
gem install bundler
As the first thing (as it doesn't come with ruby itself)
(although I can see why a "use-ruby" script could be expected to do that - I will see if it can).

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.

Bundler Trying To Run Under Wrong Version of Ruby

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

Add local binstubs to rbenv (getting command not found)

If you have simple shell command binstubs in the local bin directory of a Rails project (e.g. not generated by or running a gem), rbenv seems to have trouble executing them. For example, #tpope's heroku binstubs generate a binstub named production in the local bin directory:
#!/bin/sh
HEROKU_APP=myapp-production HKAPP=myapp-production exec "${HEROKU_COMMAND:-heroku}" "$#"
After an rbenv rehash, the production command shows up in the ~/.rbenv/shims directory looking something like this:
#!/usr/bin/env bash
set -e
[ -n "$RBENV_DEBUG" ] && set -x
program="${0##*/}"
if [ "$program" = "ruby" ]; then
for arg; do
case "$arg" in
-e* | -- ) break ;;
*/* )
if [ -f "$arg" ]; then
export RBENV_DIR="${arg%/*}"
break
fi
;;
esac
done
fi
export RBENV_ROOT="/Users/Username/.rbenv"
exec "/usr/local/Cellar/rbenv/0.4.0/libexec/rbenv" exec "$program" "$#"
So executing which production gives you:
/Users/Username/.rbenv/shims/production
But executing rbenv which production (or trying to run the command) gives you:
rbenv: production: command not found
I'm new to rbenv so maybe I missed a config step?
Apparently project-specific binstubs in Rails projects should be kept outside of the local bin/ directory since those are primarily for application scripts. So one approach (i.e. that used by the rbenv-binstubs plugin) is to keep local binstubs separate and override rbenv shims with that local binstub directory (e.g. .bundle/bin). So by using the plugin you might see this result from which production:
/Users/Username/.rbenv/shims/production
But if you move the production binstub to .bundle/bin, then rbenv which production should yield:
{PROJECT_ROOT}/.bundle/bin/production

RVM not loaded in Jenkins Shell Environment

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

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.

Resources