Undistraction
Undistraction

Reputation: 43401

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?

Upvotes: 16

Views: 13990

Answers (2)

bridiver
bridiver

Reputation: 1714

ENV['EXAMPLE'] = 'something'
Rake::Task['db:rebuild'].invoke

Upvotes: 31

Undistraction
Undistraction

Reputation: 43401

Use system instead of back-ticks:

system("EXAMPLE=something rake db:rebuild")

Upvotes: 4

Related Questions