Sub topic 15
Deploying from a CI/CD pipeline
This chapter assumes you can write a databricks.yml, run databricks bundle validate, deploy and run, and that you know how OAuth and personal access tokens differ. It shows how to run the same bundle sequence from a pipeline without exposing secrets.
What changes in a pipeline
On your laptop, databricks auth login opens a browser and stores a profile. A CI runner has no browser and no saved profile, so it must authenticate with environment variables. The CLI reads DATABRICKS_HOST and credentials from the environment, so no ~/.databrickscfg file is needed.
You have two common options:
| Option | Variables | Notes |
|---|---|---|
| Service principal with OAuth (preferred) | `DATABRICKS_HOST`, `DATABRICKS_CLIENT_ID`, `DATABRICKS_CLIENT_SECRET` | Short-lived tokens, not tied to a person |
| Personal access token | `DATABRICKS_HOST`, `DATABRICKS_TOKEN` | Simple, but long-lived and usually tied to a user |
Use a service principal dedicated to the pipeline. Grant it only the permissions the bundle needs, such as permission to manage the job and read the notebook folder.
The pipeline stages
A typical pipeline validates on every change and deploys only from the main branch.
validate catches schema errors and bad references without changing the workspace. Running it on pull requests gives fast feedback at no risk.
Example: GitHub Actions
This workflow installs the CLI, then validates, deploys and runs the bundle from the earlier chapter. Credentials come from repository secrets.
name: deploy-bundle
on:
pull_request:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
env:
DATABRICKS_HOST: ${{ vars.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 dev --var cluster_id=${{ vars.CLUSTER_ID }}
- name: Deploy
if: github.ref == 'refs/heads/main'
run: databricks bundle deploy -t dev --var cluster_id=${{ vars.CLUSTER_ID }}
- name: Run
if: github.ref == 'refs/heads/main'
run: databricks bundle run -t dev --var cluster_id=${{ vars.CLUSTER_ID }} demo_jobThe host and cluster ID are not secret, so they are stored as plain variables. Only the client ID and secret are stored as secrets.
Common errors
- The job fails with an authentication error. Check that the variable names are spelled exactly as above. A typo makes the CLI fall back to looking for a profile, and it then reports that none was found.
- Deploy works locally but fails in CI. The service principal may lack permission on the workspace folder or the job. Grant access to it rather than widening its role.
- Pull requests from forks have no secrets. Most CI systems withhold secrets from fork builds. Keep the validate step free of credentials where you can, or require approval before running.
- Secrets appear in logs. Never
echocredentials or run commands withset -x. CI masks known secret values, but it cannot mask values it was not told about.
Using separate targets
Define a prod target in databricks.yml and give it its own service principal and secrets. Deploy to dev from feature work and to prod only from the main branch, ideally behind a manual approval step. A pipeline that can only reach dev cannot damage prod.
Exercise
Why is a service principal with OAuth preferred over a personal access token for a CI pipeline?
Show answer
A service principal is not tied to a person, so the pipeline keeps working when someone leaves. OAuth tokens are short-lived, so a leaked one expires quickly. You can also grant the principal only the permissions the bundle needs.
Exercise
Which environment variables does the CLI need to authenticate as a service principal with OAuth, and which of them should be stored as secrets?
Show answer
It needs DATABRICKSHOST, DATABRICKSCLIENTID and DATABRICKSCLIENT_SECRET. The client secret must be stored as a secret. The client ID is best stored as a secret too. The host is not sensitive and can be a plain variable.