Running foreman from a rake task

2.6k views Asked by At

I have the following Rake task:

namespace :foreman do
  task :dev do
    `foreman start -f Procfile.dev`
  end
end

desc "Run Foreman using Procfile.dev"
task :foreman => 'foreman:dev'

The forman command works fine from the shell, however when I run rake foreman I get the following error:

/Users/me/.gem/ruby/2.0.0/gems/bundler-1.5.2/lib/bundler/rubygems_integration.rb:240:in `block in replace_gem': foreman is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
    from /Users/me/.gem/ruby/2.0.0/bin/foreman:22:in `<main>'

Forman specifically states:

Ruby users should take care not to install foreman in their project's Gemfile

So how can I get this task to run?

4

There are 4 answers

0
SciPhi On BEST ANSWER

If you must make it work via rake, try changing the shell-out via backtick to use a hard-coded path to the system-wide foreman binary

`/global/path/to/foreman start -f Procfile.dev`

You just need to use 'which' or 'locate' or a similar tool to determine the path that works outside your bundler context. If you are using rbenv, then this might be sufficient :

$ rbenv which rake
/home/name/.rbenv/versions/1.9.3-p448/bin/rake

I hope that helps you move forward.

3
dax On

if it has to be a rake task, try this (from this answer):

namespace :foreman do
  task :dev do
    sh "foreman start -f Procfile.dev"
  end
end

if it doesn't have to be a rake task, I have a simple bash script to start for a specific project that works well:

#!/bin/bash

export PROJECT_DIR=`pwd`
export PORT=$1

source "$HOME/.rvm/scripts/rvm"

unset BUNDLE_GEMFILE
unset BUNDLE_BIN_PATH
unset RUBYOPT
unset GEM_HOME
unset GEM_PATH

(cd <project full path> && exec foreman start -p $PORT)
4
Anshul Goyal On

Not sure if this will work, but you could export the environment variables associated with your shell explicitly and then make a call to foreman. FWIW, I don't think this is recommended, and would suggest using a bash script as @dax proposes.

Steps

  1. Get the $PATH and other environment variables from your shell

    printenv >> shell.env
    
  2. Get the environment variables from the rails environment

    namespace :foreman_test do
      task :dev do
        `printenv >> rails.env`
      end
    end
    
  3. Compare the two and find out the changed environment variables, and set them up in your rake task in the system call

    namespace :foreman do
      task :dev do
        `export PATH=/original/path:/value && GEM_DIR=/some/folder && foreman start -f Procfile.dev`
      end
    end
    
0
Undistraction On

A little way down the line and here is how I do it suing chruby:

namespace :foreman do
  task :dev do
    # 1. Load .bash_profile to ensure chruby is available
    # 2. Switch the ruby version to the one defined in .ruby-version
    # 3. Start foreman
    system %(
      source ~/.bash_profile
      chruby "$(cat .ruby-version)"
      foreman start --procfile Procfile.dev
    )
  end
end

desc "Run Foreman using Procfile.dev"
task :foreman => 'foreman:dev'