Sub topic 12
Building the CI workflow
This chapter assumes you can write pytest unit tests for PySpark code, have a Databricks Asset Bundle with a staging target, and know how GitHub Actions workflows and pull requests work. Authentication with a service principal is covered in the previous chapter. Here you assemble the pieces into a workflow.
What CI should check
Continuous integration (CI) runs automatically on every pull request. It should answer three questions before anyone reviews the change:
- Is the code clean? A linter catches style problems and obvious bugs.
- Does the logic work? Unit tests run on a local SparkSession and need no workspace.
- Is the bundle valid?
databricks bundle validatechecks the configuration against the target workspace.
The jobs run in order, from cheapest to most expensive. A failing lint step stops the pipeline before it uses a workspace.
The workflow file
Save this as .github/workflows/ci.yml. The test job installs development dependencies, lints with ruff, and runs the unit tests. The validate job starts only if test passes.
name: ci
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install -r requirements-dev.txt
- run: ruff check src tests
- run: pytest tests/unit
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 stagingrequirements-dev.txt must list pytest, pyspark, and ruff. It must not list databricks-connect. The two packages conflict, and CI unit tests use the local SparkSession from your fixture.
How the pieces fit
needs: testmakesvalidatewait fortest. If tests fail, the validate job is skipped.- Environment variables supply the credentials. The Databricks CLI reads
DATABRICKS_HOST,DATABRICKS_CLIENT_ID, andDATABRICKS_CLIENT_SECRETautomatically, so no login step is needed. varsandsecretsare different stores. The host URL is not sensitive, so it goes invars. The client secret goes insecrets, where GitHub masks it in logs.-t stagingvalidates against a real target. Validation resolves variables and checks resource definitions, so it catches errors that YAML syntax checks miss.
Common errors
| Symptom | Likely cause |
|---|---|
| `ModuleNotFoundError` for your package in CI | The package is not installed or on the path. Add `pip install -e .` or set `pythonpath` in `pytest.ini`. |
| Spark tests hang or fail to start | The runner has no Java. Add `actions/setup-java` with Java 17. |
| `validate` reports an authentication error | A secret name does not match, or the service principal has no access to the workspace. |
| Validate passes locally and fails in CI | The CLI versions differ. Pin `setup-cli` to a release tag instead of `@main`. |
Exercise
The validate job passes on a branch you pushed but fails with an authentication error on a pull request from a contributor's fork. Why, and how do you fix it?
Show answer
GitHub does not pass repository secrets to workflows triggered by fork pull requests, so DATABRICKSCLIENTID and DATABRICKSCLIENTSECRET are empty. Skip the validate job for forks, or have a maintainer push the change to a branch in the main repository so the workflow runs with secrets.