Skip to content

Instantly share code, notes, and snippets.

@castlese
Last active August 29, 2015 13:57
Show Gist options
  • Save castlese/9780118 to your computer and use it in GitHub Desktop.
Save castlese/9780118 to your computer and use it in GitHub Desktop.
Display current github branch name in your rails app when working with Capistrano
<!-- app/views/layout/application.html.erb -->
<% if !Rails.env.production? %>
<%= branch_info %>
<% end %>
# app/helpers/application_helper.rb
module ApplicationHelper
def branch_info
if Rails.env.staging?
#this will be changed after code is deployed to staging server
branch_name = 'BranchName'
else
#this works in local
branch_name = `git rev-parse --abbrev-ref HEAD`
end
content_tag :span, branch_name, :class => "label label-warning"
end
end
#config/deploy.rb
#set the branch name variable
set :branch_name, `git rev-parse --abbrev-ref HEAD`
#after the new version of app code has been uploaded to the server, edit the view file to update "BranchName" with the current branch variable as set above
#server restart is required due to caching
after "deploy:update_code" do
run "cd #{current_release}; sed -i 's/BranchName/#{branch_name}/g' app/helpers/application_helper.rb"
run "cd #{current_release}; touch tmp/restart.txt"
end
#config/deploy/staging.rb
#this file isn't necessary for the github labelling but it's useful all the same
#set capistrano to deploy from your current branch
set :branch_name, `git rev-parse --abbrev-ref HEAD`
set :branch, "#{branch_name}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment