Highlight uncommitted changes in version
If there are uncommitted changes in the app repository when building, then append `_uncommitted_<random>` to it to distinguish the image from one built from a clean checkout. Also change the version used when renaming a container on redeploy to distinguish and explain the version suffixes.
This commit is contained in:
@@ -21,7 +21,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
|||||||
execute *auditor.record("Booted app version #{version}"), verbosity: :debug
|
execute *auditor.record("Booted app version #{version}"), verbosity: :debug
|
||||||
|
|
||||||
if capture_with_info(*app.container_id_for_version(version), raise_on_non_zero_exit: false).present?
|
if capture_with_info(*app.container_id_for_version(version), raise_on_non_zero_exit: false).present?
|
||||||
tmp_version = "#{version}_#{SecureRandom.hex(8)}"
|
tmp_version = "#{version}_replaced_#{SecureRandom.hex(8)}"
|
||||||
info "Renaming container #{version} to #{tmp_version} as already deployed on #{host}"
|
info "Renaming container #{version} to #{tmp_version} as already deployed on #{host}"
|
||||||
execute *auditor.record("Renaming container #{version} to #{tmp_version}"), verbosity: :debug
|
execute *auditor.record("Renaming container #{version} to #{tmp_version}"), verbosity: :debug
|
||||||
execute *app.rename_container(version: version, new_version: tmp_version)
|
execute *app.rename_container(version: version, new_version: tmp_version)
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class Mrsk::Configuration
|
|||||||
end
|
end
|
||||||
|
|
||||||
def version
|
def version
|
||||||
@declared_version.presence || ENV["VERSION"] || current_commit_hash
|
@declared_version.presence || ENV["VERSION"] || git_version
|
||||||
end
|
end
|
||||||
|
|
||||||
def abbreviated_version
|
def abbreviated_version
|
||||||
@@ -233,10 +233,12 @@ class Mrsk::Configuration
|
|||||||
raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort
|
raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_commit_hash
|
def git_version
|
||||||
@current_commit_hash ||=
|
@git_version ||=
|
||||||
if system("git rev-parse")
|
if system("git rev-parse")
|
||||||
`git rev-parse HEAD`.strip
|
uncommitted_suffix = `git status --porcelain`.strip.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : ""
|
||||||
|
|
||||||
|
"#{`git rev-parse HEAD`.strip}#{uncommitted_suffix}"
|
||||||
else
|
else
|
||||||
raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}"
|
raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class CliAppTest < CliTestCase
|
|||||||
|
|
||||||
run_command("boot").tap do |output|
|
run_command("boot").tap do |output|
|
||||||
assert_match /Renaming container .* to .* as already deployed on 1.1.1.1/, output # Rename
|
assert_match /Renaming container .* to .* as already deployed on 1.1.1.1/, output # Rename
|
||||||
assert_match /docker rename .* .*/, output
|
assert_match /docker rename app-web-latest app-web-latest_replaced_[0-9a-f]{16}/, output
|
||||||
assert_match "docker run --detach --restart unless-stopped", output
|
assert_match "docker run --detach --restart unless-stopped", output
|
||||||
assert_match "docker container ls --all --filter name=^app-web-123$ --quiet | xargs docker stop", output
|
assert_match "docker container ls --all --filter name=^app-web-123$ --quiet | xargs docker stop", output
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -72,19 +72,36 @@ class ConfigurationTest < ActiveSupport::TestCase
|
|||||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], config.traefik_hosts
|
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], config.traefik_hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
test "version" do
|
test "version no git repo" do
|
||||||
ENV.delete("VERSION")
|
ENV.delete("VERSION")
|
||||||
|
|
||||||
@config.expects(:system).with("git rev-parse").returns(nil)
|
@config.expects(:system).with("git rev-parse").returns(nil)
|
||||||
error = assert_raises(RuntimeError) { @config.version}
|
error = assert_raises(RuntimeError) { @config.version}
|
||||||
assert_match /no git repository found/, error.message
|
assert_match /no git repository found/, error.message
|
||||||
|
end
|
||||||
|
|
||||||
@config.expects(:current_commit_hash).returns("git-version")
|
test "version from git committed" do
|
||||||
|
ENV.delete("VERSION")
|
||||||
|
|
||||||
|
@config.expects(:`).with("git rev-parse HEAD").returns("git-version")
|
||||||
|
@config.expects(:`).with("git status --porcelain").returns("")
|
||||||
assert_equal "git-version", @config.version
|
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 status --porcelain").returns("M file\n")
|
||||||
|
assert_match /^git-version_uncommitted_[0-9a-f]{16}$/, @config.version
|
||||||
|
end
|
||||||
|
|
||||||
|
test "version from env" do
|
||||||
ENV["VERSION"] = "env-version"
|
ENV["VERSION"] = "env-version"
|
||||||
assert_equal "env-version", @config.version
|
assert_equal "env-version", @config.version
|
||||||
|
end
|
||||||
|
|
||||||
|
test "version from arg" do
|
||||||
@config.version = "arg-version"
|
@config.version = "arg-version"
|
||||||
assert_equal "arg-version", @config.version
|
assert_equal "arg-version", @config.version
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ class DeployTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def latest_app_version
|
def latest_app_version
|
||||||
deployer_exec("cat version", capture: true)
|
deployer_exec("git rev-parse HEAD", capture: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_app_version(version)
|
def assert_app_version(version)
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ RUN mkdir -p /etc/docker/certs.d/registry:4443 && ln -s /shared/certs/domain.crt
|
|||||||
RUN git config --global user.email "deployer@example.com"
|
RUN git config --global user.email "deployer@example.com"
|
||||||
RUN git config --global user.name "Deployer"
|
RUN git config --global user.name "Deployer"
|
||||||
RUN git init && git add . && git commit -am "Initial version"
|
RUN git init && git add . && git commit -am "Initial version"
|
||||||
RUN git rev-parse HEAD > version
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=1s CMD pgrep sleep
|
HEALTHCHECK --interval=1s CMD pgrep sleep
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
FROM nginx:1-alpine-slim
|
FROM nginx:1-alpine-slim
|
||||||
|
|
||||||
COPY default.conf /etc/nginx/conf.d/default.conf
|
COPY default.conf /etc/nginx/conf.d/default.conf
|
||||||
COPY version /usr/share/nginx/html/version
|
|
||||||
|
ARG COMMIT_SHA
|
||||||
|
RUN echo $COMMIT_SHA > /usr/share/nginx/html/version
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ registry:
|
|||||||
password: root
|
password: root
|
||||||
builder:
|
builder:
|
||||||
multiarch: false
|
multiarch: false
|
||||||
|
args:
|
||||||
|
COMMIT_SHA: <%= `git rev-parse HEAD` %>
|
||||||
healthcheck:
|
healthcheck:
|
||||||
cmd: wget -qO- http://localhost > /dev/null
|
cmd: wget -qO- http://localhost > /dev/null
|
||||||
traefik:
|
traefik:
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
git commit -am 'Update rev' --amend
|
git commit -am 'Update rev' --amend
|
||||||
git rev-parse HEAD > version
|
|
||||||
|
|||||||
Reference in New Issue
Block a user