CI/CD với Databricks Asset Bundles
Cấu trúc bundle.yml chi tiết, targets nhiều môi trường, testing code Spark, quy trình promote dev→staging→production.
Một pipeline chỉ đáng tin cậy khi thay đổi được kiểm thử và triển khai có kiểm soát — giống hệt nguyên tắc CI/CD đã áp dụng cho code Laravel/PHP trước đây. Databricks Asset Bundles là công cụ chính thức biến toàn bộ tài nguyên Databricks (job, pipeline, cluster config) thành code quản lý được bằng Git, review được qua Pull Request, và triển khai tự động qua CI/CD pipeline.
🎯 Mục tiêu học tập
- Hiểu cấu trúc file bundle.yml và cách định nghĩa nhiều môi trường (targets)
- Viết test cho logic transform Spark bằng pytest
- Xây dựng quy trình promote thay đổi từ dev → staging → production có kiểm soát
- Tích hợp Databricks Asset Bundles vào GitHub Actions
Cấu trúc bundle.yml & Targets
# databricks.yml
bundle:
name: orders_lakehouse
targets:
dev:
mode: development
workspace:
host: https://dev-workspace.databricks.com
variables:
catalog: dev
prod:
mode: production
workspace:
host: https://prod-workspace.databricks.com
variables:
catalog: prod
run_as:
service_principal_name: etl-prod-sp
resources:
jobs:
daily_orders_pipeline:
name: "Daily Orders Pipeline (${bundle.target})"
tasks:
- task_key: ingest
notebook_task:
notebook_path: ./notebooks/ingest_orders.py
base_parameters:
catalog: ${var.catalog}
Targets cho phép cùng một định nghĩa job chạy khác nhau theo môi trường (khác
workspace, khác catalog Unity Catalog, khác cluster size) chỉ bằng cách đổi biến — không phải viết lại
cấu hình cho từng môi trường. mode: development tự động thêm prefix vào tên resource để nhiều
dev không đè lên nhau khi cùng deploy thử lên 1 workspace dev chung.
Deploy bằng CLI
# Deploy lên môi trường dev để test
databricks bundle deploy --target dev
# Chạy thử job vừa deploy
databricks bundle run daily_orders_pipeline --target dev
# Sau khi review & approve, deploy lên production
databricks bundle deploy --target prod
Testing logic Spark bằng pytest
Tách logic biến đổi dữ liệu thành hàm Python thuần (nhận vào DataFrame, trả về DataFrame) thay vì viết trực tiếp trong notebook — cho phép unit test bằng pytest với một SparkSession local nhỏ, không cần cluster thật:
# transforms.py
def clean_orders(df):
return df.filter(F.col("amount") > 0).dropDuplicates(["order_id"])
# test_transforms.py
def test_clean_orders_removes_negative_amount(spark_session):
input_df = spark_session.createDataFrame(
[(1, 100.0), (2, -50.0)], ["order_id", "amount"])
result = clean_orders(input_df)
assert result.count() == 1
Chạy bộ test này trong CI (GitHub Actions) trước khi cho phép bundle deploy
— áp dụng đúng nguyên tắc "test là cổng chặn deploy" đã học ở Module 13 khoá AI Engineer (ở đó là
regression eval cho AI, ở đây là unit test cho logic transform dữ liệu).
Quy trình Promote dev → staging → production
# .github/workflows/deploy.yml (rút gọn)
name: Deploy Databricks Bundle
on:
push:
branches: [main]
pull_request:
jobs:
test-and-deploy-dev:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pip install pytest pyspark
- run: pytest tests/
- uses: databricks/setup-cli@main
- run: databricks bundle deploy --target dev
if: github.event_name == 'pull_request'
deploy-prod:
needs: test-and-deploy-dev
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: databricks/setup-cli@main
- run: databricks bundle deploy --target prod
Mẫu chuẩn: Pull Request tự động test + deploy thử lên dev để reviewer kiểm tra; merge vào
main mới kích hoạt deploy prod — không bao giờ deploy thẳng lên production từ
máy cá nhân.
🏋️ Bài tập thực hành
Chuyển pipeline đã xây ở Module 6-7 thành Databricks Asset Bundle với 2 target (dev/prod). Viết ít nhất 3 unit test cho logic transform bằng pytest. Thiết lập GitHub Actions chạy test + deploy dev khi có Pull Request, deploy prod khi merge vào main. Thử tạo 1 PR có test fail để xác nhận pipeline CI chặn đúng cách.
📚 Tài nguyên học tập
-
Databricks — Asset Bundles: DevelopTài liệu chi tiết Databricks Asset BundlesDocs
-
Databricks — CI/CD with Asset BundlesHướng dẫn CI/CD chính thức với DABDocs
-
Databricks — Bundle settings referenceTham chiếu đầy đủ cấu hình bundle.ymlDocs
✅ Tự đánh giá hoàn thành
- Viết được bundle.yml với ít nhất 2 target (dev/prod)
- Deploy và chạy thử job qua Databricks CLI
- Viết được unit test cho logic transform bằng pytest
- Xây được GitHub Actions pipeline test → deploy dev → deploy prod có kiểm soát