Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass environmental variables to a Rake task invoked from another Rake task?

I have three Rake tasks invoked from another Rake task. The first Rake task requires that an environmental variable is set before it is executed.

The following works, however it means that I lose all the output from the task which is critical:

namespace :deploy do

  task :staging => :environment do
    `EXAMPLE=something rake db:rebuild`
    Rake::Task["rake envs:push:staging"].invoke
    Rake::Task["rake app:push:staging"].invoke
  end

end

How can I invoke the first task with the environmental variable AND display its output to the terminal?

like image 578
Undistraction Avatar asked Feb 03 '14 18:02

Undistraction


People also ask

How do you call a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

What is Rakefile?

Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax. Rake has the following features: Rakefiles (rake's version of Makefiles) are completely defined in standard Ruby syntax. No XML files to edit.


2 Answers

ENV['EXAMPLE'] = 'something'
Rake::Task['db:rebuild'].invoke
like image 72
bridiver Avatar answered Oct 13 '22 02:10

bridiver


Use system instead of back-ticks:

system("EXAMPLE=something rake db:rebuild")
like image 28
Undistraction Avatar answered Oct 13 '22 00:10

Undistraction