This makes your life easier in your development process. Setup it once at the beginning and then just modify your needs as they come up. This is also an Agile process to continuous inspect and adapt your Continuous Delivery strategy. My next post will cover how to setup Jenkins to deploy automatically after a successful build.

This post is just a shorter version with extension from the beanstalkapp blog.

Install Capistrano and Capistrano-ext

Add the following to your Gemfile and execute “bundle update” or “bundle install”

1

2

$gem'capistrano'

$gem'capistrano-ext'

Prepare your Application for Capistrano

Navigate to your Root folder of your application and execute following command that creates a special file called Capfile and ads an template deployment recipe at config/deploy.rb. Delete the content of the deploy.rb file.

1

$capify.

Create a Capistrano Recipe

In our empty deploy.rb file we will add the name of our application

1

set:application,"MySuperApp"

Also add the access to the repository

1

2

3

set:scm,:git

set:repository,"git@account.beanstalkapp.com:/repository.git"

set:scm_passphrase,""

Add the following SSH option to connect to your EC2 instance:

1

ssh_options[:forward_agent]=true

add the user on our server that we want Capistrano to run commands with

1

set:user,"server-user-name"

i left this empty and it worked.

So, because we maybe have different Servers, example for Testing and Production, or different settings to deploy from local and to deploy from a remote server, lets add the Capistrano Multistage function.

Add this to top of your deploy.rb file

1

require'capistrano/ext/multistage'

Then lets specify our “stages” or “settings” and say that staging is our default setup.

1

2

set:stages,["staging","production"]

set:default_stage,"staging"

Now we have to create files for each staging. Create for that a folder called “deploy” in your config directory and add the two files “production.rb” and “staging.rb” to it. To make it easier, add the following to both files:

1

2

server"myEC2Server.com",:app,:web,:db,:primary=>true

set:deploy_to,"/var/www/MySuperApp"

and you need to tell Capistrano where your pem file lies to connect to your server and also say him which user it is to connect to your server.

1

2

set:user,"ubuntu"

ssh_options[:keys]="~/.ssh/MyPemFile.pem"

Validate your Recipe

Lets create an initial directory structure on your server for future deployments. Run the following command:

1

$cap deploy:setup

Lets check if everything is setup correctly

1

$cap deploy:check

if you have some errors or problems, fix it and then go to next chapter.

Deploy

Now, if you want to deploy to our staging server, simply type the following command. Remember, staging is our default setup.

1

$cap deploy

if you want to deploy to production server:

1

$cap production deploy

So that’s it. In the next post, i will explain how to setup Jenkins to deploy after a successful build.