Skip to content

Commit 44404f1

Browse files
committed
Use buildpack to set git commit SHA during Heroku build
Heroku does not provide access to Git via the command line, so the methods that work for the dev and test environments won't work here. Instead, we can utilize an additional buildpack in .buildpacks to write the SOURCE_VERSION environment variable to a file named .source_version in the application root. We cannot reference this environment variable directly from the app because Heroku only makes it available during the compilation phase of the build, hence the need to use a buildpack to write it to a file for later use by the app.
1 parent dfd6d71 commit 44404f1

File tree

5 files changed

+11
-47
lines changed

5 files changed

+11
-47
lines changed

Diff for: .buildpacks

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
https://github.com/heroku/heroku-buildpack-nodejs.git
22
https://github.com/heroku/heroku-buildpack-ruby.git
3+
https://github.com/sreid/heroku-buildpack-sourceversion.git

Diff for: app/helpers/pages_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ def git_commit_sha
55

66
def git_commit_sha_short
77
full_sha = git_commit_sha
8-
full_sha[-7..-1]
8+
full_sha[-8..-1]
99
end
1010
end

Diff for: app/models/git_commit_sha.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# Retrieves the current git commit SHA of the project
12
class GitCommitSha
23
def self.current_sha
3-
@commit_sha ||= retrieve_sha_from_env_var.presence || retrieve_sha_from_git
4+
@commit_sha ||= retrieve_sha_from_file.presence || retrieve_sha_from_git
45
end
56

67
def self.current_sha=(sha)
@@ -15,8 +16,8 @@ def self.retrieve_sha_from_git
1516
`git rev-parse HEAD 2>/dev/null`.to_s.strip
1617
end
1718

18-
def self.retrieve_sha_from_env_var
19-
env_var = ENV["DEPLOYMENT_SHA"]
20-
env_var.blank? ? nil : env_var
19+
def self.retrieve_sha_from_file
20+
expected_filepath = Rails.root.join(".source_version")
21+
File.exist?(expected_filepath) ? File.read(expected_filepath) : nil
2122
end
2223
end

Diff for: scripts/deploy

-39
This file was deleted.

Diff for: spec/features/pages_spec.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
end
1212

1313
feature "Git Commit SHA" do
14-
context "when env var is not set" do
14+
context "when .source_version file does not exist" do
1515
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5x" }
1616
let(:expected_text) { "5eead5x" }
1717
before { GitCommitSha.current_sha = sha }
1818
it_behaves_like "Git Commit SHA"
1919
end
20-
context "when env var is set" do
20+
context "when .source_version file exists" do
2121
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5y" }
2222
let(:expected_text) { "5eead5y" }
2323
before do
24-
ENV["DEPLOYMENT_SHA"] = sha
24+
`cd #{Rails.root} && echo #{sha} > .source_version`
2525
GitCommitSha.reset_current_sha
2626
end
27+
after { `cd #{Rails.root} && rm .source_version` }
2728
it_behaves_like "Git Commit SHA"
2829
end
2930
end

0 commit comments

Comments
 (0)