deploymentcapistrano

Passing parameters to Capistrano


I'm looking into the possibility of using Capistrano as a generic deploy solution. By "generic", I mean not-rails. I'm not happy with the quality of the documentation I'm finding, though, granted, I'm not looking at the ones that presume you are deploying rails. So I'll just try to hack up something based on a few examples, but there are a couple of problems I'm facing right from the start.

My problem is that cap deploy doesn't have enough information to do anything. Importantly, it is missing the tag for the version I want to deploy, and this has to be passed on the command line.

The other problem is how I specify my git repository. Our git server is accessed by SSH on the user's account, but I don't know how to change deploy.rb to use the user's id as part of the scm URL.

So, how do I accomplish these things?

Example

I want to deploy the result of the first sprint of the second release. That's tagged in the git repository as r2s1. Also, let's say user "johndoe" gets the task of deploying the system. To access the repository, he has to use the URL johndoe@gitsrv.domain:app. So the remote URL for the repository depends on the user id.

The command lines to get the desired files would be these:

git clone johndoe@gitsrv.domain:app
cd app
git checkout r2s1

Solution

  • Update: For Capistrano 3, see scieslak's answer.


    As jarrad has said, capistrano-ash is a good basic set of helper modules to deploy other project types, though it's not required as at the end of the day it's just a scripting language and most tasks are done with the system commands and end up becoming almost shell script like.

    To pass in parameters, you can set the -s flag when running cap to give you a key value pair. First create a task like this:

    desc "Parameter Testing"
    task :parameter do
      puts "Parameter test #{branch} #{tag}"
    end
    

    Then start your task like so:

    cap test:parameter -s branch=master -s tag=1.0.0
    

    For the last part, I would recommend setting up passwordless access using ssh keys to your server, but if you want to take it from the current logged in user, you can do something like this:

    desc "Parameter Testing"
    task :parameter do
      system("whoami", user)
      puts "Parameter test #{user} #{branch} #{tag}"
    end
    

    UPDATE: Edited to work with the latest versions of Capistrano. The configuration array is no longer available.

    Global Parameters: See comments Use set :branch, fetch(:branch, 'a-default-value') to use parameters globally. (And pass them with -S instead.)