16

I have defined a custom Capistrano task that's supposed to run locally (on my development machine):

desc "Push code to Dreamhost"
task :push do
  run "git push dreamhost"
end

however when I try to run cap push it executes it on the remote machine, ie.

* executing `push'
* executing "git push dreamhost"
  servers: ["ec2-999-99-999-999.compute-1.amazonaws.com"]

how do I get it to execute locally instead?

Daniel
  • 1,515
  • 3
  • 10
  • 13
  • Why do you want it to be a capistrano task instead of a regular rake task? – KL-7 Jan 01 '12 at 10:23
  • 1
    I actually want to do it in `before :deploy`, and that gives the same problem (tries to execute on remote server). – Daniel Jan 01 '12 at 10:42

4 Answers4

89

Or use run_locally to run natively with Capistrano, and still get proper logging and all that good stuff

jeffbyrnes
  • 2,182
  • 2
  • 22
  • 33
patcon
  • 1,677
  • 1
  • 12
  • 9
17

I suggest using :

system("git push dreamhost")

or

output = %x[git push dreamhost]

That's just plain Ruby !

Cydonia7
  • 3,744
  • 2
  • 23
  • 32
3

For the commenter that mentioned run_locally doesn't show output, you have to dump the output to a variable and then print it to see it. Like this:

task :testing_run_locally do
    output = run_locally "hostname"
    puts "OUTPUT: " + output
end

The downside is you won't see any output until the command has finished. Not a big deal for commands that don't run long but something that runs for several minutes will cause the deploy to appear like it is hung until it finishes. There is an open pull request for Capistrano that adds real time command output to run_locally: https://github.com/capistrano/capistrano/pull/285

1

You can also use:

require 'rake'     # Access to sh command
[...]
desc "Push code to Dreamhost"
task :push do
   sh "git push dreamhost"
end
Sebastien Varrette
  • 3,876
  • 1
  • 24
  • 21