Sometimes invoking a task in Capistrano requires to input data. A very comfortable approach is to use highline since it is build for this purpose.

    task :ask_for_environment do
      set :environments, [:production,:testing,:development]
      set(:environment, Capistrano::CLI.ui.choose do |menu|

          environments.each do |choice|
            menu.choice choice do set(:environment,choice)

      ) unless exists?(:environment)

      unless environments.include?(environment.to_sym)  
        puts "".rjust(70,'-') << "\nERROR: environment \"#{environment}\" is NOT a valid option\n" << "".rjust(70,'-')
        exit 1

      puts "#{environment}"      

I think this code is self explanatory (more or less ;-). Capistrano::CLI.ui. returns a highline object we can work with. For more examples on how to use highline visit the highline svn repo This can then be combined with any task you like through the before hook

before "deploy", "ask_for_environment"

The unless exists?(:environment) lets one bypass the menu through setting the parameter at the invocation line ( -s environment=production)

Interested in Cloud or Chef Trainings? Have a look at our Commandemy Trainings page. Need help migrating to the cloud? Check out Infralovers.

comments powered by Disqus
Blog Tags