Showing the git commit SHA that is currently deployed
I cannot count how many times I (or others) have asked the following question:
What is the difference between master and the production version?
We deploy about 3-5 times a day and we merge code about twice as much into master (some days, even more then that).
We deploy to servers with Capistrano, Capistrano has a very useful feature to write a REVISION file to the root of the project, this file contains a single line with the commit sha.
So, I thought I might set up a controller that will read this file and display it when I want.
So, I spec’d it out:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe :git_sha do | |
it "should read the file correctly" do | |
controller.stub!(:current_user).and_return(nil) | |
File.should_receive(:open).with("#{Rails.root}/REVISION").and_return(stub(read: "aaa")) | |
get :git_sha | |
response.body.should == "aaa" | |
end | |
end |
And this is the controller code:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def git_sha | |
revision = File.open("#{Rails.root}/REVISION").read.strip.to_s | |
render :text => revision | |
end |