Capistrano 3 task for prompting for git tag or branch

1,856 views
Skip to first unread message

Dave Hamilton

unread,
Jan 17, 2014, 11:47:12 AM1/17/14
to capis...@googlegroups.com
I wrote this task to prompt for a git branch or tag to deploy. I'm pretty new to RoR and Capistrano so I'd welcome any critique. And, maybe this will be useful for others.

I have this at the end of my Capfile:


Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }


I put this in file lib/capistrano/tasks/git_tasks.rake


desc
"Prompt for branch or tag"
task git_branch_or_tag
: :'git:wrapper' do
  on roles
(:all) do |host|

    tags
= "<none>"
    within repo_path
do
     
with fetch(:git_environmental_variables) do
        tags
= capture(:git, :tag).split.join(', ')
     
end
   
end

    run_locally
do
      tag_prompt
= "Enter a branch or tag name to deploy, available tags include #{tags}"

      ask
(:branch_or_tag, tag_prompt)
      tag_branch_target
= fetch(:branch_or_tag)

      execute
"echo \"About to deploy branch or tag '#{tag_branch_target}'\""
     
set(:branch, tag_branch_target)
   
end

 
end
end

before
'deploy:starting', :git_branch_or_tag



Dave

Lee Hambley

unread,
Jan 20, 2014, 2:49:10 PM1/20/14
to capistrano

Looks good Dave!

Sent from my Nexus 4.

--
You received this message because you are subscribed to the Google Groups "Capistrano" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capistrano+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/capistrano/7b0c649f-5e89-41c0-87f2-edf9559b612f%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Andreas Wenk

unread,
Dec 11, 2014, 1:08:18 PM12/11/14
to capis...@googlegroups.com
I tried this in Capistrano 3.3.3 but after I added the tag number, it stopped and did not run further. 

def tag_branch_target
  tags
= `git tag`.split("\n")
  tag_prompt
= "\nlast tags available are #{tags.reverse[(0..5)]}"
  ask
:branch_or_tag, tag_prompt
  tag
= fetch(:branch_or_tag)
  tag
.match(/^\d/).nil? ? tags.last : tag
end


set :branch, proc { tag_branch_target }

It looks sth like:

NFO[edfd8380] Running /usr/bin/env chmod +x /tmp/ace/git-ssh.sh on 10.10.82.31
DEBUG
[edfd8380] Command: /usr/bin/env chmod +x /tmp/ace/git-ssh.sh
INFO
[6d1eea4e] Finished in 0.168 seconds with exit status 0 (successful).
INFO
[edfd8380] Finished in 0.164 seconds with exit status 0 (successful).
Please enter branch_or_tag (
last tags available are ["0.9.1", "0.9.0", "0.8.0", "0.7.9", "0.7.8", "0.7.7"]):

if no tag is given, that last one is used. I really like the idea to put that into a capistrano rake task so if I find time I will work on it.
Reply all
Reply to author
Forward
0 new messages