Skip to content

Commit

Permalink
Merge pull request #184 from jesusofsuburbia/master
Browse files Browse the repository at this point in the history
Allow multiple destinations
  • Loading branch information
priyawadhwa committed May 17, 2018
2 parents 9885f7f + 4c190a7 commit beb00f0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
14 changes: 7 additions & 7 deletions cmd/executor/cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ import (
"strings"
)

// The buildArg type is used to pass in multiple --build-arg flags
type buildArg []string
// This type is used to supported passing in multiple flags
type multiArg []string

// Now, for our new type, implement the two methods of
// the flag.Value interface...
// The first method is String() string
func (b *buildArg) String() string {
func (b *multiArg) String() string {
return strings.Join(*b, ",")
}

// The second method is Set(value string) error
func (b *buildArg) Set(value string) error {
logrus.Infof("appending to build args %s", value)
func (b *multiArg) Set(value string) error {
logrus.Infof("appending to multi args %s", value)
*b = append(*b, value)
return nil
}

func (b *buildArg) Type() string {
return "build-arg type"
func (b *multiArg) Type() string {
return "multi-arg type"
}
10 changes: 6 additions & 4 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ import (

var (
dockerfilePath string
destination string
destinations multiArg
srcContext string
snapshotMode string
bucket string
dockerInsecureSkipTLSVerify bool
logLevel string
force bool
buildArgs buildArg
buildArgs multiArg
tarPath string
)

func init() {
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.")
RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "/workspace/", "Path to the dockerfile build context.")
RootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.")
RootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "Registry the final image should be pushed to (ex: gcr.io/test/example:latest)")
RootCmd.PersistentFlags().VarP(&destinations, "destination", "d", "Registry the final image should be pushed to. Set it repeatedly for multiple destinations.")
RootCmd.MarkPersistentFlagRequired("destination")
RootCmd.PersistentFlags().StringVarP(&snapshotMode, "snapshotMode", "", "full", "Set this flag to change the file attributes inspected during snapshotting")
RootCmd.PersistentFlags().VarP(&buildArgs, "build-arg", "", "This flag allows you to pass in ARG values at build time. Set it repeatedly for multiple values.")
Expand Down Expand Up @@ -84,10 +84,12 @@ var RootCmd = &cobra.Command{
logrus.Error(err)
os.Exit(1)
}
if err := executor.DoPush(ref, image, destination, tarPath); err != nil {

if err := executor.DoPush(ref, image, destinations, tarPath); err != nil {
logrus.Error(err)
os.Exit(1)
}

},
}

Expand Down
43 changes: 26 additions & 17 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
pushAuth, err := authn.DefaultKeychain.Resolve(destRef.Context().Registry)
if err != nil {
return err
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
}
}
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
Expand Down

0 comments on commit beb00f0

Please sign in to comment.