Sub topic 11
Authenticating CI with a service principal
This chapter assumes you have read the earlier chapters on Databricks Asset Bundles and deployment targets. You should also know how GitHub Actions secrets and variables work.
Why not a personal token
A personal access token acts as one person. If that person leaves, the token stops working and the pipeline breaks. Deployments also show up in audit logs under that person's name, and the token carries all of their permissions.
A service principal is an identity for automation. It has no password login, it belongs to the account rather than a person, and you can grant it only the access CI needs.
How the CLI authenticates
CI uses OAuth machine-to-machine (M2M) authentication. The service principal holds a client ID and a client secret. The Databricks CLI exchanges them for a short-lived access token and refreshes it as needed. You never create or store a long-lived token.
The CLI reads three environment variables:
| Variable | Value |
|---|---|
| `DATABRICKS_HOST` | The workspace URL for the target. |
| `DATABRICKS_CLIENT_ID` | The service principal's application ID. |
| `DATABRICKS_CLIENT_SECRET` | The OAuth secret generated for it. |
Setting it up
- Create a service principal in the account console or workspace admin settings, and add it to the workspace.
- Generate an OAuth secret for it. The secret is shown once, so copy it immediately.
- Store the client ID and secret as CI secrets. Store the host as a plain CI variable, because a URL is not sensitive.
- Grant the service principal only what the deployment needs. Typically that is permission to create jobs and to manage the bundle's workspace folder. It may also need
USE CATALOGandCREATEprivileges on the target schemas.
Use a separate service principal for each environment where you can. A leaked staging secret then cannot touch production.
Using it in the workflow
The workflow from the CI chapter passes the three values as environment variables:
validate:
needs: test
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
- run: databricks bundle validate -t stagingNo login step is needed. The CLI detects the variables and picks M2M OAuth automatically. You can confirm which identity is active by adding databricks current-user me as a step. The output should show the service principal, not a person.
Authenticating versus running as
These are two separate settings.
- Deploying identity. This is the identity that runs
bundle deploy. In CI it comes from the environment variables above. - Run identity. This is the identity the jobs execute as. The
run_assetting in theprodtarget sets it, as in thedatabricks.ymlfrom the targets chapter.
They can be the same service principal or different ones. If they differ, the deploying identity needs permission to use the run-as principal. In Databricks this is the Service Principal User role on it.
Common errors
- Host mismatch.
DATABRICKS_HOSTpoints at one workspace while the target indatabricks.ymlnames another. The CLI reports an error. Keep each environment's host in sync with its target. - Mixed credentials. A
DATABRICKS_TOKENvariable or a profile in~/.databrickscfgis also set. The CLI then reports conflicting auth settings. Set only the M2M variables in CI. - Expired secret. OAuth secrets have a lifetime. A pipeline that worked for months can start returning authentication errors. Record the expiry and rotate the secret before it lapses.
- Permission denied after a successful login. Authentication worked, but the service principal lacks rights on the folder, catalog, or run-as principal. Fix the grants. Do not switch to a personal token.
Exercise
A deploy job fails with a permission error on the bundle's workspace folder, although databricks current-user me shows the correct service principal. Is the problem authentication or authorization, and what do you change?
Show answer
Authorization. The service principal authenticated successfully but lacks permission on the folder. Grant it Can Manage on the bundle's root path, or on its parent folder. Replacing the secret would not help.