diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 688f2003c..6c3cdf47e 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -61,5 +61,5 @@ jobs: dst=localhost:1338/roundtrip-test ./app/crane pull --format=oci $img $layout - ./app/crane push $layout $dst - diff <(./app/crane manifest $img) <(./app/crane manifest $dst) + ./app/crane push --image-refs=foo.images $layout $dst + diff <(./app/crane manifest $img) <(./app/crane manifest $(cat foo.images)) diff --git a/cmd/crane/cmd/push.go b/cmd/crane/cmd/push.go index 95c15510c..5de2ed050 100644 --- a/cmd/crane/cmd/push.go +++ b/cmd/crane/cmd/push.go @@ -16,6 +16,7 @@ package cmd import ( "fmt" + "io/ioutil" "os" "github.com/google/go-containerregistry/pkg/crane" @@ -30,6 +31,7 @@ import ( // NewCmdPush creates a new cobra.Command for the push subcommand. func NewCmdPush(options *[]crane.Option) *cobra.Command { index := false + imageRefs := "" cmd := &cobra.Command{ Use: "push PATH IMAGE", Short: "Push local image contents to a remote registry", @@ -43,23 +45,43 @@ func NewCmdPush(options *[]crane.Option) *cobra.Command { return err } - // TODO(generics): Make crane.Push support index. + o := crane.GetOptions(*options...) + ref, err := name.ParseReference(tag, o.Name...) + if err != nil { + return err + } + var h v1.Hash switch t := img.(type) { case v1.Image: - return crane.Push(t, tag, *options...) + if err := remote.Write(ref, t, o.Remote...); err != nil { + return err + } + if h, err = t.Digest(); err != nil { + return err + } case v1.ImageIndex: - o := crane.GetOptions(*options...) - ref, err := name.ParseReference(tag, o.Name...) - if err != nil { + if err := remote.WriteIndex(ref, t, o.Remote...); err != nil { + return err + } + if h, err = t.Digest(); err != nil { return err } - return remote.WriteIndex(ref, t, o.Remote...) + default: + return fmt.Errorf("cannot push type (%T) to registry", img) + } + + digest := ref.Context().Digest(h.String()) + if imageRefs != "" { + return ioutil.WriteFile(imageRefs, []byte(digest.String()), 0600) } + // TODO(mattmoor): think about printing the digest to standard out + // to facilitate command composition similar to ko build. - return fmt.Errorf("cannot push type (%T) to registry", img) + return nil }, } cmd.Flags().BoolVar(&index, "index", false, "push a collection of images as a single index, currently required if PATH contains multiple images") + cmd.Flags().StringVar(&imageRefs, "image-refs", "", "path to file where a list of the published image references will be written") return cmd } diff --git a/cmd/crane/doc/crane_push.md b/cmd/crane/doc/crane_push.md index b4181edd8..f88a353ae 100644 --- a/cmd/crane/doc/crane_push.md +++ b/cmd/crane/doc/crane_push.md @@ -13,8 +13,9 @@ crane push PATH IMAGE [flags] ### Options ``` - -h, --help help for push - --index push a collection of images as a single index, currently required if PATH contains multiple images + -h, --help help for push + --image-refs string path to file where a list of the published image references will be written + --index push a collection of images as a single index, currently required if PATH contains multiple images ``` ### Options inherited from parent commands diff --git a/go.sum b/go.sum index a6b7f73ba..844711124 100644 --- a/go.sum +++ b/go.sum @@ -432,7 +432,6 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0= github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0eJc8R6ouapiM= -github.com/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyycI+I= github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/handlers v0.0.0-20150720190736-60c7bfde3e33/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=