Search code examples
ruby-on-railscapistrano

How to send a message to workstation issuing Capistrano task?


If I execute cap deploy from a workstation how can I specify custom feedback to be displayed in the workstation's console as the recipe runs?

It's confusing to me because Capistrano runs its 'tasks' on the deployment machines (or servers) so that run 'echo "this is my message"' would just appear in the server's console.


Solution

  • Can't you just use puts/print?

    Shell commands

    Anyway, if you need to run commands in the workstation issuing the task, you should use the backtick operator. Even Capistrano documentation recommends it. See the Caveats section, at the bottom of this page.

    Quoting:

    The run helper may not be appropriate for running commands which do not need to be executed remotely. In such instances, it may be more appropriate to utilize the backtick (```) instead.

    The backtick operator executes system commands and returns the output to you. For example, if you want to execute echo "this is my message", you would do: `echo "this is my message"`

    That would call the echo "system command", and return the output to you: "this is my message\n". But it wouldn't print it to the console, which is what you want.

    To print it, you should use: puts `echo "this is my message"`
    But that's redundant, you might as well call puts or print directly.
    See this link for more information about executing shell commands in Ruby.

    Puts and print

    Puts and print prints a message to the console that is running the Ruby code. That seems like what you're trying to achieve. In your case, you should just do:

    puts "this is my message"
    # or, depending on what you want
    print "this is my message"
    

    puts is like print, but it adds a new line to the end of the string. This:

    puts "my message"
    

    Is the same as this:

    print "my message\n"
    

    For some documentation, follow these links:

    Anyway

    Just use puts "this is my message" and be happy.