Skip to content

Commit

Permalink
add targets and stages
Browse files Browse the repository at this point in the history
  • Loading branch information
jwaldrip committed Aug 30, 2019
1 parent 664766a commit e42653c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
2 changes: 1 addition & 1 deletion shard.yml
@@ -1,5 +1,5 @@
name: psykube
version: 2.21.1
version: 2.22.0
crystal: 0.30.0

authors:
Expand Down
64 changes: 43 additions & 21 deletions src/cli/commands/concerns/docker.cr
Expand Up @@ -51,29 +51,43 @@ module Psykube::CLI::Commands::Docker
end

Dir.cd actor.working_directory do
args = ["build"]
build_args.each do |arg|
args << "--build-arg=#{arg}"
build_context.stages.each do |stage|
build_image(build_context, tag, stage)
end
build_context.args.each do |arg|
args << "--build-arg=#{arg}"
end
args << "--file=#{build_context.dockerfile}" if build_context.dockerfile
build_context.cache_from.each do |c|
args << "--cache-from=#{c}"
end
build_context.build_tags.each do |t|
args << "--tag=#{t}"
build_image(build_context, tag, build_context.target)
end
end

def build_image(build_context : BuildContext, tag : String? = nil, target : String? = nil)
iidfile = File.tempfile("iidfile")
args = ["build"]
build_args.each do |arg|
args << "--build-arg=#{arg}"
end
build_context.args.each do |arg|
args << "--build-arg=#{arg}"
end
args << "--file=#{build_context.dockerfile}" if build_context.dockerfile
build_context.cache_from.each do |c|
build_context.stages.each do |stage|
args << "--cache-from=#{c}-#{stage}" unless stage == target
end
args << "--quiet" if flags.quiet_build
docker_run args + [build_context.context]
io = IO::Memory.new
docker_run args + ["--quiet"] + [build_context.context], output: io
sha = io.rewind.gets_to_end.strip
build_context.image, build_context.tag = tag.split(':') if tag && tag.includes?(":")
build_context.tag ||= sha.sub(':', '-')
docker_run ["tag", sha, build_context.image(tag)]
c += "-#{target}" if target
args << "--cache-from=#{c}"
end
build_context.build_tags.each do |t|
t += "-#{target}" if target
args << "--tag=#{t}"
end
args << "--quiet" if flags.quiet_build
args << "--iidfile=#{iidfile.path}"
args << "--target=#{target}" if target
docker_run args + [build_context.context]
sha = File.read(iidfile.path).strip
iidfile.delete
build_context.image, build_context.tag = tag.split(':') if tag && tag.includes?(":")
build_context.tag ||= sha.sub(':', '-')
docker_run ["tag", sha, build_context.image(tag)]
end

def docker_push(build_contexts : Array(BuildContext), tag : String? = nil)
Expand All @@ -84,10 +98,18 @@ module Psykube::CLI::Commands::Docker
docker_login(build_context)

image = tag && tag.includes?(":") ? tag : build_context.image(tag)
build_context.stages.each do |stage|
push_tags(build_context, stage)
end
push_tags(build_context, build_context.target)
docker_run ["push", build_context.image(tag)]
end

def push_tags(build_context : BuildContext, target : String? = nil)
build_context.build_tags.each do |build_tag|
build_tag += "-#{target}" if target
docker_run ["push", build_tag]
end
docker_run ["push", build_context.image(tag)]
end

def docker_run(args : Array(String), *, input = Process::Redirect::Close, output = @output_io, allow_failure = false)
Expand Down
4 changes: 3 additions & 1 deletion src/psykube/build_context.cr
Expand Up @@ -12,8 +12,10 @@ class Psykube::BuildContext
getter dockerfile : String?
getter args : Array(String)
getter login : Login?
getter stages : Array(String)
getter target : String?

def initialize(*, @container_name : String, image : String, tag, @context, @dockerfile, @build, args, @login = nil, cache_from = nil, build_tags = nil)
def initialize(*, @container_name : String, image : String, tag, @context, @dockerfile, @build, args, @login = nil, cache_from = nil, build_tags = nil, @stages = [] of String, @target = nil)
parts = image.split(':')
@image = parts[0]
tag ||= parts[1]?
Expand Down
2 changes: 2 additions & 0 deletions src/psykube/v2/manifest.cr
Expand Up @@ -96,6 +96,8 @@ abstract class Psykube::V2::Manifest
login: get_login(image, image_pull_secrets),
cache_from: container.build.try(&.cache_from),
build_tags: container.build.try(&.tag),
stages: container.build.try(&.stages) || [] of String,
target: container.build.try(&.target)
)
end

Expand Down
2 changes: 2 additions & 0 deletions src/psykube/v2/manifest/shared/container/build.cr
Expand Up @@ -10,6 +10,8 @@ class Psykube::V2::Manifest::Shared::Container::Build
tag: {type: String | Array(String), optional: true},
context: {type: String, optional: true},
cache_from: {type: String | Array(String | CacheFromTag), optional: true},
stages: {type: Array(String), optional: true},
target: {type: String, optional: true},
args: {type: StringMap, default: StringMap.new},
})

Expand Down

0 comments on commit e42653c

Please sign in to comment.