Capistrano 3.0 : Starting : atomic operations

When you run cap, Capistrano dutifully connects to your server(s) via SSH and executes the steps necessary to deploy your project. You can define those steps yourself by writing Rake tasks, or by using pre-built task libraries provided by the Capistrano community.

Include the gem in Gemfile

group :development do
  gem 'capistrano', '~> 3.1'
end

We will start from creating simple tasks

Capistrano 3.0 is very different from its ancestors. So the syntax is different

# Simple tasks in previous versions
# config/deploy.rb
# 
task :hello do
  puts 'Hello'
end

task :world do
  puts 'world'
end

$ cap hello

but in v3.0 and greater

# add extra few lines in deploy.rb
after :deploy, :hello
after :hello, :world
$ cap production deploy
Hello
world

Calling some task from another task

task :world do
  puts 'world'
  some_task
end

def some_task
  puts 'some task'
end

Ask values to Variable via user Inputs

When using ask to get user input, you can pass echo: false to prevent the input from being displayed.

ask(:value_from_input, 'default_value')

# You use `echo: false` if you are typing some `secrets`
ask(:some_secret, 'default_value', echo: false)

Setting Variables for later use

`set` method is used to set and to get the value `fetch` method is used

set :server_name, 'My App server'
# use a lambda to delay evaluation
set :application, -> { "SomeThing_#{fetch :other_config}" }
task :world do 
  puts 'world' 
  puts "Server name is #{fetch(:server_name)}" 
end

Fetch Values from Variables

A value can be retrieved from the configuration at any time:

fetch :application
# => "MyLittleApplication"

fetch(:special_thing, 'some_default_value')
# will return the value if set, or the second argument as default value

Execute Commands on server

$ execute(:bundle, :install)

and

$ execute('bundle install')

don’t behave identically.

for example, execute(:bundle, :install)  the first argument is a String with no whitespace. This allows the command to pass through the SSHKit::CommandMap which enables a number of powerful features.

So I prefer the former one

execute 'echo', "'Hello World' > ~/hello"

Note: `execute` needs to be executed inside `on` block like

on roles(:all) do |host|
  # Should create a file called `hello` in home dir in my server
  execute 'echo', "'Hello World' > ~/hello"
end

All backends support the execute(*args), test(*args) & capture(*args) methods for executing a command. You can call any of these methods in the context of an on() block.

Sources:

https://github.com/capistrano/capistrano

http://railscasts.com/episodes/133-capistrano-tasks-revised?view=asciicast

https://github.com/capistrano/sshkit

http://capistranorb.com/documentation/getting-started/user-input/

http://capistranorb.com/documentation/getting-started/configuration/

http://capistranorb.com/documentation/getting-started/tasks/

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s