39

I am wondering how I can target a specific commit SHA in Git for deployment, using Capistrano? It should be something like

cap deploy --version=<sha targeted>

Can't seem to find the answer to this after a lot of searching.

4 Answers 4

64

For Capistrano 2.9 until 3.0:

cap -S revision=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy

For older versions of Capistrano, you can deploy a particular git commit/tree/branch/tag by doing this:

cap -s branch=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy

In some cases there may be a need of specifying the Environment as an argument as well. production is just an example.

cap production -S revision=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy
6
  • is that the SHA for a specific commit? May 27, 2009 at 21:45
  • 1
    The branch will be passed to git with "git checkout <branch>", so a commit id should work just as fine as a tree id, branch name, etc.
    – molf
    May 27, 2009 at 22:12
  • this answer doesn't work for me, see @eahanson 's answer below.
    – Siwei
    Aug 8, 2013 at 1:47
  • 5
    For Capistrano 3, please see this question: stackoverflow.com/questions/21036175/…
    – KIR
    Jan 24, 2014 at 10:01
  • 1
    set :branch, 'SHA' works uniformly in Capistrano 2 & 3 Feb 28, 2014 at 11:35
17

molf's answer didn't work for me (using capistrano 2.11.2). I had to use "revision" instead of branch, like this:

cap -S revision=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy
3
  • Working 100% fine in 2.14.2 Sep 10, 2013 at 8:34
  • 3
    Well, 2.11.2 is above 2.9, and you use the syntax mentioned for above 2.9. Am I missing something ?
    – kik
    Feb 26, 2014 at 14:10
  • @kik molf changed the answer after my comment
    – eahanson
    Mar 29, 2020 at 15:40
15

Capistrano 3

In your deploy.rb or stage-specific file like config/deploy/production.rb

set :branch, ENV.fetch('REVISION', 'master')

This allows you to point to a specific git revision. It accepts a SHA but also anything that resolves to a real revision (e.g. git tag, annotated tag, or branch).

Use it on the command line by setting the REVISION environment variable, e.g.

bundle exec cap production deploy REVISION=80655da8d80aaaf92ce5357e7828dc09adb00993

bundle exec cap staging deploy REVISION=my-topic-branch
3
  • Is there a way to pass the branch as a command line argument? Dec 23, 2015 at 8:57
  • I don't know a way to do that. Dec 23, 2015 at 9:14
  • 1
    @marcovtwout yes that's possible, see the updated the answer.
    – Dennis
    May 6, 2016 at 14:37
1

ask :branch, 'master'

Prompts for input but defaults to 'master' if you press return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.