Skip to content

Commit

Permalink
only push image with flag
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelSavignano committed Feb 14, 2020
1 parent b624b60 commit 587ad43
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion spec/cached_docker/app_spec.cr
Expand Up @@ -5,7 +5,7 @@ describe CachedDocker::App do
subject = CachedDocker::App.new(
"crystal-dev",
"v1",
""
"",
)
subject.commands.should eq([
"docker pull crystal-dev",
Expand Down
9 changes: 9 additions & 0 deletions src/cached_docker.cr
Expand Up @@ -35,6 +35,14 @@ cli = Commander::Command.new do |cmd|
flag.description = "Name of the stage target for use in cache"
end

cmd.flags.add do |flag|
flag.name = "push"
flag.long = "--push"
flag.short = "-p"
flag.default = false
flag.description = "push image"
end

cmd.flags.add do |flag|
flag.name = "dockerfile_path"
flag.long = "--file"
Expand Down Expand Up @@ -71,6 +79,7 @@ cli = Commander::Command.new do |cmd|
options.string["build_params"],
options.string["cache_stage_target"],
options.string["dockerfile_path"],
options.bool["push"]
)

if options.bool["print"]
Expand Down
5 changes: 3 additions & 2 deletions src/cached_docker/app.cr
Expand Up @@ -6,9 +6,10 @@ class CachedDocker::App
include TemplateCommand
include CachedStages
@cache_stages : Array(Hash(String, String))
@push = true
getter :image_tag, :dockerfile_path, :build_params

def initialize(@image_name = "", @image_tag = "", @build_params = "", @cache_stage_target = "", @dockerfile_path = "")
def initialize(@image_name = "", @image_tag = "", @build_params = "", @cache_stage_target = "", @dockerfile_path = "", @push = true)
if @dockerfile_path == ""
@dockerfile_path = "./Dockerfile"
else
Expand All @@ -23,7 +24,7 @@ class CachedDocker::App
commands.each do |command|
puts command
end
DockerRunner.new(commands).run
DockerRunner.new(commands, @push).run
end

def multistage?
Expand Down
2 changes: 1 addition & 1 deletion src/cached_docker/docker_runner.cr
@@ -1,5 +1,5 @@
class CachedDocker::DockerRunner
def initialize(@commands : Array(String))
def initialize(@commands : Array(String), @push = true)
end

def run
Expand Down
16 changes: 12 additions & 4 deletions src/cached_docker/template_command.cr
@@ -1,17 +1,25 @@
module CachedDocker::TemplateCommand
@push = true

def commands
[
"docker pull #{@image_name}",
pull_cache_steps,
build_cache_steps,
"docker build #{@build_params} #{cache_froms} \
-t #{@image_name} \
-t #{@image_name}:#{@image_tag} \
.",
-t #{@image_name} \
-t #{@image_name}:#{@image_tag} \
.",
@push ? push_commands : [] of String,
].flatten
end

def push_commands
[
push_cache_steps,
"docker push #{@image_name}:#{@image_tag}",
"docker push #{@image_name}",
].flatten
]
end

def cache_froms
Expand Down
2 changes: 1 addition & 1 deletion src/cached_docker/version.cr
@@ -1,3 +1,3 @@
module CachedDocker
VERSION = "v0.1.0"
VERSION = "v1.0.0"
end

0 comments on commit 587ad43

Please sign in to comment.