support pushing multiple tags

This commit is contained in:
Megan Marsh 2020-05-06 16:39:41 -07:00
parent a88b5dfe7b
commit 02c1cf5b28
3 changed files with 38 additions and 7 deletions

View File

@ -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{} {

View File

@ -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

View File

@ -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