Sub topic 13
Deploying from a CI/CD pipeline
This chapter assumes you can write a databricks.yml with dev and prod targets, run databricks bundle validate, deploy, and run by hand, and read basic GitHub Actions or similar pipeline YAML. You should also know what a service principal is (a non-human identity in Databricks).
Why automate deployment
Deploying from a laptop ties production to one person's credentials and local state. A pipeline gives you a repeatable process: every change is reviewed in Git, validated the same way, and deployed by the same identity. The bundle commands you already know are the pipeline steps. CI/CD only adds triggers, authentication, and ordering.
The standard flow
A common pattern uses two triggers:
- Pull request: validate the bundle and deploy it to a dev target to test it.
- Merge to main: deploy to prod.
Authenticating without a browser
databricks auth login opens a browser, so it does not work in a pipeline. Instead, the CLI reads environment variables. The recommended approach for automation is OAuth machine-to-machine (M2M) with a service principal. You create an OAuth secret for the service principal and store these values as pipeline secrets:
| Variable | Meaning |
|---|---|
| `DATABRICKS_HOST` | Workspace URL |
| `DATABRICKS_CLIENT_ID` | Service principal application ID |
| `DATABRICKS_CLIENT_SECRET` | OAuth secret for that principal |
Never write these values into databricks.yml or commit them. Store them in your CI system's secret store.
An example pipeline
This GitHub Actions workflow deploys to prod when code lands on main. It uses the databricks/setup-cli action to install the CLI.
name: deploy-prod
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
env:
DATABRICKS_HOST: ${{ secrets.DATABRICKS_HOST }}
DATABRICKS_CLIENT_ID: ${{ secrets.DATABRICKS_CLIENT_ID }}
DATABRICKS_CLIENT_SECRET: ${{ secrets.DATABRICKS_CLIENT_SECRET }}
steps:
- uses: actions/checkout@v4
- uses: databricks/setup-cli@main
- name: Validate
run: databricks bundle validate -t prod
- name: Deploy
run: databricks bundle deploy -t prodA matching pull request workflow would trigger on pull_request, use the dev credentials, and run validate, deploy -t dev, and optionally bundle run -t dev daily_etl so a failing job blocks the merge.
The environment: production line is a GitHub feature that lets you require manual approval before the job runs. Other CI systems have equivalents.
How this connects to run_as
The pipeline's identity and the job's identity are separate settings. The pipeline authenticates as a principal that deploys the bundle. The run_as setting in the prod target decides which identity the deployed jobs execute as. Using the same service principal for both is simple and common. Using different ones lets you restrict deployment rights separately from data access.
The principal needs permission to create jobs in the workspace, to write to the bundle's root_path, and to use whatever catalogs the job reads and writes.
Common errors
- Authentication fails in CI but works locally. Locally you may use a profile from
auth login. In CI, check that all three environment variables are set and the secret has not expired. - Target mismatch. Without
-t, the CLI uses the target markeddefault: true. In a pipeline, always pass-texplicitly so a prod job never deploys to dev by accident. - Concurrent deployments. Two pipeline runs deploying the same target at once can conflict. Serialize them, for example with your CI system's concurrency controls.
- Deploying to the wrong path. Production mode expects a shared
root_path, as in the earlier prod target. A per-user path ties the deployment to one identity.
Exercise
Your pipeline runs databricks bundle deploy on merge to main with no -t flag, and the bundle has dev marked default: true. What happens, and how do you fix it?
Show answer
The deployment goes to the dev target, because the CLI falls back to the default target. Fix it by passing the target explicitly in the pipeline, for example databricks bundle deploy -t prod.
Exercise
Why does a CI pipeline use OAuth M2M environment variables instead of databricks auth login?
Show answer
auth login needs an interactive browser sign-in, which a pipeline cannot do. M2M credentials (host, client ID, client secret) for a service principal are supplied through environment variables, so the CLI authenticates without user interaction and without tying deployments to a person.