From 4c190a70371e0c82f3a320757e151b57bc1bedaf Mon Sep 17 00:00:00 2001 From: jesusofsuburb1a Date: Thu, 17 May 2018 09:35:41 +0200 Subject: [PATCH] allow multiple destinations, error handling for each push --- cmd/executor/cmd/root.go | 7 +++---- pkg/executor/executor.go | 43 ++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index f1b7f7e1d..43dbc547c 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -85,10 +85,9 @@ var RootCmd = &cobra.Command{ os.Exit(1) } - for _, destination := range destinations { - if err := executor.DoPush(ref, image, destination, tarPath); err != nil { - logrus.Error(err) - } + if err := executor.DoPush(ref, image, destinations, tarPath); err != nil { + logrus.Error(err) + os.Exit(1) } }, diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index b3c697859..7300ec1d0 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -170,26 +170,35 @@ func DoBuild(dockerfilePath, srcContext, snapshotMode string, args []string) (na return nil, nil, err } -func DoPush(ref name.Reference, image v1.Image, destination, tarPath string) error { - // Push the image - destRef, err := name.NewTag(destination, name.WeakValidation) - if err != nil { - return err - } +func DoPush(ref name.Reference, image v1.Image, destinations []string, tarPath string) error { + // continue pushing unless an error occurs + for _, destination := range destinations { + // Push the image + destRef, err := name.NewTag(destination, name.WeakValidation) + if err != nil { + return err + } - if tarPath != "" { - return tarball.WriteToFile(tarPath, destRef, image, nil) - } + if tarPath != "" { + return tarball.WriteToFile(tarPath, destRef, image, nil) + } - wo := remote.WriteOptions{} - if ref != nil { - wo.MountPaths = []name.Repository{ref.Context()} + wo := remote.WriteOptions{} + if ref != nil { + wo.MountPaths = []name.Repository{ref.Context()} + } + pushAuth, err := authn.DefaultKeychain.Resolve(destRef.Context().Registry) + if err != nil { + return err + } + + err = remote.Write(destRef, image, pushAuth, http.DefaultTransport, wo) + if err != nil { + logrus.Error(fmt.Errorf("Failed to push to destination %s", destination)) + return err + } } - pushAuth, err := authn.DefaultKeychain.Resolve(destRef.Context().Registry) - if err != nil { - return err - } - return remote.Write(destRef, image, pushAuth, http.DefaultTransport, wo) + return nil } func saveStageDependencies(index int, stages []instructions.Stage, buildArgs *dockerfile.BuildArgs) error { // First, get the files in this stage later stages will need