Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit beb00f0

Browse files
author
priyawadhwa
authored
Merge pull request #184 from jesusofsuburbia/master
Allow multiple destinations
2 parents 9885f7f + 4c190a7 commit beb00f0

3 files changed

Lines changed: 39 additions & 28 deletions

File tree

cmd/executor/cmd/args.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ import (
2121
"strings"
2222
)
2323

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

2727
// Now, for our new type, implement the two methods of
2828
// the flag.Value interface...
2929
// The first method is String() string
30-
func (b *buildArg) String() string {
30+
func (b *multiArg) String() string {
3131
return strings.Join(*b, ",")
3232
}
3333

3434
// The second method is Set(value string) error
35-
func (b *buildArg) Set(value string) error {
36-
logrus.Infof("appending to build args %s", value)
35+
func (b *multiArg) Set(value string) error {
36+
logrus.Infof("appending to multi args %s", value)
3737
*b = append(*b, value)
3838
return nil
3939
}
4040

41-
func (b *buildArg) Type() string {
42-
return "build-arg type"
41+
func (b *multiArg) Type() string {
42+
return "multi-arg type"
4343
}

cmd/executor/cmd/root.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ import (
3131

3232
var (
3333
dockerfilePath string
34-
destination string
34+
destinations multiArg
3535
srcContext string
3636
snapshotMode string
3737
bucket string
3838
dockerInsecureSkipTLSVerify bool
3939
logLevel string
4040
force bool
41-
buildArgs buildArg
41+
buildArgs multiArg
4242
tarPath string
4343
)
4444

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

pkg/executor/executor.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,35 @@ func DoBuild(dockerfilePath, srcContext, snapshotMode string, args []string) (na
170170
return nil, nil, err
171171
}
172172

173-
func DoPush(ref name.Reference, image v1.Image, destination, tarPath string) error {
174-
// Push the image
175-
destRef, err := name.NewTag(destination, name.WeakValidation)
176-
if err != nil {
177-
return err
178-
}
173+
func DoPush(ref name.Reference, image v1.Image, destinations []string, tarPath string) error {
174+
// continue pushing unless an error occurs
175+
for _, destination := range destinations {
176+
// Push the image
177+
destRef, err := name.NewTag(destination, name.WeakValidation)
178+
if err != nil {
179+
return err
180+
}
179181

180-
if tarPath != "" {
181-
return tarball.WriteToFile(tarPath, destRef, image, nil)
182-
}
182+
if tarPath != "" {
183+
return tarball.WriteToFile(tarPath, destRef, image, nil)
184+
}
183185

184-
wo := remote.WriteOptions{}
185-
if ref != nil {
186-
wo.MountPaths = []name.Repository{ref.Context()}
187-
}
188-
pushAuth, err := authn.DefaultKeychain.Resolve(destRef.Context().Registry)
189-
if err != nil {
190-
return err
186+
wo := remote.WriteOptions{}
187+
if ref != nil {
188+
wo.MountPaths = []name.Repository{ref.Context()}
189+
}
190+
pushAuth, err := authn.DefaultKeychain.Resolve(destRef.Context().Registry)
191+
if err != nil {
192+
return err
193+
}
194+
195+
err = remote.Write(destRef, image, pushAuth, http.DefaultTransport, wo)
196+
if err != nil {
197+
logrus.Error(fmt.Errorf("Failed to push to destination %s", destination))
198+
return err
199+
}
191200
}
192-
return remote.Write(destRef, image, pushAuth, http.DefaultTransport, wo)
201+
return nil
193202
}
194203
func saveStageDependencies(index int, stages []instructions.Stage, buildArgs *dockerfile.BuildArgs) error {
195204
// First, get the files in this stage later stages will need

0 commit comments

Comments
 (0)