-
Notifications
You must be signed in to change notification settings - Fork 161
bundle: add --deploy-apps to push source code during bundle deploy #5055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamesbroadhead
wants to merge
2
commits into
databricks:main
Choose a base branch
from
jamesbroadhead:jb/deploy-01-bundle-deploy-apps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package appdeploy | ||
|
|
||
| import ( | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/databricks/cli/libs/dyn" | ||
| "github.com/databricks/cli/libs/dyn/convert" | ||
| "github.com/databricks/cli/libs/dyn/dynvar" | ||
| ) | ||
|
|
||
| // ResolveAppConfig returns the app config with `${resources.*}` variable references | ||
| // resolved against the current bundle state. The app runtime configuration (env vars, | ||
| // command) can reference other bundle resources whose properties are known only after | ||
| // the initialization phase — so this has to be called after resources have been | ||
| // created and the bundle Config reflects their current state. | ||
| // | ||
| // appKey is the map key under `resources.apps` (usually equal to `app.Name` but not | ||
| // guaranteed — resources may be keyed by a local alias). | ||
| // | ||
| // The function takes a pointer to config.Root rather than *bundle.Bundle to avoid an | ||
| // import cycle between bundle and appdeploy (bundle → direct → dresources → appdeploy). | ||
| func ResolveAppConfig(cfg *config.Root, appKey string, app *resources.App) (*resources.AppConfig, error) { | ||
| if app == nil || app.Config == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| root := cfg.Value() | ||
|
|
||
| // Normalize the full config so that all typed fields are present, even those | ||
| // not explicitly set. This allows looking up resource properties by path. | ||
| normalized, _ := convert.Normalize(cfg, root, convert.IncludeMissingFields) | ||
|
|
||
| configPath := dyn.MustPathFromString("resources.apps." + appKey + ".config") | ||
| configV, err := dyn.GetByPath(root, configPath) | ||
| if err != nil || !configV.IsValid() { | ||
| return app.Config, nil //nolint:nilerr // missing config path means use default config | ||
| } | ||
|
|
||
| resourcesPrefix := dyn.MustPathFromString("resources") | ||
|
|
||
| // Resolve ${resources.*} references in the app config against the full bundle config. | ||
| // Other variable types (bundle.*, workspace.*, variables.*) are already resolved | ||
| // during the initialization phase and are left in place if encountered here. | ||
| resolved, err := dynvar.Resolve(configV, func(path dyn.Path) (dyn.Value, error) { | ||
| if !path.HasPrefix(resourcesPrefix) { | ||
| return dyn.InvalidValue, dynvar.ErrSkipResolution | ||
| } | ||
| return dyn.GetByPath(normalized, path) | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var resolvedConfig resources.AppConfig | ||
| if err := convert.ToTyped(&resolvedConfig, resolved); err != nil { | ||
| return nil, err | ||
| } | ||
| return &resolvedConfig, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package appdeploy_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/bundle/appdeploy" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/databricks/databricks-sdk-go/service/apps" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestResolveAppConfig_NilApp(t *testing.T) { | ||
| cfg := &config.Root{} | ||
| out, err := appdeploy.ResolveAppConfig(cfg, "my_app", nil) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, out) | ||
| } | ||
|
|
||
| func TestResolveAppConfig_AppWithoutConfig(t *testing.T) { | ||
| cfg := &config.Root{} | ||
| app := &resources.App{App: apps.App{Name: "my_app"}} | ||
| out, err := appdeploy.ResolveAppConfig(cfg, "my_app", app) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, out) | ||
| } | ||
|
|
||
| // TODO: add a test covering `${resources.*}` interpolation — requires setting up a | ||
| // config.Root with a populated dyn.Value via the standard bundle load path. Factored | ||
| // out to a follow-up to keep this draft PR scoped. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package phases | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/appdeploy" | ||
| "github.com/databricks/cli/libs/cmdio" | ||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/databricks/cli/libs/logdiag" | ||
| ) | ||
|
|
||
| // DeployApps uploads source code and triggers an AppDeployment for every app in the | ||
| // bundle. It is called at the end of the Deploy phase when the user asks for a | ||
| // single-command deploy-and-push (via `--deploy-apps` or `b.DeployApps = true`). | ||
| // | ||
| // The resource CRUD path (terraform/direct) provisions the app compute but does not | ||
| // push source code, which is why this exists as a separate phase: `w.Apps.Deploy` | ||
| // and file upload to the workspace are not modelled as resources. | ||
| func DeployApps(ctx context.Context, b *bundle.Bundle) { | ||
| appCount := len(b.Config.Resources.Apps) | ||
| if !b.DeployApps { | ||
| if appCount > 0 { | ||
| cmdio.LogString(ctx, skippedAppsMessage(b)) | ||
| } | ||
| return | ||
| } | ||
| if appCount == 0 { | ||
| return | ||
| } | ||
|
|
||
| log.Info(ctx, "Phase: deploy apps") | ||
| cmdio.LogString(ctx, "Deploying app source code...") | ||
|
|
||
| w := b.WorkspaceClient(ctx) | ||
| var failures []error | ||
| for key, app := range b.Config.Resources.Apps { | ||
| if app == nil { | ||
| continue | ||
| } | ||
| cmdio.LogString(ctx, fmt.Sprintf("✓ Deploying app source for %s", app.Name)) | ||
|
|
||
| config, err := appdeploy.ResolveAppConfig(&b.Config, key, app) | ||
| if err != nil { | ||
| failures = append(failures, fmt.Errorf("app %s: resolve config: %w", key, err)) | ||
| continue | ||
| } | ||
|
|
||
| deployment := appdeploy.BuildDeployment(app.SourceCodePath, config, app.GitSource) | ||
| if err := appdeploy.Deploy(ctx, w, app.Name, deployment); err != nil { | ||
| failures = append(failures, fmt.Errorf("app %s: %w", key, err)) | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| if len(failures) > 0 { | ||
| logdiag.LogError(ctx, errors.Join(failures...)) | ||
| return | ||
| } | ||
| cmdio.LogString(ctx, "App source code deployed!") | ||
| } | ||
|
|
||
| func skippedAppsMessage(b *bundle.Bundle) string { | ||
| keys := make([]string, 0, len(b.Config.Resources.Apps)) | ||
| for key := range b.Config.Resources.Apps { | ||
| keys = append(keys, key) | ||
| } | ||
| sort.Strings(keys) | ||
|
|
||
| var lines []string | ||
| lines = append(lines, fmt.Sprintf("Bundle contains %d Apps, but --deploy-apps was not set, not deploying apps. To deploy, run:", len(keys))) | ||
| for _, key := range keys { | ||
| lines = append(lines, " databricks bundle run "+key) | ||
| } | ||
| return strings.Join(lines, "\n") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package phases | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestSkippedAppsMessage(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Apps: map[string]*resources.App{ | ||
| "my_app": {}, | ||
| "other_app": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| msg := skippedAppsMessage(b) | ||
|
|
||
| expected := "Bundle contains 2 Apps, but --deploy-apps was not set, not deploying apps. To deploy, run:\n" + | ||
| " databricks bundle run my_app\n" + | ||
| " databricks bundle run other_app" | ||
| assert.Equal(t, expected, msg) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have recently added lifecycle.started field, can you check if it works? Unlike pure config option, it is actually tracked in resource state so it handles transitions between started/stopped/unspecified better.