diff --git a/pkg/skaffold/initializer/init.go b/pkg/skaffold/initializer/init.go index 5189572ba54..4f2701096e7 100644 --- a/pkg/skaffold/initializer/init.go +++ b/pkg/skaffold/initializer/init.go @@ -116,13 +116,13 @@ func DoInit(out io.Writer, c Config) error { } images := k.GetImages() if c.Analyze { - return printAnalyzeJSON(out, dockerfiles, images) + return printAnalyzeJSON(out, c.SkipBuild, dockerfiles, images) } var pairs []dockerfilePair // conditionally generate build artifacts if !c.SkipBuild { if len(dockerfiles) == 0 { - return errors.New("one or more valid Dockerfiles must be present to run skaffold; please provide at least one Dockerfile and try again") + return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again or run `skaffold init --skip-build`") } if c.CliArtifacts != nil { @@ -289,7 +289,10 @@ func generateSkaffoldPipeline(k Initializer, dockerfilePairs []dockerfilePair) ( return pipelineStr, nil } -func printAnalyzeJSON(out io.Writer, dockerfiles, images []string) error { +func printAnalyzeJSON(out io.Writer, skipBuild bool, dockerfiles, images []string) error { + if !skipBuild && len(dockerfiles) == 0 { + return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again or run `skaffold init --skip-build`") + } a := struct { Dockerfiles []string `json:"dockerfiles,omitempty"` Images []string `json:"images,omitempty"` diff --git a/pkg/skaffold/initializer/init_test.go b/pkg/skaffold/initializer/init_test.go index 9e0c5440c7e..12c12ed2a2d 100644 --- a/pkg/skaffold/initializer/init_test.go +++ b/pkg/skaffold/initializer/init_test.go @@ -28,6 +28,8 @@ func TestPrintAnalyzeJSON(t *testing.T) { name string dockerfiles []string images []string + skipBuild bool + shouldErr bool expected string }{ { @@ -37,25 +39,25 @@ func TestPrintAnalyzeJSON(t *testing.T) { expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"],\"images\":[\"image1\",\"image2\"]}", }, { - name: "no dockerfile", - images: []string{"image1", "image2"}, - expected: "{\"images\":[\"image1\",\"image2\"]}", - }, + name: "no dockerfile, skip build", + images: []string{"image1", "image2"}, + skipBuild: true, + expected: "{\"images\":[\"image1\",\"image2\"]}"}, { - name: "no images", - dockerfiles: []string{"Dockerfile", "Dockerfile_2"}, - expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"]}", + name: "no dockerfile", + images: []string{"image1", "image2"}, + shouldErr: true, }, { - name: "no dockerfiles or images", - expected: "{}", + name: "no dockerfiles or images", + shouldErr: true, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { out := bytes.NewBuffer([]byte{}) - err := printAnalyzeJSON(out, test.dockerfiles, test.images) - testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, out.String()) + err := printAnalyzeJSON(out, test.skipBuild, test.dockerfiles, test.images) + testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, out.String()) }) } }