From 02c1cf5b28e83a657b09d58f5c1d687690fb3877 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 6 May 2020 16:39:41 -0700 Subject: [PATCH 1/3] support pushing multiple tags --- builder/docker/artifact_import.go | 16 ++++++++++++- post-processor/docker-push/post-processor.go | 25 +++++++++++++++----- post-processor/docker-tag/post-processor.go | 4 ++++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/builder/docker/artifact_import.go b/builder/docker/artifact_import.go index acc34019d..1849f7621 100644 --- a/builder/docker/artifact_import.go +++ b/builder/docker/artifact_import.go @@ -2,6 +2,7 @@ package docker import ( "fmt" + "strings" ) // ImportArtifact is an Artifact implementation for when a container is @@ -29,7 +30,20 @@ func (a *ImportArtifact) Id() string { } func (a *ImportArtifact) String() string { - return fmt.Sprintf("Imported Docker image: %s", a.Id()) + tags := a.StateData["docker_tags"] + if tags == nil { + return fmt.Sprintf("Imported Docker image: %s", a.Id()) + } else { + cast := tags.([]interface{}) + names := []string{} + for _, name := range cast { + if n, ok := name.(string); ok { + names = append(names, n) + } + } + return fmt.Sprintf("Imported Docker image: %s with tags %s", + a.Id(), strings.Join(names, " ")) + } } func (a *ImportArtifact) State(name string) interface{} { diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index a276b0fca..3b492b8dd 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -5,6 +5,7 @@ package dockerpush import ( "context" "fmt" + "log" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/builder/docker" @@ -103,18 +104,30 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact }() } - // Get the name. - name := artifact.Id() + names := []string{artifact.Id()} + tags := artifact.State("docker_tags") + if tags != nil { + cast := tags.([]interface{}) + for _, name := range cast { + if n, ok := name.(string); ok { + names = append(names, n) + } + } + } - ui.Message("Pushing: " + name) - if err := driver.Push(name); err != nil { - return nil, false, false, err + // Get the name. + for _, name := range names { + ui.Message("Pushing: " + name) + if err := driver.Push(name); err != nil { + return nil, false, false, err + } } artifact = &docker.ImportArtifact{ BuilderIdValue: BuilderIdImport, Driver: driver, - IdValue: name, + IdValue: names[0], + StateData: map[string]interface{}{"docker_tags": tags}, } return artifact, true, false, nil diff --git a/post-processor/docker-tag/post-processor.go b/post-processor/docker-tag/post-processor.go index b4af223e6..4650b184e 100644 --- a/post-processor/docker-tag/post-processor.go +++ b/post-processor/docker-tag/post-processor.go @@ -5,6 +5,7 @@ package dockertag import ( "context" "fmt" + "log" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/builder/docker" @@ -68,6 +69,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact importRepo := p.config.Repository var lastTaggedRepo = importRepo + RepoTags := []string{} if len(p.config.Tag) > 0 { for _, tag := range p.config.Tag { local := importRepo + ":" + tag @@ -79,6 +81,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact return nil, false, true, err } + RepoTags = append(RepoTags, local) lastTaggedRepo = local } } else { @@ -95,6 +98,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact BuilderIdValue: BuilderId, Driver: driver, IdValue: lastTaggedRepo, + StateData: map[string]interface{}{"docker_tags": RepoTags}, } // If we tag an image and then delete it, there was no point in creating the From 841c23d5f90bea07b719fdcd0c1ede61b2f4bf2c Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 6 May 2020 17:14:27 -0700 Subject: [PATCH 2/3] fix tests --- post-processor/docker-push/post-processor.go | 1 - post-processor/docker-tag/post-processor.go | 1 - 2 files changed, 2 deletions(-) diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index 3b492b8dd..979ea2896 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -5,7 +5,6 @@ package dockerpush import ( "context" "fmt" - "log" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/builder/docker" diff --git a/post-processor/docker-tag/post-processor.go b/post-processor/docker-tag/post-processor.go index 4650b184e..b4b27350a 100644 --- a/post-processor/docker-tag/post-processor.go +++ b/post-processor/docker-tag/post-processor.go @@ -5,7 +5,6 @@ package dockertag import ( "context" "fmt" - "log" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/builder/docker" From 62556c464c68838949ea60f601a06b7ee15192d8 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 7 May 2020 11:39:09 +0200 Subject: [PATCH 3/3] Update builder/docker/artifact_import.go --- builder/docker/artifact_import.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/builder/docker/artifact_import.go b/builder/docker/artifact_import.go index 1849f7621..c626a9df3 100644 --- a/builder/docker/artifact_import.go +++ b/builder/docker/artifact_import.go @@ -33,17 +33,16 @@ func (a *ImportArtifact) String() string { tags := a.StateData["docker_tags"] if tags == nil { return fmt.Sprintf("Imported Docker image: %s", a.Id()) - } else { - cast := tags.([]interface{}) - names := []string{} - for _, name := range cast { - if n, ok := name.(string); ok { - names = append(names, n) - } + } + cast := tags.([]interface{}) + names := []string{} + for _, name := range cast { + if n, ok := name.(string); ok { + names = append(names, n) } - return fmt.Sprintf("Imported Docker image: %s with tags %s", - a.Id(), strings.Join(names, " ")) } + return fmt.Sprintf("Imported Docker image: %s with tags %s", + a.Id(), strings.Join(names, " ")) } func (a *ImportArtifact) State(name string) interface{} {