Jenkins and running rake tasks - ruby

I'm having some issues running a rake task from within Jenkins (within a docker container) as part of a build process (I have the Rake plugin installed). I am getting the error
java.io.IOException: Cannot run program "rake" (in directory "/var/jenkins/workspace/HendricksFeaturesCopy"): error=2, No such file or directory
Which i don't understand as when i pwd $ECHO before the rake task is invoked i get.
/var/jenkins/workspace/HendricksFeaturesCopy
So i'm in the correct place and rake is installed as its located here
/usr/local/rvm/rubies/ruby-2.3.0/bin/rake
My Rakefile looks like
import 'lib/tasks/yard-docs.rake'
lib/tasks/yard-docs.rake
require 'yard'
namespace :yard_docs do
desc 'Generate Yard Documentation'
task :generate do
# Generate Yard Documentation
end
end
Does anyone know how to rectify this or what I am missing?
UPDATE
After doing echo $PATH
/usr/local/rvm/gems/ruby-2.3.0/bin:/usr/local/rvm/gems/ruby-2.3.0#global/bin:/usr/local/rvm/rubies/ruby-2.3.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/rvm/bin
So this means the correct gemset is being used and when I have done rvm #global do gem list
rake (10.4.2)
So, rake is available right ?

Make sure /usr/local/rvm/rubies/ruby-2.3.0/bin/ is in your $PATH.
PATH=/usr/local/rvm/rubies/ruby-2.3.0/bin/:$PATH
Alternatively, symlink /usr/local/rvm/rubies/ruby-2.3.0/bin/rake to /usr/local/bin/
ln -s /usr/local/rvm/rubies/ruby-2.3.0/bin/rake /usr/local/bin/

Related

Execute without `bundle exec` via rubygems-bundler

I have a standard gem scaffold, and in inside the bin directory I have a simple executable (bin/do_things.rb):
#!/usr/bin/env ruby
require 'my_gem'
MyGem::doThings()
The gem hasn't been installed via gem install, so running bin/do_things.rb without bundle exec fails (my Gemfile has the gemspec line in it).
Is there a simple way to have rubygems-bundler execute my script in the bundler context? Should I just modify the $LOAD_PATH in my executable instead?
I could create a wrapper script to execute under Bundler as well, but I'd rather leverage rubygems-bundler somehow.
Or should I just type bundle exec?
try:
bundle exec bash -l
it will set you into bundler context - it's an extra shell so running exit will get you back to bundler less context.
Change the file permission
chmod a+x bin/do_things.rb
and resolve bin path, ignoring symlinks
require "pathname"
bin_file = Pathname.new(__FILE__).realpath
post, add self to libpath
$:.unshift File.expand_path("../../lib", bin_file)
Generating binstubs via bundle install --binstubs creates a wrapper script for all executables listed in my gemspec.
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'do_things.rb' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('my_gem', 'do_things.rb')
However, by default, the binstubs path is bin, which conflicts with gem's executable path, and will overwrite files in bin.
Running bundle install --binstubs=SOME_DIR and then adding SOME_DIR to .gitignore seem to be the most maintainable way.
Then, I can simple execute SOME_DIR/do_things or any other project-specific executable I add down the line.

rake install does not work for octopress

I am trying to setup rake via this doc.
http://octopress.org/docs/setup/
But I get some errors.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ rake install
## Copying classic theme into ./source and ./sass
mkdir -p source
rake aborted!
Permission denied - source
Tasks: TOP => install
(See full trace by running task with --trace)
With sudo I get this output.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ sudo rake install
rake aborted!
no such file to load -- bundler/setup
(See full trace by running task with --trace)
Here is a list of files in the directory.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ ls -a
. config.ru .git Rakefile .slugignore
.. _config.yml .gitignore .rbenv-version .themes
CHANGELOG.markdown Gemfile plugins README.markdown
config.rb Gemfile.lock .powrc .rvmrc
How can I solve the problem?
Ikhthiandor: Looks like you are a beginner in the ruby/rails world.
By running the rake install command, you are installing the default octopress theme using the rake tool. Not trying to setup rake as you mention in the question.
The first error ( Permission denied - source when trying mkdir -p source - as you correctly guessed - is because the user doesn't have permissions to create that directory.
The second error ( no such file to load -- bundler/setup ) is because the previous install dependencies steps are not performed correctly (for the user running this command).
The install dependencies steps to be completed successfully are:
1. gem install bundler
2. rbenv rehash # If you use rbenv, rehash to be able to run the bundle command
3. bundle install
I am guessing you ran these steps successfully as 'ikhthiandor' user, so the bundler gem is not available for the 'sudo' user.
You can fix this by either of the following options:
changing permissions on /opt/octopress folder so that 'ikhthiandor' user has rights to create sub-folders/files within.
Run all the commands in the Octopress Setup doc as 'sudo'
Best practice would be to use either rvm or rbenv to manage custom installations of ruby environments per user (and not do everything as super user).
If you are indeed a fresher in the ruby-rails world, and want to step up your knowledge of the tools and the best practices in the ruby/rails world, I suggest browsing through the first few chapters of the Ruby on Rails Tutorial book that is available for free on-line.
HTH

Capistrano: run_locally in separate project

I'm using capistrano for deployment. I'm trying to define a task which will:
cd other-rails-app && bundle exec rake sometask
But it's complaining about Rake not being part of the bundle (which is obviously not true in the project where I am trying to run this task). I think it must be altering the environment somehow before executing the command and assuming bundler is already loaded (which is true in the capistrano task, but it's the wrong Gemfile).
How do I run a command locally with capistrano with a clean environment?
I figured out the problem. When you run capistrano, the BUNDLE_GEMFILE environment variable gets set to the Gemfile of the current project. So when you run bundle exec rake, Bundler looks first to the Gemfile set in the environment. The solution is to "reset" this Gemfile variable. I created a script for running my rake task in the root of my other project.
So instead of doing:
run_locally "cd other-rails-app && bundle exec rake mytask"
I do this:
run_locally "cd other-rails-app && ./mytask.rb"
In the mytask.rb file I do this:
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] = File.expand_path('Gemfile', File.dirname(__FILE__))
require 'bundler/setup'
%x{rake mytask}

Capistrano + RVM + Rake task

I have a project which
uses rvm ruby 1.9.2 (set in .rvmrc in project dir).
deploying with capistrano.
has a rake task I want to run remotely from my local machine by capistrano.
I've created a .sh file to run my task:
cd /var/www/pluslook/current
/home/kirill/.rvm/scripts/rvm use 1.9.2#pluslook
/home/kirill/.rvm/gems/ruby-1.9.2-p180#pluslook/bin/rake parse:feed RAILS_ENV="production" --trace
But when I'm trying to run this task i have an error:
Using /home/kirill/.rvm/gems/ruby-1.9.2-p180 with gemset pluslook
Could not find linecache19-0.5.12 in any of the sources
Run `bundle install` to install missing gems.
I've installed all my gems in project's current directory so it looks like rake task is running from another directory. When I'm trying to run task from capistrano, It shows me the same error.
Thank you and sorry for my English:)
do you have 'require "bundler/capistrano"' in config/deploy.rb
did you run "bundle install" before commit latest code changes?

rake command looks for Rakefile up the directory chain?

I am using Rails3 and accidentally I ran rake command from within lib directory. rake command successfully ran. I think rake command looks for Rakefile all the up the chain. Is that true?
Yes. According to http://docs.rubyrake.org/user_guide/chapter02.html you can avoid that by:
rake -N

Resources