From fae16fd7cf09ec177ff5fbb9b2b026ed6fb9fcae Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 22 Jun 2026 19:01:26 +0200 Subject: [PATCH] bundle: add env var to enable experimental record_deployment_history Add DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY as an environment-variable way to opt into the experimental record_deployment_history setting, so the feature can be toggled per invocation without editing the bundle config. A new mutator in the initialize phase sets experimental.record_deployment_history when the env var is present. It never disables a setting the configuration already enabled. Co-authored-by: Isaac --- .../record_deployment_history/databricks.yml | 2 + .../record_deployment_history/out.test.toml | 3 + .../record_deployment_history/output.txt | 8 +++ .../record_deployment_history/script | 9 +++ .../apply_record_deployment_history_env.go | 36 ++++++++++ ...pply_record_deployment_history_env_test.go | 67 +++++++++++++++++++ bundle/env/record_deployment_history.go | 17 +++++ bundle/phases/initialize.go | 5 ++ 8 files changed, 147 insertions(+) create mode 100644 acceptance/bundle/experimental/record_deployment_history/databricks.yml create mode 100644 acceptance/bundle/experimental/record_deployment_history/out.test.toml create mode 100644 acceptance/bundle/experimental/record_deployment_history/output.txt create mode 100644 acceptance/bundle/experimental/record_deployment_history/script create mode 100644 bundle/config/mutator/apply_record_deployment_history_env.go create mode 100644 bundle/config/mutator/apply_record_deployment_history_env_test.go create mode 100644 bundle/env/record_deployment_history.go diff --git a/acceptance/bundle/experimental/record_deployment_history/databricks.yml b/acceptance/bundle/experimental/record_deployment_history/databricks.yml new file mode 100644 index 00000000000..576d7a9ef25 --- /dev/null +++ b/acceptance/bundle/experimental/record_deployment_history/databricks.yml @@ -0,0 +1,2 @@ +bundle: + name: test-bundle diff --git a/acceptance/bundle/experimental/record_deployment_history/out.test.toml b/acceptance/bundle/experimental/record_deployment_history/out.test.toml new file mode 100644 index 00000000000..f784a183258 --- /dev/null +++ b/acceptance/bundle/experimental/record_deployment_history/out.test.toml @@ -0,0 +1,3 @@ +Local = true +Cloud = false +EnvMatrix.DATABRICKS_BUNDLE_ENGINE = ["terraform", "direct"] diff --git a/acceptance/bundle/experimental/record_deployment_history/output.txt b/acceptance/bundle/experimental/record_deployment_history/output.txt new file mode 100644 index 00000000000..e742e244b21 --- /dev/null +++ b/acceptance/bundle/experimental/record_deployment_history/output.txt @@ -0,0 +1,8 @@ + +=== record_deployment_history is unset by default +>>> [CLI] bundle validate -o json +null + +=== DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY enables record_deployment_history +>>> [CLI] bundle validate -o json +true diff --git a/acceptance/bundle/experimental/record_deployment_history/script b/acceptance/bundle/experimental/record_deployment_history/script new file mode 100644 index 00000000000..a41bdae448e --- /dev/null +++ b/acceptance/bundle/experimental/record_deployment_history/script @@ -0,0 +1,9 @@ +title "record_deployment_history is unset by default" + +trace $CLI bundle validate -o json | jq .experimental + +title "DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY enables record_deployment_history" + +export DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY=true + +trace $CLI bundle validate -o json | jq .experimental.record_deployment_history diff --git a/bundle/config/mutator/apply_record_deployment_history_env.go b/bundle/config/mutator/apply_record_deployment_history_env.go new file mode 100644 index 00000000000..d7e1d7b8c63 --- /dev/null +++ b/bundle/config/mutator/apply_record_deployment_history_env.go @@ -0,0 +1,36 @@ +package mutator + +import ( + "context" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/bundle/env" + "github.com/databricks/cli/libs/diag" +) + +type applyRecordDeploymentHistoryEnv struct{} + +// ApplyRecordDeploymentHistoryEnv enables the experimental record_deployment_history +// setting when the DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY environment variable is +// set. This lets the feature be toggled per invocation without editing the bundle +// configuration; it never disables a setting that the configuration already enabled. +func ApplyRecordDeploymentHistoryEnv() bundle.Mutator { + return &applyRecordDeploymentHistoryEnv{} +} + +func (m *applyRecordDeploymentHistoryEnv) Name() string { + return "ApplyRecordDeploymentHistoryEnv" +} + +func (m *applyRecordDeploymentHistoryEnv) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { + if _, ok := env.RecordDeploymentHistory(ctx); !ok { + return nil + } + + if b.Config.Experimental == nil { + b.Config.Experimental = &config.Experimental{} + } + b.Config.Experimental.RecordDeploymentHistory = true + return nil +} diff --git a/bundle/config/mutator/apply_record_deployment_history_env_test.go b/bundle/config/mutator/apply_record_deployment_history_env_test.go new file mode 100644 index 00000000000..622f1fcca26 --- /dev/null +++ b/bundle/config/mutator/apply_record_deployment_history_env_test.go @@ -0,0 +1,67 @@ +package mutator_test + +import ( + "testing" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/bundle/config/mutator" + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestApplyRecordDeploymentHistoryEnv(t *testing.T) { + tests := []struct { + name string + envValue string + envSet bool + experimental *config.Experimental + want bool + }{ + { + name: "env not set leaves config untouched", + envSet: false, + want: false, + }, + { + name: "env set enables the experimental setting", + envValue: "true", + envSet: true, + want: true, + }, + { + name: "env set enables it even when experimental is already present", + envValue: "true", + envSet: true, + experimental: &config.Experimental{PythonWheelWrapper: true}, + want: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ctx := t.Context() + if tc.envSet { + ctx = env.Set(ctx, "DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY", tc.envValue) + } + + b := &bundle.Bundle{ + Config: config.Root{ + Experimental: tc.experimental, + }, + } + + diags := bundle.Apply(ctx, b, mutator.ApplyRecordDeploymentHistoryEnv()) + require.NoError(t, diags.Error()) + + if tc.want { + require.NotNil(t, b.Config.Experimental) + assert.True(t, b.Config.Experimental.RecordDeploymentHistory) + } else { + // When the env var is unset the mutator must not allocate Experimental. + assert.Nil(t, b.Config.Experimental) + } + }) + } +} diff --git a/bundle/env/record_deployment_history.go b/bundle/env/record_deployment_history.go new file mode 100644 index 00000000000..a118010d963 --- /dev/null +++ b/bundle/env/record_deployment_history.go @@ -0,0 +1,17 @@ +package env + +import "context" + +// recordDeploymentHistoryVariable names the environment variable that opts the +// bundle into recording deployment history. It is the environment-variable +// equivalent of setting experimental.record_deployment_history in the bundle +// configuration. +const recordDeploymentHistoryVariable = "DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY" + +// RecordDeploymentHistory returns the environment variable that opts the bundle +// into recording deployment history. +func RecordDeploymentHistory(ctx context.Context) (string, bool) { + return get(ctx, []string{ + recordDeploymentHistoryVariable, + }) +} diff --git a/bundle/phases/initialize.go b/bundle/phases/initialize.go index 80127843e83..e1926c6d842 100644 --- a/bundle/phases/initialize.go +++ b/bundle/phases/initialize.go @@ -34,6 +34,11 @@ func Initialize(ctx context.Context, b *bundle.Bundle) { validate.ValidateEngine(), validate.Scripts(), + // Updates (typed): b.Config.Experimental.RecordDeploymentHistory + // Enables the experimental record_deployment_history setting from the + // DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY environment variable. + mutator.ApplyRecordDeploymentHistoryEnv(), + // Updates (dynamic): sync.{paths,include,exclude} (makes them relative to bundle root rather than to definition file) // Rewrites sync paths to be relative to the bundle root instead of the file they were defined in. mutator.RewriteSyncPaths(),