Capifony – A simple way to use it in many environments

- Maxime Teneur - [View Comments]

At la Netscouade, we use Capifony (1), a Symfony-oriented Capistrano release, to deploy our developments on our servers.

Credits Capifony.org

Credits www.capifony.org

During the project life cycle, we have to release and deploy different states of our developments on different physical servers: staging, pre-production, production (and so on…).

On each new deployment, we get to tag a new version (SVN) of our developments and to deploy it using Capifony.

The default Capifony/Capistrano config and doc do not explain how to deploy easily in various server and environments configurations (i do not know Ruby)… Here is a simple example to deal with many server environments using Capifony and to reduce the time needed to set up each new deployment (2).

To avoid editing our Capifony config files each time we want to change environment (staging, pre-prod etc.), we will set different server configurations. Before deployment, the deployer will have to choose which configuration he wants to use.

Each time the deployer will execute ‘cap deploy’, he will be prompted to set:

  • Which SVN tag he wants to deploy
  • Which server configuration he wants to use

Prerequisites for our example:
We are using Symfony 1.4 with subversion versioning,
We have 2 differents environments : production and staging,
The project has already been ‘capify’-ed (See Capifony docs to do that …),
Before each deploy, you set a new tag in your SCM.

Config files structure

We will not follow the official capifony doc, but instead create a ‘capifony’ directory in our Symfony project config.
As we have 2 different environments, we use 2 Capifony files: prod.rb and staging.rb.

Each file will have 2 purposes:

  • deploy.rb will contains all project related configuration (subversion, project name etc…)
  • env config files (prod.rb and staging.rb) will contains all servers and symfony environment configuration

Config file content

  1. We have to change the default root file ‘Capfile’. Usually, this file calls deploy.rb in root config directory. We will want to edit this to use the config/capifony directory instead. Simply edit ‘Capfile’, and change last line: 
    load 'config/capifony/deploy'
    
  2. Now, we have to change deploy.rb  to ask the deployer which SVN version and which server environment he wants to use. Many other stuff can be configured here…
    #config/capifony/deploy.rb
    set :symfony_apps, ['main', 'back']
    set :application, "your app name"
    set :repository,  "your svn server url"
    set :scm, :subversion
    # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
    set :scm_username, "username"
    set :scm_password, "passwd"
    
    ###########################
    #STUFF IS HERE
    #
    #Here we ask deployer which svn tag he wants to use
    ######
    
    set(:tag) { Capistrano::CLI.ui.ask("Tag to use for deployment  ") }
    set(:repository) { (tag == "trunk") ? "#{repository_root}/trunk" : "#{repository_root}/tags/#{tag}" }
    # Set the deployment directory on the target hosts.
    set :deploy_via, :export
    
    ##################
    #
    #STUFF IS HERE
    #Now we ask which server to use for deployement
    ######
    
    set(:my_server_env) { Capistrano::CLI.ui.ask("Env to use for deploy (prod | staging) => prod is default): ") }
    #Now we load the related config file
    load "config/capifony/#{my_server_env}.rb
    
  3. Edit  your env config files with server and symfony  env config values.For example prod config
    #config/capifony/prod.rb
    set :symfony_env_prod, 'prod'
    set :deploy_to,   'your server path'</code></code>
    role :web,        'your web server IP / name'
    #Your HTTP server, Apache/etc
    role :app,        'your web server IP / name'                         # This may be the same as your `Web` server<code lang="rb">
    role :db,         'your DB server IP / name', :primary => true       # This</code>
    

From now on, each time you will run your ‘cap:deploy’ script, you will be asked which SVN tag and server environment you want to use.

That’s it!

thx to @AimeAile for his fast and furious read.

(1)Capifony is developped by KnpLabs and Konstantin Kudryashov and is released under open source license.
From README:
Deploying symfony Applications with Capistrano
Capistrano is an open source tool for running scripts on multiple servers. It’s primary use is for easily deploying applications. While it was built specifically for deploying Rails apps, it’s pretty simple to customize it to deploy other types of applications. We’ve been working on creating a deployment “recipe” to work with symfony applications to make our job a lot easier.

(2)  there may be a more natural way using Capistrano…but Ruby is not my fun ;) …Remember the tiltle, our solution is simple and is just an example

  • http://www.zalas.eu/ Jakub Zalas

    Hi, nice post! There is more natural way of achieving the same by using capistrano’s multistage extension. Some time ago I wrote a blog post about it: http://www.zalas.eu/multistage-deployment-of-symfony-applications-with-capifony

  • http://profiles.google.com/nichevallier Chevallier Nicolas

    I was using ant for deployement, but I’m searching for a new way. Capistrano seems to be an excellent deployement tool.

blog comments powered by Disqus