Sub topic 13
Deploying from a CI/CD pipeline
A bundle is a set of files in Git, so a pipeline can validate and deploy it on every change. This chapter shows how to move bundle commands out of your terminal and into an automated workflow. It covers authentication for a non-interactive runner, a GitHub Actions example, and the errors that most often break pipelines.
This chapter assumes you have read the earlier chapters. You should know the databricks.yml structure, targets, run_as, and the validate, deploy, run, and destroy commands. You should also know basic Git branching and how your CI system stores secrets.
Why automate deployment
Deploying from a laptop has three problems:
- The result depends on whoever ran the command, with their local CLI version and uncommitted edits.
- Production deployments carry a personal identity unless every developer remembers the right flags.
- Nothing records who deployed what, or whether the checks passed first.
A pipeline fixes these. Every deployment starts from a clean checkout of a known commit. It uses one pinned CLI version and one service identity. The CI system keeps a log of each run.
A common split between targets looks like this:
| Event | Target | Commands | Identity |
|---|---|---|---|
| Pull request opened or updated | `dev` | `validate`, optionally `deploy` and `run` a test job | CI service principal for dev |
| Merge to `main` | `prod` | `validate`, `deploy` | Production service principal |
| Manual approval or tag | `prod` | `run` for a one-off job | Production service principal |
The working model
Every CI run does the same four things:
- Check out the repository.
- Install the Databricks CLI at a pinned version.
- Authenticate using environment variables instead of an interactive login.
- Run the bundle commands against one target. The CLI exits with a non-zero code on failure, which fails the pipeline step.
Authenticating without a browser
databricks auth login opens a browser, so it cannot run on a CI runner. Instead, the CLI reads credentials from environment variables. For a service principal using OAuth machine-to-machine (M2M) authentication, set these three:
| Variable | Value |
|---|---|
| `DATABRICKS_HOST` | The workspace URL, for example `https://<your-workspace-url>` |
| `DATABRICKS_CLIENT_ID` | The service principal's application (client) ID |
| `DATABRICKS_CLIENT_SECRET` | An OAuth secret generated for that service principal |
Store the client ID and secret in your CI system's secret store. Never commit them to the repository, and never echo them in logs. Use a separate service principal, and therefore a separate secret, for each environment. A leaked dev secret then cannot touch production.
Which identity deploys and which identity runs
Two identities are involved, and they are often confused:
- The deploying identity is whoever authenticates the CLI. Here, that is the CI service principal.
- The run-as identity is who the jobs execute as. It is set by
run_asin the target, as in theprodtarget from the earlier chapter.
They can be the same principal. If they differ, the deploying identity needs permission to use the run-as identity. A deployment fails if the deployer lacks that permission, or lacks write access to the bundle's root_path.
In prod, the CI principal also needs permission on everything the bundle creates or modifies. That includes the root path, the catalog and schemas the job writes to, and the cluster policies it references.
A GitHub Actions workflow
This workflow validates and deploys to dev on pull requests. It deploys to prod after a merge to main. The secrets and variables are examples, so create your own with matching names.
name: bundle-ci
on:
pull_request:
branches: [main]
push:
branches: [main]
concurrency:
group: bundle-${{ github.ref }}
cancel-in-progress: false
jobs:
dev:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
env:
DATABRICKS_HOST: ${{ vars.DEV_DATABRICKS_HOST }}
DATABRICKS_CLIENT_ID: ${{ secrets.DEV_CLIENT_ID }}
DATABRICKS_CLIENT_SECRET: ${{ secrets.DEV_CLIENT_SECRET }}
steps:
- uses: actions/checkout@v4
- uses: databricks/setup-cli@main
- name: Validate
run: databricks bundle validate -t dev
- name: Deploy
run: databricks bundle deploy -t dev
- name: Run the job
run: databricks bundle run -t dev daily_etl
prod:
if: github.event_name == 'push'
runs-on: ubuntu-latest
environment: production
env:
DATABRICKS_HOST: ${{ vars.PROD_DATABRICKS_HOST }}
DATABRICKS_CLIENT_ID: ${{ secrets.PROD_CLIENT_ID }}
DATABRICKS_CLIENT_SECRET: ${{ secrets.PROD_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 prodSome details in this file are deliberate:
- The
environment: productionline lets you require a manual approval in GitHub before the job starts. It also scopes the production secrets to that job. - The
concurrencygroup queues runs for the same ref instead of cancelling a deployment halfway through. - The
devjob usesbundle runto execute the job and wait for it. The command exits non-zero if the run fails, so the pull request check fails too. - Each job's
envblock sets credentials only for that job, so the production secrets are not exposed to pull request runs. databricks/setup-cli@maintracks the latest CLI. For reproducible builds, pin a release instead. The action accepts aversioninput, and you should set it to the CLI version your team has tested.
The same flow in other systems
The commands do not change between CI systems. Only the syntax for secrets, triggers, and approvals does. In Azure DevOps, GitLab CI, or Jenkins you will do the same four things: install the CLI, export the three DATABRICKS_* variables from the secret store, run validate, then run deploy on the protected branch. In a shell-based system, the install step typically downloads the CLI from the official install script or a pinned release archive.
Non-interactive behavior and useful flags
Some commands prompt for confirmation. A prompt blocks forever on a runner that has no terminal, so the step hangs or fails.
databricks bundle deploycan ask for approval when the deployment would delete or recreate a resource that holds data, such as a Unity Catalog schema or a pipeline. In CI, pass--auto-approveonly after you have decided the change is safe. A review step before the merge is the usual safeguard.databricks bundle deploy --fail-on-active-runsfails the deployment if a job from the bundle is running. This stops a deployment from replacing a job definition in the middle of a long run.databricks bundle destroyalso prompts. Use it in CI only for throwaway environments, such as a per-pull-request dev target that you clean up when the pull request closes. If you use--auto-approvewith it, restrict the trigger carefully.--var="name=value"overrides a variable for one invocation. This lets a pipeline inject a value like a commit SHA or a schema name without editing the YAML.
Per-pull-request isolation
Two developers deploying the same bundle to the same dev target would overwrite each other's resources. In mode: development, the CLI prefixes resource names with [dev <username>]. With a shared CI service principal, every pull request has the same username, so the prefixes collide.
Two ways to avoid this:
- Add a variable such as
schema_suffix, pass--var="schema_suffix=pr_${PR_NUMBER}"from the pipeline, and use it in the job's parameters. Each pull request then writes to its own schema. - Use a dedicated target per environment and serialize runs with the
concurrencysetting, accepting that only one pull request is tested at a time.
The first approach scales better. It does need a cleanup step on pull request close that runs databricks bundle destroy with the same variable value.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `cannot configure default credentials` | The `DATABRICKS_*` variables are missing or empty, often because a secret name is misspelled or the job lacks access to the environment's secrets | Check the names in the `env` block and the secret scope |
| `403` or permission denied on deploy | The CI principal cannot write to `root_path` or cannot use the `run_as` principal | Grant the missing permission to the CI principal |
| Pipeline hangs on a deploy step | A confirmation prompt is waiting for input | Review the change, then pass `--auto-approve` |
| `validate` passes locally but fails in CI | A different CLI version, or a variable that has a default locally but no value in CI | Pin the CLI version and set required variables explicitly |
| Deployment succeeds but the job fails with a missing library | The wheel was not built before deploy | Add the build step, or define it under `artifacts` so the CLI builds it |
| Two deployments interfere with each other | Concurrent runs against the same target | Use a `concurrency` group so runs for one target are serialized |
Checklist for a production pipeline
- Pin the CLI version.
- Run
validatefor every target the pipeline touches, before any deploy. - Use one service principal per environment, with the least privilege that works.
- Keep credentials in the CI secret store and scope production secrets to a protected environment.
- Require a review and a green dev run before the merge that triggers a production deployment.
- Serialize deployments to the same target.
- Treat
--auto-approveas a decision you made, not a default.
Exercise
A pipeline step running databricks bundle deploy -t prod hangs for an hour and then times out. The same command works on your laptop. What is the most likely cause, and what should you check before applying the fix?
Show answer
The deployment probably needs a confirmation, for example because it would delete or recreate a resource, and the CI runner has no terminal to answer the prompt. Before adding --auto-approve, check which resource the CLI wants to change, for example by running the deploy on your laptop and reading the prompt, and confirm that removing or recreating it is intended and will not lose data you need.
Exercise
Your workflow runs on pull requests and on pushes to main, and it uses DATABRICKS_CLIENT_SECRET for the production service principal at the workflow level. Name two problems with this setup and one change that fixes both.
Show answer
Pull request runs can read the production secret, and a pull request could deploy to or alter production. Move the production credentials into a protected environment (such as environment: production in GitHub Actions), reference them only in the job that runs on pushes to main, and give pull request jobs dev-only credentials. This limits exposure of the production secret and restricts production deployments to the protected branch.