Skip to content

Commit 3257663

Browse files
committed
Add current Git commit hash to index view
1 parent 94d9235 commit 3257663

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

app/controllers/pages_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class PagesController < ApplicationController
22
def index
33
@comments = Comment.all
4+
@git_commit_sha = GitCommitSha.current_sha
45

56
# NOTE: The below notes apply if you want to set the value of the props in the controller, as
67
# compared to he view. However, it's more convenient to use Jbuilder from the view. See

app/models/git_commit_sha.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class GitCommitSha
2+
def self.current_sha
3+
return @commit_sha unless @commit_sha.blank?
4+
@commit_sha = (retrieve_sha_from_env_var || retrieve_sha_from_git_folder)
5+
end
6+
7+
def self.current_sha=(sha)
8+
@commit_sha = sha
9+
end
10+
11+
def self.reset_current_sha
12+
self.current_sha = ""
13+
end
14+
15+
def self.retrieve_sha_from_git_folder
16+
`git rev-parse HEAD 2>/dev/null`.to_s.strip
17+
end
18+
19+
def self.retrieve_sha_from_env_var
20+
env_var = ENV["DEPLOYMENT_SHA"]
21+
env_var.blank? ? false : env_var
22+
end
23+
end

app/views/pages/index.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<h2>Using React + Redux + Rails Backend (using the react_on_rails gem)</h2>
2+
<p>current Git commit:
3+
<span id="current-git-commit-sha">
4+
<%= @git_commit_sha %>
5+
</span>
6+
</p>
27
<ul>
38
<li>
49
If this work interests you and you're a developer or designer looking for full or part-time remote work: please

scripts/deploy

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
# Pass in app as either staging or production first -- must match what you seen in git remote -v
3+
# Examples:
4+
# ./deploy staging
5+
# ./deploy staging --force
6+
# ./deploy production
7+
8+
app=$1
9+
10+
shift
11+
12+
sha=`git rev-parse HEAD 2>/dev/null`
13+
current_branch=`git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///'`
14+
15+
# echo "current_branch is $current_branch"
16+
# echo "sha is $sha"
17+
# echo "app is $app"
18+
19+
if [ "$app" == "" ]; then
20+
echo "You must specify a param value for the app, such as production or staging"
21+
exit 1
22+
fi
23+
24+
if [[ "$app" == "production" && "$current_branch" != "master" ]]; then
25+
echo "You can only deploy master to production!"
26+
exit 1
27+
fi
28+
29+
echo Running:
30+
echo git push $app $current_branch:master $@
31+
git push $app $current_branch:master $@
32+
33+
echo Setting heroku config DEPLOYMENT_SHA
34+
echo heroku config:set DEPLOYMENT_SHA=$sha --remote $app
35+
heroku config:set DEPLOYMENT_SHA=$sha --remote $app
36+
37+
# Run migrations always just in case
38+
heroku run rake db:migrate --remote $app
39+
heroku restart --remote $app

spec/features/pages_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "rails_helper"
2+
3+
shared_examples "Git Commit SHA" do
4+
# Keep existing env variable intact
5+
let!(:old_sha) { ENV["DEPLOYMENT_SHA"] }
6+
after { ENV["DEPLOYMENT_SHA"] = old_sha }
7+
background { visit root_path }
8+
it "displays the current git commit" do
9+
expect(page).to have_css("#current-git-commit-sha", text: sha)
10+
end
11+
end
12+
13+
feature "Git Commit SHA" do
14+
context "when env var is not set" do
15+
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5x" }
16+
before { GitCommitSha.current_sha = sha }
17+
it_behaves_like "Git Commit SHA"
18+
end
19+
context "when env var is set" do
20+
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5y" }
21+
before do
22+
ENV["DEPLOYMENT_SHA"] = sha
23+
GitCommitSha.reset_current_sha
24+
end
25+
it_behaves_like "Git Commit SHA"
26+
end
27+
end

0 commit comments

Comments
 (0)