Skip to content

Commit

Permalink
build multiple image tags (#3)
Browse files Browse the repository at this point in the history
* build multiple image tags

* update README
  • Loading branch information
MiguelSavignano committed Oct 16, 2020
1 parent 5cc754e commit 42e9040
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -36,11 +36,11 @@ cached-docker help

| Alias | Flag | Description | Default |
| ----- | -------------------- | --------------------------------------------------------------------- | -------------------- |
| -i | --image-name | Image name without tag | |
| -i | --image-name | Image name without tag or image names separated by comma | |
| -t | --image-tag | Image tag | Time in unix seconds |
| | --build-params | Add any docker build flag, --build-params="--build-arg=TOKEN=\$TOKEN" | |
| -f | --file | Name of the Dockerfile | 'PATH/Dockerfile' |
| | --cache-stage-target | Name of the stage target for use in cache. | |
| | --cache-stage-target | Name of the stage target for use in cache. default first stage | |
| | --print | Only print docker commands | false |
| -v | --version | Version | |
| -h | --help | Help for this command. | false |
Expand Down
6 changes: 3 additions & 3 deletions spec/cached_docker/app_spec.cr
Expand Up @@ -3,7 +3,7 @@ require "../spec_helper"
describe CachedDocker::App do
it "#commands" do
subject = CachedDocker::App.new(
image_name: "crystal-dev",
image_names: ["crystal-dev"],
image_tag: "v1",
)
subject.commands.should eq([
Expand All @@ -19,7 +19,7 @@ describe CachedDocker::App do

it "#initialize set default values" do
subject = CachedDocker::App.new(
image_name: "crystal-dev",
image_names: ["crystal-dev"],
image_tag: "",
build_params: "--build-arg=NPM_TOKEN=1234",
dockerfile_path: "Dockerfile"
Expand All @@ -31,7 +31,7 @@ describe CachedDocker::App do

it "#commands" do
subject = CachedDocker::App.new(
image_name: "crystal-dev",
image_names: ["crystal-dev"],
image_tag: "v1",
build_params: "--build-arg=NPM_TOKEN=1234",
dockerfile_path: "Dockerfile"
Expand Down
4 changes: 3 additions & 1 deletion spec/cached_docker/multi_stage_spec.cr
Expand Up @@ -2,9 +2,11 @@ require "../spec_helper"

class Dummy
include CachedDocker::CachedStages
@image_name : String

def initialize
@image_name = "gcr.io/docker-rails-258302/rails-sqlite"
@image_names = ["gcr.io/docker-rails-258302/rails-sqlite"]
@image_name = @image_names[0]
@dockerfile_path = "./Dockerfile"
@cache_stage_target = ""
end
Expand Down
4 changes: 3 additions & 1 deletion spec/cached_docker/template_command_spec.cr
Expand Up @@ -2,9 +2,11 @@ require "../spec_helper"

class Service
include CachedDocker::TemplateCommand
@image_name : String

def initialize
@image_name = "gcr.io/docker-rails-258302/rails-sqlite"
@image_names = ["gcr.io/docker-rails-258302/rails-sqlite"]
@image_name = @image_names[0]
@dockerfile_path = "./spec/fixtures/Dockerfile.dev"
@cache_stage_target = ""
@image_tag = "v1"
Expand Down
2 changes: 1 addition & 1 deletion src/cached_docker.cr
Expand Up @@ -74,7 +74,7 @@ cli = Commander::Command.new do |cmd|
puts cmd.help
else
app = CachedDocker::App.new(
image_name: options.string["image_name"],
image_names: options.string["image_name"].split(",").map &.strip,
image_tag: options.string["image_tag"],
push: options.bool["push"],
dockerfile_path: options.string["dockerfile_path"],
Expand Down
4 changes: 3 additions & 1 deletion src/cached_docker/app.cr
Expand Up @@ -6,13 +6,15 @@ class CachedDocker::App
include TemplateCommand
include CachedStages
@cache_stages : Array(Hash(String, String))
@image_name : String
@push = true
getter :image_tag, :dockerfile_path, :build_params

def initialize(*,
@image_name = "", @image_tag = "",
@image_names = [""], @image_tag = "",
@push = true, @build_params = "",
@cache_stage_target = "", @dockerfile_path = "./Dockerfile")
@image_name = @image_names[0]
if @dockerfile_path != "./Dockerfile"
@build_params = "#{@build_params} -f #{@dockerfile_path}"
end
Expand Down
11 changes: 7 additions & 4 deletions src/cached_docker/template_command.cr
Expand Up @@ -6,10 +6,7 @@ module CachedDocker::TemplateCommand
"docker pull #{@image_name}",
pull_cache_steps,
build_cache_steps,
"docker build #{@build_params} #{cache_froms} \
-t #{@image_name} \
-t #{@image_name}:#{@image_tag} \
.",
"docker build #{@build_params} #{cache_froms} #{image_tags} .",
@push ? push_commands : [] of String,
].flatten
end
Expand All @@ -22,6 +19,12 @@ module CachedDocker::TemplateCommand
]
end

def image_tags
@image_names.map do |image_name|
"-t #{image_name} -t #{image_name}:#{@image_tag}"
end.join(" ")
end

def cache_froms
stages = @cache_stages.map { |stage| "--cache-from=#{stage["name"]}" }.join(" ")
"#{stages} --cache-from=#{@image_name}"
Expand Down

0 comments on commit 42e9040

Please sign in to comment.