Skip to content

Commit

Permalink
Add option to skaffold run similar to "skaffold build -b" (#4734)
Browse files Browse the repository at this point in the history
* Add option to skaffold run similar to "skaffold build -b"

See #4494

* fix comments

* fix linters

Co-authored-by: Gaurav <[email protected]>
  • Loading branch information
anshlykov and gsquared94 authored Sep 21, 2020
1 parent e1339dd commit eb86b7f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
1 change: 0 additions & 1 deletion cmd/skaffold/app/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func NewCmdBuild() *cobra.Command {
WithExample("Print the final image names", "build -q --dry-run").
WithCommonFlags().
WithFlags(func(f *pflag.FlagSet) {
f.StringSliceVarP(&opts.TargetImages, "build-image", "b", nil, "Choose which artifacts to build. Artifacts with image names that contain the expression will be built only. Default is to build sources for all artifacts")
f.BoolVarP(&quietFlag, "quiet", "q", false, "Suppress the build output and print image built on success. See --output to format output.")
f.VarP(buildFormatFlag, "output", "o", "Used in conjunction with --quiet flag. "+buildFormatFlag.Usage())
f.StringVar(&buildOutputFlag, "file-output", "", "Filename to write build images to")
Expand Down
9 changes: 9 additions & 0 deletions cmd/skaffold/app/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ var flagRegistry = []Flag{
FlagAddMethod: "DurationVar",
DefinedOn: []string{"deploy", "dev", "run", "debug"},
},
{
Name: "build-image",
Shorthand: "b",
Usage: "Only build artifacts with image names that contain the given substring. Default is to build sources for all artifacts",
Value: &opts.TargetImages,
DefValue: []string{},
FlagAddMethod: "StringSliceVar",
DefinedOn: []string{"build", "run"},
},
{
Name: "detect-minikube",
Usage: "Use heuristics to detect a minikube cluster",
Expand Down
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewCmdRun() *cobra.Command {

func doRun(ctx context.Context, out io.Writer) error {
return withRunner(ctx, func(r runner.Runner, config *latest.SkaffoldConfig) error {
bRes, err := r.BuildAndTest(ctx, out, config.Build.Artifacts)
bRes, err := r.BuildAndTest(ctx, out, targetArtifacts(opts, config))
if err != nil {
return fmt.Errorf("failed to build: %w", err)
}
Expand Down
55 changes: 55 additions & 0 deletions cmd/skaffold/app/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ limitations under the License.
package cmd

import (
"context"
"io"
"io/ioutil"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
)

Expand All @@ -37,3 +43,52 @@ func TestNewCmdRun(t *testing.T) {
t.CheckDeepEqual(false, opts.EnableRPC)
})
}

type mockRunRunner struct {
runner.Runner
artifactImageNames []string
}

func (r *mockRunRunner) BuildAndTest(_ context.Context, _ io.Writer, artifacts []*latest.Artifact) ([]build.Artifact, error) {
var result []build.Artifact
for _, artifact := range artifacts {
imageName := artifact.ImageName
r.artifactImageNames = append(r.artifactImageNames, imageName)
result = append(result, build.Artifact{
ImageName: imageName,
})
}

return result, nil
}

func (r *mockRunRunner) DeployAndLog(context.Context, io.Writer, []build.Artifact) error {
return nil
}

func TestBuildImageFlag(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
mockRunner := &mockRunRunner{}
t.Override(&createRunner, func(config.SkaffoldOptions) (runner.Runner, *latest.SkaffoldConfig, error) {
return mockRunner, &latest.SkaffoldConfig{
Pipeline: latest.Pipeline{
Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{
{ImageName: "first"},
{ImageName: "second-test"},
{ImageName: "test"},
{ImageName: "aaabbbccc"},
},
},
},
}, nil
})
t.Override(&opts, config.SkaffoldOptions{
TargetImages: []string{"test"},
})

err := doRun(context.Background(), ioutil.Discard)
t.CheckNoError(err)
t.CheckDeepEqual([]string{"second-test", "test"}, mockRunner.artifactImageNames)
})
}
4 changes: 3 additions & 1 deletion docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Examples:
skaffold build -q --dry-run
Options:
-b, --build-image=[]: Choose which artifacts to build. Artifacts with image names that contain the expression will be built only. Default is to build sources for all artifacts
-b, --build-image=[]: Only build artifacts with image names that contain the given substring. Default is to build sources for all artifacts
--cache-artifacts=true: Set to false to disable default caching of artifacts
--cache-file='': Specify the location of the cache file (default $HOME/.skaffold/cache)
-c, --config='': File for global configurations (defaults to $HOME/.skaffold/config)
Expand Down Expand Up @@ -804,6 +804,7 @@ Examples:
skaffold run -p <profile>
Options:
-b, --build-image=[]: Only build artifacts with image names that contain the given substring. Default is to build sources for all artifacts
--cache-artifacts=true: Set to false to disable default caching of artifacts
--cache-file='': Specify the location of the cache file (default $HOME/.skaffold/cache)
--cleanup=true: Delete deployments after dev or debug mode is interrupted
Expand Down Expand Up @@ -846,6 +847,7 @@ Use "skaffold options" for a list of global command-line options (applies to all
```
Env vars:

* `SKAFFOLD_BUILD_IMAGE` (same as `--build-image`)
* `SKAFFOLD_CACHE_ARTIFACTS` (same as `--cache-artifacts`)
* `SKAFFOLD_CACHE_FILE` (same as `--cache-file`)
* `SKAFFOLD_CLEANUP` (same as `--cleanup`)
Expand Down

0 comments on commit eb86b7f

Please sign in to comment.