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 used databricks bundle deploy (earlier chapters). It also assumes you know how to build a Python package with pyproject.toml.
What an artifact is
Notebooks are uploaded as plain files, but shared code is usually packaged. An artifact is a build output that the bundle produces locally and uploads during deployment. The most common one is a Python wheel (.whl), which a job installs on its cluster as a library.
You declare artifacts in the top-level artifacts mapping. During databricks bundle deploy, the CLI runs each artifact's build command, then uploads the result to the target's workspace folder.
Package the code
Put reusable logic in a package with an entry point. The pyproject.toml below defines a package named my_project with a console script called main.
[build-system]
requires = ["setuptools>=68", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my_project"
version = "0.1.0"
requires-python = ">=3.10"
[project.scripts]
main = "my_project.main:main"
[tool.setuptools.packages.find]
where = ["src"]Save this module as src/my_project/main.py, and add an empty src/my_project/__init__.py.
import sys
def main():
catalog = sys.argv[1] if len(sys.argv) > 1 else "dev_catalog"
print(f"Running against catalog: {catalog}")
if __name__ == "__main__":
main()Declare the artifact
Add this to databricks.yml:
artifacts:
my_wheel:
type: whl
build: python -m build --wheel
path: .type: whltells the CLI the output is a wheel, so it knows how to find and upload it.buildis the shell command to run. If you omit it for awhlartifact, the CLI falls back topython3 setup.py bdist_wheel, which needs asetup.py.pathis the folder the build runs in, relative todatabricks.yml.
The python -m build command comes from the build package. Install it with pip install build on your machine or CI runner.
Attach the wheel to a job
In resources/daily_etl.yml, add a python_wheel_task and list the wheel under libraries:
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_package
job_cluster_key: main
python_wheel_task:
package_name: my_project
entry_point: main
parameters:
- ${var.catalog}
libraries:
- whl: ../dist/*.whlThe whl path is relative to the resource file, so ../dist/*.whl points at the dist folder next to resources. The glob matches the wheel that build produced. During deployment the CLI uploads it and rewrites the path to the workspace location, so the job uses the uploaded copy.
package_name is the distribution name, and entry_point is the console script name from [project.scripts]. Each entry in parameters is passed to the script as a command-line argument, which is why main() reads sys.argv.
Build and deploy
databricks bundle validate -t dev
databricks bundle deploy -t dev
databricks bundle run -t dev daily_etldeploy triggers the build, so you do not need to run python -m build yourself. Check that dist/ now holds a .whl file.
Common errors
| Symptom | Likely cause |
|---|---|
| `No such file` or empty glob for `../dist/*.whl` | The build command failed, or `path` points at the wrong folder. |
| `python: No module named build` | The `build` package is not installed where the CLI runs. |
| Job fails with `entry point not found` | `entry_point` does not match a name under `[project.scripts]`. |
| Old code runs after a change | Cluster cached an older wheel with the same version. Bump `version`. |
Exercise
Your job fails because the whl: ../dist/*.whl library matches no files. Which two settings in the artifacts block should you check first, and why?
Show answer
Check build and path. The build command must succeed and write a wheel into dist. path sets the folder the command runs in, relative to databricks.yml. If it points elsewhere, dist is created in the wrong place and the glob in the job finds nothing.