@@ -72,36 +72,46 @@ func pushCmd(dockerCli command.Cli) *cobra.Command {
7272
7373func runPush (dockerCli command.Cli , name string , opts pushOptions ) error {
7474 defer muteDockerCli (dockerCli )()
75-
76- bundleStore , err := prepareBundleStore ( )
75+ // Get the bundle
76+ bndl , ref , err := resolveReferenceAndBundle ( dockerCli , name )
7777 if err != nil {
7878 return err
7979 }
80-
81- bndl , ref , err := resolveBundle ( dockerCli , bundleStore , name , false , nil )
80+ // Retag invocation image if needed
81+ retag , err := shouldRetagInvocationImage ( metadata . FromBundle ( bndl ), bndl , opts . tag , ref )
8282 if err != nil {
8383 return err
8484 }
85- // Use the bundle reference as a tag
86- if ref != "" && opts .tag == "" {
87- opts .tag = ref
85+ if retag .shouldRetag {
86+ if err := retagInvocationImage (dockerCli , bndl , retag .invocationImageRef .String ()); err != nil {
87+ return err
88+ }
8889 }
89- if err := bndl .Validate (); err != nil {
90+ // Push the invocation image
91+ if err := pushInvocationImage (dockerCli , retag ); err != nil {
9092 return err
9193 }
94+ // Push the bundle
95+ return pushBundle (dockerCli , opts , bndl , retag )
96+ }
9297
93- retag , err := shouldRetagInvocationImage (metadata .FromBundle (bndl ), bndl , opts .tag )
98+ func resolveReferenceAndBundle (dockerCli command.Cli , name string ) (* bundle.Bundle , string , error ) {
99+ bundleStore , err := prepareBundleStore ()
94100 if err != nil {
95- return err
101+ return nil , "" , err
96102 }
97- if retag .shouldRetag {
98- err := retagInvocationImage (dockerCli , bndl , retag .invocationImageRef .String ())
99- if err != nil {
100- return err
101- }
103+
104+ bndl , ref , err := resolveBundle (dockerCli , bundleStore , name , false , nil )
105+ if err != nil {
106+ return nil , "" , err
107+ }
108+ if err := bndl .Validate (); err != nil {
109+ return nil , "" , err
102110 }
111+ return bndl , ref , err
112+ }
103113
104- // pushing invocation image
114+ func pushInvocationImage ( dockerCli command. Cli , retag retagResult ) error {
105115 repoInfo , err := registry .ParseRepositoryInfo (retag .invocationImageRef )
106116 if err != nil {
107117 return err
@@ -117,10 +127,13 @@ func runPush(dockerCli command.Cli, name string, opts pushOptions) error {
117127 return errors .Wrapf (err , "starting push of %q" , retag .invocationImageRef .String ())
118128 }
119129 defer reader .Close ()
120- if err = jsonmessage .DisplayJSONMessagesStream (reader , ioutil .Discard , 0 , false , nil ); err != nil {
130+ if err : = jsonmessage .DisplayJSONMessagesStream (reader , ioutil .Discard , 0 , false , nil ); err != nil {
121131 return errors .Wrapf (err , "pushing to %q" , retag .invocationImageRef .String ())
122132 }
133+ return nil
134+ }
123135
136+ func pushBundle (dockerCli command.Cli , opts pushOptions , bndl * bundle.Bundle , retag retagResult ) error {
124137 resolver := remotes .CreateResolver (dockerCli .ConfigFile (), opts .registry .insecureRegistries ... )
125138 var display fixupDisplay = & plainDisplay {out : os .Stdout }
126139 if term .IsTerminal (os .Stdout .Fd ()) {
@@ -133,9 +146,7 @@ func runPush(dockerCli command.Cli, name string, opts pushOptions) error {
133146 fixupOptions = append (fixupOptions , remotes .WithComponentImagePlatforms (platforms ))
134147 }
135148 // bundle fixup
136- err = remotes .FixupBundle (context .Background (), bndl , retag .cnabRef , resolver , fixupOptions ... )
137-
138- if err != nil {
149+ if err := remotes .FixupBundle (context .Background (), bndl , retag .cnabRef , resolver , fixupOptions ... ); err != nil {
139150 return errors .Wrapf (err , "fixing up %q for push" , retag .cnabRef )
140151 }
141152 // push bundle manifest
@@ -185,7 +196,11 @@ type retagResult struct {
185196 invocationImageRef reference.Named
186197}
187198
188- func shouldRetagInvocationImage (meta metadata.AppMetadata , bndl * bundle.Bundle , tagOverride string ) (retagResult , error ) {
199+ func shouldRetagInvocationImage (meta metadata.AppMetadata , bndl * bundle.Bundle , tagOverride , bundleRef string ) (retagResult , error ) {
200+ // Use the bundle reference as a tag override
201+ if tagOverride == "" && bundleRef != "" {
202+ tagOverride = bundleRef
203+ }
189204 imgName := tagOverride
190205 var err error
191206 if imgName == "" {
0 commit comments