Sub topic 15
Deploying from a CI/CD pipeline
This chapter assumes you have read the earlier chapters on authentication, Asset Bundles, and the bundle commands (validate, deploy, run). You should also know basic Git workflows and YAML. The examples use GitHub Actions, but the same steps work in any CI system that can run shell commands and store secrets.
What a pipeline changes
On your laptop, databricks auth login opens a browser and stores an OAuth token for you. A CI runner has no browser, no persistent home directory, and no person to click "Allow". The pipeline therefore needs three things:
- A non-interactive identity.
- The CLI installed on every run.
- A fixed sequence of commands that gives the same result each time.
The bundle files in version control define what gets deployed. The pipeline defines when and with which identity.
Use a service principal, not a person
A service principal is an identity for automation. It is not tied to an employee, so it keeps working when people change teams, and you can grant it only the permissions the deployment needs. Avoid putting a personal access token (PAT) in CI. A PAT acts as the user who created it, often lasts for months, and disappears from the pipeline when that user's account is removed.
The CLI supports OAuth machine-to-machine (M2M) authentication for service principals. You create an OAuth secret for the service principal and give the CLI three environment variables:
| Variable | Value |
|---|---|
| `DATABRICKS_HOST` | The workspace URL, for example `https://<workspace-host>` |
| `DATABRICKS_CLIENT_ID` | The service principal's application (client) ID |
| `DATABRICKS_CLIENT_SECRET` | The OAuth secret generated for that service principal |
The CLI reads these variables automatically. The pipeline needs no profile in ~/.databrickscfg and no auth login step. The client ID and host are not sensitive. The client secret is, so it belongs in the CI system's secret store.
Separate targets for each environment
Bundle targets let one databricks.yml describe several environments. The earlier chapter showed a dev target. A production target typically adds mode: production, a fixed deployment path, and a run_as identity:
targets:
dev:
mode: development
default: true
workspace:
host: https://<workspace-host>
prod:
mode: production
workspace:
host: https://<workspace-host>
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}
run_as:
service_principal_name: <application-id>The two modes behave differently:
developmentmode prefixes resource names with[dev <user>]and pauses schedules. Several developers can deploy the same bundle without overwriting each other.productionmode turns on extra validation. It checks that the deployment path is not specific to one user, and it expects a clear identity to run the resources as. Resources keep their real names and schedules stay active.
The <application-id> placeholder is the service principal's application ID. Replace it with the real value. Keep each target's workspace host in the target definition. That way a pipeline step cannot accidentally deploy to the wrong workspace because of a stale environment variable.
Pipeline stages
A typical pipeline runs different stages for pull requests and for merges:
Validation runs on every change because it is fast and catches YAML mistakes, unknown fields, and bad variable references before anything touches a workspace. Deployment to dev on pull requests gives reviewers a real environment to check. Deployment to prod runs only from the protected main branch.
A GitHub Actions workflow
The workflow below validates on every push and pull request. It deploys to prod only for pushes to main. It assumes the repository has the variables and secrets named in the env blocks.
name: deploy-bundle
on:
pull_request:
push:
branches: [main]
jobs:
validate:
runs-on: ubuntu-latest
env:
DATABRICKS_HOST: ${{ vars.DATABRICKS_HOST }}
DATABRICKS_CLIENT_ID: ${{ vars.DATABRICKS_CLIENT_ID }}
DATABRICKS_CLIENT_SECRET: ${{ secrets.DATABRICKS_CLIENT_SECRET }}
BUNDLE_VAR_cluster_id: ${{ vars.CLUSTER_ID }}
steps:
- uses: actions/checkout@v4
- uses: databricks/setup-cli@main
- run: databricks bundle validate -t prod
deploy-prod:
needs: validate
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
env:
DATABRICKS_HOST: ${{ vars.DATABRICKS_HOST }}
DATABRICKS_CLIENT_ID: ${{ vars.DATABRICKS_CLIENT_ID }}
DATABRICKS_CLIENT_SECRET: ${{ secrets.DATABRICKS_CLIENT_SECRET }}
BUNDLE_VAR_cluster_id: ${{ vars.CLUSTER_ID }}
steps:
- uses: actions/checkout@v4
- uses: databricks/setup-cli@main
- run: databricks bundle deploy -t prodSeveral details in this file matter:
databricks/setup-cliinstalls the CLI on the runner.@maintracks the newest release. For reproducible builds, pin the action to a release tag or commit, and pin the CLI version if you need exact repeatability.BUNDLE_VAR_cluster_idsets the bundle variablecluster_idwithout a--varflag. Any variablexcan be set asBUNDLE_VAR_x.environment: productionties the job to a GitHub environment. You can require manual approval on that environment and restrict which branches may use its secrets. Move the client secret into the environment's own secrets so only this job can read it.needs: validatestops the deployment if validation fails.
For a pull-request test run, add a job that executes databricks bundle deploy -t dev followed by databricks bundle run -t dev demo_job. Use a separate service principal with access only to the dev workspace.
Failure modes and gotchas
The deploy prompts for confirmation. If a deployment would delete a resource, such as a job you removed from the YAML, the CLI asks for approval. A CI runner has no terminal to answer, so the step fails or hangs. Pass --auto-approve only after you have decided that unattended deletes are acceptable. A manual-approval environment gate is a safer place to make that decision.
Concurrent deployments conflict. The CLI takes a lock on the deployment state in the workspace. Two pipeline runs deploying the same target at once can fail on that lock. Use your CI system's concurrency controls so only one production deploy runs at a time. Avoid --force-lock as a routine fix, because it can corrupt state if the other run is still active.
The run finishes but the job fails. databricks bundle run waits for the run to complete and returns a non-zero exit code if it fails. Rely on that exit code rather than parsing output, so the pipeline stage fails visibly.
Permissions are too broad. If the service principal is a workspace admin, a leaked secret gives an attacker full control of the workspace. Grant it only what the bundle needs, such as permission to create jobs in one folder and use specific compute. Create one service principal per environment so a dev secret cannot touch production.
Secrets rotate. OAuth secrets for service principals expire. Track the expiry date and rotate the CI secret before it lapses, or the first sign of trouble will be a failed production deploy.
Fork pull requests. Most CI systems withhold secrets from pull requests that come from forks. This is the intended behavior. Do not work around it by running untrusted code with production credentials.
Good practices
- Run
bundle validateon every change, for every target. - Keep deployments to production on a protected branch with required reviews.
- Treat the pipeline as the only way to change production. Manual UI edits to bundle-managed resources are overwritten on the next deploy.
- Log the CLI version at the start of a run with
databricks --version, so you can tell later which version produced a deployment.
Exercise
A pipeline step runs databricks bundle deploy -t prod and fails with an authentication error, even though the same command works on your laptop. The runner has DATABRICKS_HOST set. What is most likely missing, and why does the laptop work?
Show answer
The service principal credentials are missing: DATABRICKS_CLIENT_ID and DATABRICKS_CLIENT_SECRET. The laptop works because databricks auth login stored an OAuth token for your user in a profile. A CI runner has no such profile and no browser, so the CLI needs the M2M environment variables to authenticate non-interactively.
Exercise
Why should the dev and prod pipelines use different service principals, and what should each be allowed to do?
Show answer
A separate service principal per environment limits the damage if one secret leaks. The dev principal should have access only to the dev workspace and its resources. The prod principal should have only the permissions the bundle needs, such as creating the bundle's jobs and using specific compute, and should not be a workspace admin.