forked from shakacode/react-webpack-rails-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_commit_sha.rb
More file actions
27 lines (22 loc) · 847 Bytes
/
git_commit_sha.rb
File metadata and controls
27 lines (22 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true
# Retrieves the current git commit SHA of the project
class GitCommitSha
attr_writer :current_sha
def self.current_sha
@current_sha ||= ENV["GIT_COMMIT_SHA"].presence ||
retrieve_sha_from_file.presence ||
retrieve_sha_from_git
end
def self.reset_current_sha
@current_sha = nil
end
# Assumes the git CLI is available. This is not the case in production on Heroku.
def self.retrieve_sha_from_git
`git rev-parse HEAD 2>/dev/null`.to_s.strip
end
# Assumes a .source_version file with SHA inside. A special Heroku buildpack creates this for us in production.
def self.retrieve_sha_from_file
expected_filepath = Rails.root.join(".source_version")
File.exist?(expected_filepath) ? File.read(expected_filepath).to_s.strip : nil
end
end