Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten git version to generate correct container DNS hostname #412

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/mrsk/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,14 @@ def role_names
raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort
end

# git version will be part of container hostname, DNS has a limit of 63 characters
# so, make this a bit shorter, see: https://en.wikipedia.org/wiki/Subdomain#Overview
def git_version
@git_version ||=
if system("git rev-parse")
uncommitted_suffix = Mrsk::Utils.uncommitted_changes.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : ""
uncommitted_suffix = Mrsk::Utils.uncommitted_changes.present? ? "_#{SecureRandom.hex(2)}" : ""

"#{`git rev-parse HEAD`.strip}#{uncommitted_suffix}"
"#{`git rev-parse --short HEAD`.strip}#{uncommitted_suffix}"
else
raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}"
end
Expand Down
6 changes: 3 additions & 3 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ class ConfigurationTest < ActiveSupport::TestCase
test "version from git committed" do
ENV.delete("VERSION")

@config.expects(:`).with("git rev-parse HEAD").returns("git-version")
@config.expects(:`).with("git rev-parse --short HEAD").returns("git-version")
Mrsk::Utils.expects(:uncommitted_changes).returns("")
assert_equal "git-version", @config.version
end

test "version from git uncommitted" do
ENV.delete("VERSION")

@config.expects(:`).with("git rev-parse HEAD").returns("git-version")
@config.expects(:`).with("git rev-parse --short HEAD").returns("git-version")
Mrsk::Utils.expects(:uncommitted_changes).returns("M file\n")
assert_match /^git-version_uncommitted_[0-9a-f]{16}$/, @config.version
assert_match /^git-version_[0-9a-f]{4}$/, @config.version
end

test "version from env" do
Expand Down
Loading