From b0a6df3d42eeda4b792794e73626d9045e9bf13d Mon Sep 17 00:00:00 2001 From: ihab4real Date: Tue, 12 Nov 2024 17:10:31 +0200 Subject: [PATCH 1/3] define GIT_COMMIT_SHA in Dockerfile --- .controlplane/Dockerfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.controlplane/Dockerfile b/.controlplane/Dockerfile index e54d715e..f3033029 100644 --- a/.controlplane/Dockerfile +++ b/.controlplane/Dockerfile @@ -76,3 +76,7 @@ ENTRYPOINT ["./.controlplane/entrypoint.sh"] # Default args to pass to the entry point that can be overridden # For Kubernetes and ControlPlane, these are the "workload args" CMD ["./bin/rails", "server"] + +# Current commit hash environment variable +ARG GIT_COMMIT_SHA +ENV GIT_COMMIT_SHA=${GIT_COMMIT_SHA} From 2d1870405a640a2cce4cf504fa1bc4f691966172 Mon Sep 17 00:00:00 2001 From: ihab4real Date: Tue, 12 Nov 2024 17:12:02 +0200 Subject: [PATCH 2/3] check for GIT_COMMIT_SHA environment variable presence in GitDCommitSha.current_sha --- app/models/git_commit_sha.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/git_commit_sha.rb b/app/models/git_commit_sha.rb index 598ab20d..3c58498f 100644 --- a/app/models/git_commit_sha.rb +++ b/app/models/git_commit_sha.rb @@ -5,7 +5,9 @@ class GitCommitSha attr_writer :current_sha def self.current_sha - @current_sha ||= retrieve_sha_from_file.presence || retrieve_sha_from_git + @current_sha ||= ENV["GIT_COMMIT_SHA"].presence || + retrieve_sha_from_file.presence || + retrieve_sha_from_git end def self.reset_current_sha From 309d712429f62c6ac28792ea051cfb9328b184e2 Mon Sep 17 00:00:00 2001 From: ihab4real Date: Tue, 12 Nov 2024 17:17:00 +0200 Subject: [PATCH 3/3] add a new context to test if GIT_COMMIT_SHA exists --- spec/system/pages_spec.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/spec/system/pages_spec.rb b/spec/system/pages_spec.rb index a34147d1..61cdd2cb 100644 --- a/spec/system/pages_spec.rb +++ b/spec/system/pages_spec.rb @@ -17,13 +17,16 @@ GitCommitSha.reset_current_sha end - context "when .source_version file does not exist" do - let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5x" } + context "when GIT_COMMIT_SHA env var exists" do + let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5z" } let(:expected_text) { "94d9235" } before do - # stub this method since we need to control what the sha actually is - allow(GitCommitSha).to receive(:retrieve_sha_from_git) { sha } + ENV["GIT_COMMIT_SHA"] = sha + end + + after do + ENV.delete("GIT_COMMIT_SHA") end it_behaves_like "Git Commit SHA" @@ -39,4 +42,16 @@ it_behaves_like "Git Commit SHA" end + + context "when falling back to git command" do + let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5x" } + let(:expected_text) { "94d9235" } + + before do + # stub this method since we need to control what the sha actually is + allow(GitCommitSha).to receive(:retrieve_sha_from_git) { sha } + end + + it_behaves_like "Git Commit SHA" + end end