Sub topic 13
Validating, deploying, and running a bundle
This chapter covers the bundle commands that take a databricks.yml file from your repository to a running job in a workspace. It assumes you can write a basic databricks.yml (covered in the previous chapter), have an authenticated CLI profile, and know what a job and a cluster are.
The bundle life cycle
A bundle is a set of resource definitions kept in version control. Four CLI commands move it through its life cycle:
databricks bundle validatechecks the configuration.databricks bundle deploycreates or updates the resources in a workspace.databricks bundle runstarts a job or pipeline that the bundle deployed.databricks bundle destroydeletes everything the bundle deployed.
Each command takes a target with -t. A target is a named environment in the targets: section of databricks.yml, such as dev or prod. If you omit -t, the CLI uses the target marked default: true.
The example bundle
The examples below use this databricks.yml. It declares one notebook job, a variable for the cluster ID, and a dev target.
bundle:
name: demo_bundle
variables:
cluster_id:
description: ID of an existing cluster to run the job on
resources:
jobs:
demo_job:
name: demo_job
tasks:
- task_key: main
existing_cluster_id: ${var.cluster_id}
notebook_task:
notebook_path: ./demo_notebook.ipynb
targets:
dev:
mode: development
default: true
workspace:
host: https://<workspace-host>The variable cluster_id has no default value. You must supply one when you run a command that needs it, which the next sections show.
Validating
databricks bundle validate -t devValidation reads the configuration, resolves variables and target overrides, and reports problems. It catches YAML syntax errors, unknown keys, and references that do not resolve, such as ${var.cluster_id} with no declared variable. It also prints a summary of the resolved bundle: name, target, workspace host, and the user it will act as.
Validation does not prove that the deployment will succeed. It checks the configuration, not the state of the workspace. A cluster ID that does not exist, or a path you have no permission to write to, still fails later at deploy or run time. Treat validate as a fast first gate, and run it on every commit in CI.
To see the fully resolved configuration, add JSON output:
databricks bundle validate -t dev --output jsonThis is useful when a value comes from a variable or a target override and you want to confirm what the CLI will really send.
Deploying
databricks bundle deploy -t dev --var cluster_id=<cluster-id>Deploy does three things:
- It uploads the bundle's local files, such as notebooks, to a folder in the workspace.
- It creates or updates each declared resource, here the job
demo_job. - It records what it created in deployment state, so later deploys know what to update and what to remove.
The state tracking is what makes deploys repeatable. If you rename a task and deploy again, the existing job is updated. If you delete a resource from databricks.yml and deploy, the CLI removes that resource from the workspace. Do not edit bundle-managed jobs in the UI, because the next deploy overwrites those changes with what the file says.
Where files go
By default the bundle is deployed under the deploying user's home folder, in a path that includes the bundle name and target:
/Workspace/Users/<your-user>/.bundle/demo_bundle/devInside it, files/ holds your uploaded sources and state/ holds the deployment state. You can override this with workspace.root_path for a target, which is common for production deployments that should not live in one person's home folder.
What development mode changes
The dev target above uses mode: development. In this mode the CLI makes the deployment safe for experimentation:
- Deployed resource names get a
[dev <your-user>]prefix, so your copy ofdemo_jobdoes not collide with a teammate's. - Schedules and triggers on jobs are paused.
- Deployments from several developers stay separate.
A prod target would normally use mode: production. Production mode applies stricter checks, such as requiring a defined root path and a run identity, and it does not add the name prefix. Check the current Databricks documentation for the exact rules, because they have changed between CLI versions.
Ways to pass variables
You have three options for the cluster_id variable. Each is useful in a different setting.
| Method | Example | Good for |
|---|---|---|
| Command-line flag | `--var cluster_id=0123-456789-abcdefgh` | One-off runs and CI steps |
| Environment variable | `export BUNDLE_VAR_cluster_id=0123-456789-abcdefgh` | CI systems that set variables per job |
| Default in YAML | `default: 0123-456789-abcdefgh` under the variable | Values that rarely change |
A target can also set a variable value under targets.<name>.variables, which lets dev and prod use different clusters without any flags.
Running
databricks bundle run -t dev --var cluster_id=<cluster-id> demo_jobThe last argument is the resource key from databricks.yml. That is demo_job, the key under resources.jobs, not the display name. If the bundle has only one runnable resource, you can leave the key out.
bundle run starts the job and then waits. It streams the run URL and status updates to your terminal, and it exits with a non-zero status if the run fails. That exit code is what lets a CI step fail when the job fails. If you only want to start the run and move on, use the --no-wait flag.
Run uses the deployed copy of the job. If you edit the notebook locally and run without deploying first, you run the old code. Always deploy before you run.
The full sequence
A CI pipeline runs the same three commands you run by hand:
databricks bundle validate -t dev
databricks bundle deploy -t dev --var cluster_id=<cluster-id>
databricks bundle run -t dev --var cluster_id=<cluster-id> demo_jobBecause each command fails with a non-zero exit code on error, a shell script with set -e stops at the first problem.
#!/usr/bin/env bash
set -euo pipefail
TARGET="${1:-dev}"
databricks bundle validate -t "$TARGET"
databricks bundle deploy -t "$TARGET" --var cluster_id="$CLUSTER_ID"
databricks bundle run -t "$TARGET" --var cluster_id="$CLUSTER_ID" demo_jobDestroying
databricks bundle destroy -t dev --var cluster_id=<cluster-id>Destroy deletes the jobs and other resources recorded in the deployment state, and the uploaded files folder. It asks for confirmation first. Pass --auto-approve to skip the prompt in automation, and do that only for disposable targets such as per-developer or per-pull-request environments.
Destroy removes what the bundle created. It does not touch resources it did not create, such as the existing cluster in this example.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a missing variable value | `cluster_id` has no default and no `--var` was given | Pass `--var`, set `BUNDLE_VAR_cluster_id`, or add a default |
| Unknown field in `validate` output | Misspelled key or wrong indentation in YAML | Compare against the resource schema and fix the key |
| Authentication or 401/403 error on deploy | Wrong profile, expired OAuth login, or missing permission | Run `databricks auth profiles`, then log in again or pass `-p <profile>` |
| `bundle run` reports the resource was not found | Wrong resource key, or the bundle was never deployed to this target | Use the key under `resources.jobs`, and deploy to the same `-t` first |
| Job runs old code | Local edits were not deployed | Run `bundle deploy` again before `bundle run` |
| Two people overwrite each other's job | Both deployed to a shared path without development mode | Use `mode: development` or a per-user `root_path` |
Choosing between bundles and direct API calls
Bundles describe the desired state and let the CLI work out the changes. Direct API calls, such as jobs/run-now, perform one action. Use a bundle when the job definition itself should be versioned, reviewed, and promoted across environments. Use the API or SDK when you only need to trigger or inspect something that already exists.
Exercise
You change the schedule in databricks.yml for the dev target and run databricks bundle run -t dev demo_job. The job still behaves the old way. What went wrong, and what should the sequence be? Also, why does the deployed job in dev have a [dev <your-user>] prefix in its name?
Show answer
bundle run starts the job as it was last deployed. Editing the local file changes nothing in the workspace until you deploy. The sequence is bundle validate -t dev, then bundle deploy -t dev, then bundle run -t dev demo_job. The prefix comes from mode: development, which names resources per user so developers' copies of the same bundle do not collide. In that mode schedules are also paused, so the schedule would not fire on its own anyway.
Exercise
A CI step runs databricks bundle destroy -t dev --auto-approve and then databricks bundle deploy -t dev. Does destroy remove the existing cluster referenced by existing_cluster_id? Explain.
Show answer
No. Destroy deletes only the resources the bundle created and recorded in its deployment state, plus its uploaded files. The cluster existed before the bundle and is referenced by ID, so the bundle does not own it.