From c2f33a165d79d85e74b1cf0db2a4259b4f9e88bf Mon Sep 17 00:00:00 2001 From: Francisco Miranda Date: Thu, 5 Dec 2024 14:31:57 -0300 Subject: [PATCH 1/4] Add skipIfNoCache to tests --- cmd/test/cmd.go | 4 +++- pkg/model/test.go | 15 ++++++++------- pkg/remote/remote.go | 26 ++++++++++++++++---------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/cmd/test/cmd.go b/cmd/test/cmd.go index 7759cc7fa5ed..c4439615c0da 100644 --- a/cmd/test/cmd.go +++ b/cmd/test/cmd.go @@ -372,9 +372,11 @@ func doRun(ctx context.Context, servicesToTest []string, options *Options, ioCtr Caches: test.Caches, IgnoreRules: testIgnoreRules, Artifacts: test.Artifacts, - NoCache: options.NoCache, Hosts: test.Hosts, } + if test.SkipIfNoFileChanges { + params.CacheInvalidationKey = "const" + } ioCtrl.Out().Infof("Executing test container '%s'", name) if err := runner.Run(ctx, params); err != nil { diff --git a/pkg/model/test.go b/pkg/model/test.go index f7b6f36e5eab..0fc1281710ab 100644 --- a/pkg/model/test.go +++ b/pkg/model/test.go @@ -21,13 +21,14 @@ import ( ) type Test struct { - Image string `yaml:"image,omitempty"` - Context string `yaml:"context,omitempty"` - Commands []TestCommand `yaml:"commands,omitempty"` - DependsOn []string `yaml:"depends_on,omitempty"` - Caches []string `yaml:"caches,omitempty"` - Artifacts []Artifact `yaml:"artifacts,omitempty"` - Hosts []Host `yaml:"hosts,omitempty"` + Image string `yaml:"image,omitempty"` + Context string `yaml:"context,omitempty"` + Commands []TestCommand `yaml:"commands,omitempty"` + DependsOn []string `yaml:"depends_on,omitempty"` + Caches []string `yaml:"caches,omitempty"` + Artifacts []Artifact `yaml:"artifacts,omitempty"` + Hosts []Host `yaml:"hosts,omitempty"` + SkipIfNoFileChanges bool `yaml:"skipIfNoFileChanges,omitempty"` } type Host struct { diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index 5a39107df6c6..0b95d4696385 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -159,9 +159,13 @@ type Params struct { ExecutionEnvVars map[string]string Manifest *model.Manifest Command string - TemplateName string - DockerfileName string - BaseImage string + // CacheInvalidationKey is the value use to invalidate the cache. Defaults + // to a random value which essentially means no-cache. Setting this to a + // static or known value will reuse the build cache + CacheInvalidationKey string + TemplateName string + DockerfileName string + BaseImage string // ContextAbsolutePathOverride is the absolute path for the build context. Optional. // If this values is not defined it will default to the folder location of the // okteto manifest which is resolved through params.ManifestPathFlag @@ -186,8 +190,6 @@ type Params struct { // UseOktetoDeployIgnoreFile if enabled loads the docker ignore file from an // .oktetodeployignore file. Disabled by default UseOktetoDeployIgnoreFile bool - - NoCache bool } // dockerfileTemplateProperties internal struct with the information needed by the Dockerfile template @@ -297,11 +299,15 @@ func (r *Runner) Run(ctx context.Context, params *Params) error { return err } - randomNumber, err := rand.Int(rand.Reader, big.NewInt(1000000)) - if err != nil { - return err + cacheKey := params.CacheInvalidationKey + if cacheKey == "" { + + randomNumber, err := rand.Int(rand.Reader, big.NewInt(1000000)) + if err != nil { + return err + } + cacheKey = strconv.Itoa(int(randomNumber.Int64())) } - cacheKey := strconv.Itoa(int(randomNumber.Int64())) b, err := yaml.Marshal(params.Deployable) if err != nil { @@ -320,7 +326,7 @@ func (r *Runner) Run(ctx context.Context, params *Params) error { outputMode = buildCmd.DeployOutputModeOnBuild } - buildOptions := buildCmd.OptsFromBuildInfoForRemoteDeploy(buildInfo, &types.BuildOptions{OutputMode: outputMode, NoCache: params.NoCache}) + buildOptions := buildCmd.OptsFromBuildInfoForRemoteDeploy(buildInfo, &types.BuildOptions{OutputMode: outputMode}) buildOptions.Manifest = params.Manifest buildOptions.BuildArgs = append( buildOptions.BuildArgs, From 9efb2e49544232682a9281d0e5dcb143112038aa Mon Sep 17 00:00:00 2001 From: Francisco Miranda Date: Tue, 10 Dec 2024 14:10:40 -0300 Subject: [PATCH 2/4] Consider --no-cache in cache resolution --- cmd/test/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/test/cmd.go b/cmd/test/cmd.go index c4439615c0da..05fd9b4c9fb8 100644 --- a/cmd/test/cmd.go +++ b/cmd/test/cmd.go @@ -374,7 +374,7 @@ func doRun(ctx context.Context, servicesToTest []string, options *Options, ioCtr Artifacts: test.Artifacts, Hosts: test.Hosts, } - if test.SkipIfNoFileChanges { + if test.SkipIfNoFileChanges && !options.NoCache { params.CacheInvalidationKey = "const" } From f8f07a3426d0c337f95f97253cff0f27c95b641b Mon Sep 17 00:00:00 2001 From: Francisco Miranda Date: Tue, 10 Dec 2024 14:23:14 -0300 Subject: [PATCH 3/4] add to schema and fix tests --- pkg/model/schema_test.go | 2 +- pkg/schema/test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/model/schema_test.go b/pkg/model/schema_test.go index f86279d72649..482c39c8c536 100644 --- a/pkg/model/schema_test.go +++ b/pkg/model/schema_test.go @@ -220,7 +220,7 @@ func Test_getStructKeys(t *testing.T) { "model.StorageResource": {"size", "class"}, "model.Sync": {"folders", "rescanInterval", "compression", "verbose"}, "model.SyncFolder": {"localPath", "remotePath"}, - "model.Test": {"image", "context", "commands", "depends_on", "caches", "artifacts", "hosts"}, + "model.Test": {"image", "context", "commands", "depends_on", "caches", "artifacts", "hosts", "skipIfNoFileChanges"}, "model.TestCommand": {"name", "command"}, "model.Timeout": {"default", "resources"}, "model.VolumeSpec": {"labels", "annotations", "size", "class"}, diff --git a/pkg/schema/test.go b/pkg/schema/test.go index 186b07dfffb9..af8f3aa9be20 100644 --- a/pkg/schema/test.go +++ b/pkg/schema/test.go @@ -117,6 +117,12 @@ func (test) JSONSchema() *jsonschema.Schema { Description: "Base image used to run your test.", }) + testProps.Set("skipIfNoFileChanges", &jsonschema.Schema{ + Type: &jsonschema.Type{Types: []string{"boolean"}}, + Title: "skipIfNoFileChanges", + Description: "Skip the test execution if no files have changed since the last test run. This is useful to avoid running tests when the code hasn't changed.", + }) + return &jsonschema.Schema{ Type: &jsonschema.Type{Types: []string{"object"}}, Title: "test", From d258bd5c3f2f32504f0da193115f5ae3323f513a Mon Sep 17 00:00:00 2001 From: Francisco Miranda Date: Wed, 11 Dec 2024 09:22:07 -0300 Subject: [PATCH 4/4] update schema --- schema.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/schema.json b/schema.json index 4291bafa5eed..ea689332db09 100644 --- a/schema.json +++ b/schema.json @@ -1264,6 +1264,11 @@ "description": "Base image used to run your test.", "title": "image", "type": "string" + }, + "skipIfNoFileChanges": { + "description": "Skip the test execution if no files have changed since the last test run. This is useful to avoid running tests when the code hasn't changed.", + "title": "skipIfNoFileChanges", + "type": "boolean" } }, "required": [