Sub topic 13
Deploying and promoting with a CD workflow
This chapter assumes you have a bundle with dev, staging, and prod targets, a CI workflow that runs unit tests and databricks bundle validate, and a service principal for CI. It also assumes a job in the bundle that runs your integration tests in the workspace.
What the CD workflow does
Continuous integration checks a pull request. Continuous deployment (CD) starts after a merge to main. It ships the merged commit through the environments in order, and each stage must succeed before the next one starts.
- Deploy the bundle to staging.
- Run the integration test job in staging.
- Deploy the same commit to production, after a person approves.
The workflow
This workflow assumes resources/ defines a job with the resource key integration_tests. Each stage uses needs, so a failure stops everything after it.
name: cd
on:
push:
branches: [main]
jobs:
deploy-staging:
runs-on: ubuntu-latest
environment: staging
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 deploy -t staging
- run: databricks bundle run -t staging integration_tests
deploy-prod:
needs: deploy-staging
runs-on: ubuntu-latest
environment: production
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 deploy -t proddatabricks bundle run waits for the job to finish. It exits with a non-zero code if the job fails, which fails the GitHub Actions step and blocks deploy-prod.
Why GitHub environments
Each job names an environment. A GitHub environment holds its own variables and secrets, so DATABRICKS_HOST and the service principal credentials can differ between staging and production. The same variable names resolve to different values depending on the job's environment.
Environments also support required reviewers. If you add reviewers to production, GitHub pauses deploy-prod until one of them approves. That pause is your manual approval gate, and it needs no extra workflow code.
Details that cause failures
- Deploy the same commit. Both jobs check out the commit that triggered the run. Production receives exactly the code that passed staging, not a newer
main. - Use separate service principals. The staging principal should have no access to production. In the
prodtarget,run_assets the identity that runs the jobs. The deploying principal still needs permission to manage the bundle in the production workspace. - Pin the CLI.
databricks/setup-cli@maintracks the latest release. For reproducible deployments, pin a release tag instead. - Prevent overlapping runs. Two quick merges can deploy at the same time. Add a
concurrencygroup to the workflow so runs queue instead of racing. - Check test data. The integration job should read and write only staging data. Confirm that the staging target points at staging catalogs.
Exercise
A merge to main deploys to staging, but the integration job fails. What happens to the deploy-prod job, and why?
Show answer
It does not run. deploy-prod declares needs: deploy-staging, and a failed step in that job fails the whole job. The failing step is databricks bundle run, which exits non-zero when the Databricks job fails. Production keeps its previous deployment.
Exercise
Why does the workflow set environment: production on the deploy-prod job?
Show answer
It makes the job use the production environment's own secrets and variables. It also lets GitHub require reviewer approval before the job starts.