Tag images with service label so we can prune exclusively

This commit is contained in:
David Heinemeier Hansson
2023-02-15 13:41:03 +01:00
parent 90f4212a68
commit 8ec94f105c
4 changed files with 20 additions and 16 deletions

View File

@@ -6,10 +6,18 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
end end
def build_options def build_options
[ *build_tags, *build_args, *build_secrets ] [ *build_tags, *build_labels, *build_args, *build_secrets ]
end end
private private
def build_tags
[ "-t", config.absolute_image, "-t", config.latest_image ]
end
def build_labels
argumentize "--label", { service: config.service }
end
def build_args def build_args
argumentize "--build-arg", args, redacted: true argumentize "--build-arg", args, redacted: true
end end
@@ -18,10 +26,6 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
argumentize "--secret", secrets.collect { |secret| [ "id", secret ] } argumentize "--secret", secrets.collect { |secret| [ "id", secret ] }
end end
def build_tags
[ "-t", config.absolute_image, "-t", config.latest_image ]
end
def args def args
(config.builder && config.builder["args"]) || {} (config.builder && config.builder["args"]) || {}
end end

View File

@@ -6,7 +6,7 @@ class Mrsk::Commands::Prune < Mrsk::Commands::Base
PRUNE_CONTAINERS_AFTER = 3.days.in_hours.to_i PRUNE_CONTAINERS_AFTER = 3.days.in_hours.to_i
def images def images
docker :image, :prune, "--all", "--force", "--filter", "until=#{PRUNE_IMAGES_AFTER}h" docker :image, :prune, "--all", "--force", "--filter", "label=service=#{config.service}", "--filter", "until=#{PRUNE_IMAGES_AFTER}h"
end end
def containers def containers

View File

@@ -8,50 +8,50 @@ class CommandsBuilderTest < ActiveSupport::TestCase
test "target multiarch by default" do test "target multiarch by default" do
builder = new_builder_command builder = new_builder_command
assert_equal "multiarch", builder.name assert_equal "multiarch", builder.name
assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch", "-t", "dhh/app:123", "-t", "dhh/app:latest", "."], builder.push assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch", "-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "."], builder.push
end end
test "target native when multiarch is off" do test "target native when multiarch is off" do
builder = new_builder_command(builder: { "multiarch" => false }) builder = new_builder_command(builder: { "multiarch" => false })
assert_equal "native", builder.name assert_equal "native", builder.name
assert_equal [:docker, :build, "-t", "dhh/app:123", "-t", "dhh/app:latest", ".", "&&", :docker, :push, "dhh/app:123"], builder.push assert_equal [:docker, :build, "-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", ".", "&&", :docker, :push, "dhh/app:123"], builder.push
end end
test "target multiarch remote when local and remote is set" do test "target multiarch remote when local and remote is set" do
builder = new_builder_command(builder: { "local" => { }, "remote" => { } }) builder = new_builder_command(builder: { "local" => { }, "remote" => { } })
assert_equal "multiarch/remote", builder.name assert_equal "multiarch/remote", builder.name
assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch-remote", "-t", "dhh/app:123", "-t", "dhh/app:latest", "."], builder.push assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch-remote", "-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "."], builder.push
end end
test "target native remote when only remote is set" do test "target native remote when only remote is set" do
builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" } }) builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" } })
assert_equal "native/remote", builder.name assert_equal "native/remote", builder.name
assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64", "--builder", "mrsk-app-native-remote", "-t", "dhh/app:123", "-t", "dhh/app:latest", "."], builder.push assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64", "--builder", "mrsk-app-native-remote", "-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "."], builder.push
end end
test "build args" do test "build args" do
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } }) builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
assert_equal ["-t", "dhh/app:123", "-t", "dhh/app:latest", "--build-arg", "a=1", "--build-arg", "b=2"], builder.target.build_options assert_equal ["-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "--build-arg", "a=1", "--build-arg", "b=2"], builder.target.build_options
end end
test "build secrets" do test "build secrets" do
builder = new_builder_command(builder: { "secrets" => ["token_a", "token_b"] }) builder = new_builder_command(builder: { "secrets" => ["token_a", "token_b"] })
assert_equal ["-t", "dhh/app:123", "-t", "dhh/app:latest", "--secret", "id=token_a", "--secret", "id=token_b"], builder.target.build_options assert_equal ["-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "--secret", "id=token_a", "--secret", "id=token_b"], builder.target.build_options
end end
test "native push with build args" do test "native push with build args" do
builder = new_builder_command(builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } }) builder = new_builder_command(builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } })
assert_equal [ :docker, :build, "-t", "dhh/app:123", "-t", "dhh/app:latest", "--build-arg", "a=1", "--build-arg", "b=2", ".", "&&", :docker, :push, "dhh/app:123" ], builder.push assert_equal [ :docker, :build, "-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "--build-arg", "a=1", "--build-arg", "b=2", ".", "&&", :docker, :push, "dhh/app:123" ], builder.push
end end
test "multiarch push with build args" do test "multiarch push with build args" do
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } }) builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
assert_equal [ :docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch", "-t", "dhh/app:123", "-t", "dhh/app:latest", "--build-arg", "a=1", "--build-arg", "b=2", "." ], builder.push assert_equal [ :docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch", "-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "--build-arg", "a=1", "--build-arg", "b=2", "." ], builder.push
end end
test "native push with with build secrets" do test "native push with with build secrets" do
builder = new_builder_command(builder: { "multiarch" => false, "secrets" => [ "a", "b" ] }) builder = new_builder_command(builder: { "multiarch" => false, "secrets" => [ "a", "b" ] })
assert_equal [ :docker, :build, "-t", "dhh/app:123", "-t", "dhh/app:latest", "--secret", "id=a", "--secret", "id=b", ".", "&&", :docker, :push, "dhh/app:123" ], builder.push assert_equal [ :docker, :build, "-t", "dhh/app:123", "-t", "dhh/app:latest", "--label", "service=app", "--secret", "id=a", "--secret", "id=b", ".", "&&", :docker, :push, "dhh/app:123" ], builder.push
end end
private private

View File

@@ -10,7 +10,7 @@ class CommandsPruneTest < ActiveSupport::TestCase
test "images" do test "images" do
assert_equal \ assert_equal \
"docker image prune --all --force --filter until=168h", "docker image prune --all --force --filter label=service=app --filter until=168h",
new_command.images.join(" ") new_command.images.join(" ")
end end