Sub topic 11
Building and deploying artifacts such as wheels
This chapter assumes you can read a databricks.yml and a job resource file, and that you have run databricks bundle deploy against a dev target. You should also know how to package a Python project with pyproject.toml. The examples use a Python wheel because it is the most common artifact in Databricks projects.
What an artifact is in a bundle
Notebooks and plain .py files are uploaded as source files, so they need no build step. Shared logic usually should not live in notebooks, though. It belongs in a Python package with tests, a version number, and its own dependencies.
An artifact is a build output that a bundle produces and uploads when you deploy. You declare it under the top-level artifacts mapping. A job then refers to the built file as a library. Deployment does three things:
- It runs the build command you configured.
- It uploads the resulting file to the workspace, or to a Unity Catalog volume if you configure that.
- It rewrites the local path in your job definition to the uploaded location.
Because the build runs during deploy, one command keeps the code and the job definition in sync. You do not copy a wheel into the workspace by hand.
Project layout
This layout puts the package, its metadata, and the bundle files in one repository.
my_project/
databricks.yml
pyproject.toml
resources/
daily_etl.yml
src/
my_project/
__init__.py
main.pyThe pyproject.toml defines the package and one console script. The job will call that script by name.
[build-system]
requires = ["setuptools>=68", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my_project"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = []
[project.scripts]
run_etl = "my_project.main:main"
[tool.setuptools.packages.find]
where = ["src"]The entry point module reads its parameters from the command line. A Python wheel task passes its parameters list as command-line arguments.
# src/my_project/main.py
import argparse
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--catalog", required=True)
args = parser.parse_args()
# Imported inside main() so that building or importing the package
# does not require a Spark session.
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
spark.sql(f"USE CATALOG {args.catalog}")
print(f"Running ETL against catalog {args.catalog}")
if __name__ == "__main__":
main()Declaring the artifact
Add an artifacts block to databricks.yml. Keep the variables, include, and targets sections from the earlier chapters as they are.
bundle:
name: my_project
artifacts:
default:
type: whl
build: python -m build --wheel
path: .
include:
- resources/*.ymlThe keys work as follows:
| Key | Meaning |
|---|---|
| `default` | An arbitrary artifact key. Choose a name that identifies the package. |
| `type` | `whl` tells the CLI this artifact is a Python wheel. |
| `build` | The shell command that produces the wheel. It runs with `path` as the working directory. |
| `path` | The directory containing the project, relative to `databricks.yml`. |
The CLI does not install build for you. Install it in the environment where you run the CLI, for example with pip install build. If you use Poetry or uv, set build to poetry build or uv build --wheel instead. The only requirement is that a .whl file appears where your job expects it. With python -m build that is dist/.
If your project has several packages, declare several artifacts, each with its own path.
artifacts:
core_lib:
type: whl
build: python -m build --wheel
path: ./libs/core
etl_lib:
type: whl
build: python -m build --wheel
path: ./libs/etlAttaching the wheel to a job
The job resource references the built file under libraries and uses a python_wheel_task to run it. In resources/daily_etl.yml:
resources:
jobs:
daily_etl:
name: daily_etl
job_clusters:
- job_cluster_key: main
new_cluster:
spark_version: 15.4.x-scala2.12
node_type_id: ${var.node_type_id}
num_workers: 2
tasks:
- task_key: run_etl
job_cluster_key: main
python_wheel_task:
package_name: my_project
entry_point: run_etl
parameters:
- --catalog
- ${var.catalog}
libraries:
- whl: ../dist/*.whlThree details matter here:
- The
whlpath is relative to the resource file, not todatabricks.yml. The file is inresources/, so../dist/reaches the project'sdist/directory. - Glob patterns are allowed.
../dist/*.whlmatches whatever the build produced. package_nameis the name frompyproject.toml.entry_pointis the script name under[project.scripts], not a module path.
On deploy, the CLI uploads each matching wheel and replaces the local path with a workspace path in the job definition that it sends to Databricks. The local path appears only in your source files. You can see the rewrite by running databricks bundle validate -o json and reading the library entries. Before deploy they still show local paths, so check the job in the workspace UI after deploying.
Using a wheel with serverless compute
Serverless jobs do not take cluster-level libraries. You declare an environment instead and point the task at it.
resources:
jobs:
daily_etl:
name: daily_etl
environments:
- environment_key: default
spec:
client: "1"
dependencies:
- ../dist/*.whl
tasks:
- task_key: run_etl
environment_key: default
python_wheel_task:
package_name: my_project
entry_point: run_etl
parameters:
- --catalog
- ${var.catalog}The wheel is handled the same way: built on deploy, uploaded, and the path rewritten. Which client version values are accepted depends on the current serverless release, so check the Databricks documentation for your workspace.
Where artifacts are uploaded
By default, artifacts go to an artifacts folder under the bundle's root_path in the workspace. Each target has its own root_path, so dev and prod do not overwrite each other's wheels. You can move the destination with workspace.artifact_path. A Unity Catalog volume is a common choice, because volume permissions give you finer control over who can read the wheels.
targets:
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}
artifact_path: /Volumes/prod_catalog/default/bundle_artifacts/${bundle.name}/${bundle.target}The identity that deploys needs write access to that volume. The identity the job runs as needs read access. If you use run_as with a service principal, grant it READ VOLUME on the volume, or the job will fail at library installation.
The stale wheel problem
Compute caches installed wheels by file name and version. If you change code but leave version = "0.1.0", the new wheel has the same name as the old one. A long-running or reused cluster, for example an all-purpose cluster, may keep the previously installed copy. The job then runs old code with no error.
You have several options:
- Bump the version in
pyproject.tomlon every release. This is the most reliable option for production. - Use job clusters, which start fresh for each run and install the wheel every time.
- Restart the all-purpose cluster after deploying.
- For development, set
dynamic_version: trueon the artifact. The CLI then patches the wheel's version with a timestamp on each deploy, so every build is unique. This setting exists in recent CLI versions, so checkdatabricks --versionand the release notes if the key is rejected.
artifacts:
default:
type: whl
build: python -m build --wheel
path: .
dynamic_version: trueCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No such file` or a glob matches nothing at deploy | The build did not produce a wheel, or the `whl` path is relative to the wrong file | Run the build command by hand in `path` and check the path from the resource file's directory |
| `No module named build` | The `build` package is not installed where the CLI runs | `pip install build`, and add it to your CI setup |
| Job runs old code after a deploy | Same wheel version, cached install | Bump the version, use job clusters, or use `dynamic_version` in dev |
| `entry point not found` at run time | `entry_point` does not match a name in `[project.scripts]` | Use the script name, for example `run_etl`, not `my_project.main:main` |
| Permission denied on library install | The run-as identity cannot read the artifact location | Grant read access on the volume or workspace folder |
| Old `.whl` files get uploaded too | Stale files remain in `dist/` and the glob matches several | Clean `dist/` first, for example `build: rm -rf dist && python -m build --wheel` |
The last row is worth acting on early. A glob such as ../dist/*.whl uploads every match, so leftover wheels from earlier builds end up in the job's library list. Include the cleanup in the build command or in your CI job.
Verifying the result
Run these commands from the project root:
pip install build
databricks bundle validate -t dev
databricks bundle deploy -t dev
databricks bundle run -t dev daily_etlvalidate checks configuration only. It does not run the build, so a broken build command surfaces at deploy. After deploying, open the job in the workspace and check that the library entry points to an uploaded path under the target's artifact location. If the run succeeds, the task output should show the Running ETL against catalog line.
Exercise
Your job runs ../dist/*.whl from resources/daily_etl.yml. After you fix a bug and run databricks bundle deploy -t dev, the job still shows the old behavior on a reused all-purpose cluster. The wheel version is still 0.1.0. Give two causes that could contribute, and one change that fixes it for a production release.
Show answer
The cluster caches the installed wheel by name and version, and the new build has the same name and version, so the cluster keeps the old copy. A stale file in dist/ could also be matched by the glob and uploaded alongside the new one. For a production release, bump version in pyproject.toml (for example to 0.1.1) so the file name changes and the cluster installs the new build. Cleaning dist/ in the build command also prevents old wheels from being uploaded.
Exercise
A teammate sets artifact_path to a Unity Catalog volume and deploys successfully as themselves. The scheduled job, which uses run_as with a service principal, then fails while installing the library. What is the most likely cause?
Show answer
The service principal lacks read access to the volume. The deployer wrote the wheel with its own permissions, but the run-as identity reads it at run time. Granting READ VOLUME on the volume to the service principal fixes it.