curl -fsSL https://raw.githubusercontent.com/databricks/setup-cli/main/install.sh | sh
若要將 Bash 指令碼工作新增至管線,請參閱步驟 3.6. 安裝 Databricks CLI 和 Python Wheel 組建工具。
設定流程以便已安裝的 Databricks CLI 能夠認證您的服務主體與您的工作區進行驗證。 若要這樣做,請參閱步驟 3.1:定義發行管線的環境變數。
視需要將更多 Bash 指令碼工作新增至管線,以執行 Databricks CLI 命令。 請參閱 Databricks CLI 命令。
若要使用本文的範例,您必須具備:
現有的 Azure DevOps 專案。 如果您還沒有專案,請在 Azure DevOps 中建立專案。
具有 Azure DevOps 支援的 Git 供應商的現有存放庫。 您會將 Python 範例程式碼、範例 Python 筆記本,以及相關的版本設定檔案新增至此存放庫。 如果您還沒有存放庫,請遵循 Git 提供者的指示建立一個存放庫。 然後,將您的 Azure DevOps 專案連結到此存放庫(如果您尚未這樣做)。 如需指示,請遵循支援的來源存放庫中的連結。
本文的範例會使用 OAuth 計算機對計算機 (M2M) 驗證,向 Azure Databricks 工作區驗證Microsoft Entra ID 服務主體。 您必須擁有一個包含 Azure Databricks OAuth 機密的 Microsoft Entra ID 服務主體。 請參閱 使用 OAuth,授權服務主體以無人值守方式存取 Azure Databricks 資源。
步驟 1:將範例的檔案新增至您的存放庫
在此步驟中,您會將本篇文章中的所有範例檔案新增至您第三方 Git 提供者的存放庫,並由您的 Azure DevOps 管線在遠端 Azure Databricks 工作區上進行建置、部署及執行。
步驟 1.1:新增 Python Wheel 元件檔案
在本文中的範例中,您的 Azure DevOps 管線會建置和單元測試 Python 轉輪檔案。 Azure Databricks Notebook 接著會呼叫建置的 Python 轉輪檔案功能。
若要定義筆記本執行所針對的 Python Wheel 檔案的邏輯和單元測試,請在存放庫根目錄中建立名為 addcol.py
和 test_addcol.py
的兩個檔案,並將其新增至 python/dabdemo/dabdemo
資料夾中名為 Libraries
的資料夾結構,如下所示:
└── Libraries
└── python
└── dabdemo
└── dabdemo
├── addcol.py
└── test_addcol.py
檔案 addcol.py
包含程式庫函式,稍後會組建至 Python Wheel 檔案,然後安裝在 Azure Databricks 叢集上。 這是一個簡單的函式,可將填入常值的新欄位新增至 Apache Spark DataFrame。
# Filename: addcol.py
import pyspark.sql.functions as F
def with_status(df):
return df.withColumn("status", F.lit("checked"))
檔案 test_addcol.py
包含測試,以將模擬 DataFrame 物件傳遞至 with_status
中定義的 addcol.py
函式。 然後,結果會與包含預期值的 DataFrame 對象進行比較。 如果值相符,測試會通過:
# Filename: test_addcol.py
import pytest
from pyspark.sql import SparkSession
from dabdemo.addcol import *
class TestAppendCol(object):
def test_with_status(self):
spark = SparkSession.builder.getOrCreate()
source_data = [
("paula", "white", "paula.white@example.com"),
("john", "baer", "john.baer@example.com")
source_df = spark.createDataFrame(
source_data,
["first_name", "last_name", "email"]
actual_df = with_status(source_df)
expected_data = [
("paula", "white", "paula.white@example.com", "checked"),
("john", "baer", "john.baer@example.com", "checked")
expected_df = spark.createDataFrame(
expected_data,
["first_name", "last_name", "email", "status"]
assert(expected_df.collect() == actual_df.collect())
若要讓 Databricks CLI 正確地將此程式庫程式碼封裝到 Python Wheel 檔案中,請在與上述兩個檔案相同的資料夾中建立名為 __init__.py
和 __main__.py
的兩個檔案。 此外,在 setup.py
資料夾中建立名為 python/dabdemo
的檔案,如下所示:
└── Libraries
└── python
└── dabdemo
├── dabdemo
│ ├── __init__.py
│ ├── __main__.py
│ ├── addcol.py
│ └── test_addcol.py
└── setup.py
檔案 __init__.py
包含連結庫的版本號碼和作者。 以您的名稱取代 <my-author-name>
:
# Filename: __init__.py
__version__ = '0.0.1'
__author__ = '<my-author-name>'
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
檔案 __main__.py
包含函式庫的進入點:
# Filename: __main__.py
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
from addcol import *
def main():
if __name__ == "__main__":
main()
檔案 setup.py
包含將程式庫組建至 Python Wheel 檔案的其他設定。 以有效值取代 <my-url>
、<my-author-name>@<my-organization>
和 <my-package-description>
:
# Filename: setup.py
from setuptools import setup, find_packages
import dabdemo
setup(
name = "dabdemo",
version = dabdemo.__version__,
author = dabdemo.__author__,
url = "https://<my-url>",
author_email = "<my-author-name>@<my-organization>",
description = "<my-package-description>",
packages = find_packages(include = ["dabdemo"]),
entry_points={"group_1": "run=dabdemo.__main__:main"},
install_requires = ["setuptools"]
步驟 1.2:新增 Python Wheel 檔案的單元測試筆記本
稍後,Databricks CLI 會執行筆記本作業。 此作業會執行具有檔案名 run_unit_tests.py
的 Python 筆記本。 此筆記本會針對 Python wheel 函式庫的邏輯執行 pytest
。
若要執行本文範例的單元測試,請在存放庫的根目錄新增一個名為 run_unit_tests.py
的筆記本檔案,其中包含以下內容:
# Databricks notebook source
# COMMAND ----------
# MAGIC %sh
# MAGIC
# MAGIC mkdir -p "/Workspace${WORKSPACEBUNDLEPATH}/Validation/reports/junit/test-reports"
# COMMAND ----------
# Prepare to run pytest.
import sys, pytest, os
# Skip writing pyc files on a readonly filesystem.
sys.dont_write_bytecode = True
# Run pytest.
retcode = pytest.main(["--junit-xml", f"/Workspace{os.getenv('WORKSPACEBUNDLEPATH')}/Validation/reports/junit/test-reports/TEST-libout.xml",
f"/Workspace{os.getenv('WORKSPACEBUNDLEPATH')}/files/Libraries/python/dabdemo/dabdemo/"])
# Fail the cell execution if there are any test failures.
assert retcode == 0, "The pytest invocation failed. See the log for details."
步驟 1.3:新增呼叫 Python Wheel 檔案的筆記本
稍後,Databricks CLI 會執行另一個筆記本作業。 此筆記本會建立 DataFrame 物件、將它傳遞給 Python 滾輪連結庫的 with_status
函式、列印結果,以及報告作業的執行結果。 在根目錄建立帶有下列內容的名為 dabdemo_notebook.py
的筆記本檔案:
# Databricks notebook source
# COMMAND ----------
# Restart Python after installing the Python wheel.
dbutils.library.restartPython()
# COMMAND ----------
from dabdemo.addcol import with_status
df = (spark.createDataFrame(
schema = ["first_name", "last_name", "email"],
data = [
("paula", "white", "paula.white@example.com"),
("john", "baer", "john.baer@example.com")
new_df = with_status(df)
display(new_df)
# Expected output:
# +------------+-----------+-------------------------+---------+
# │ first_name │ last_name │ email │ status │
# +============+===========+=========================+=========+
# │ paula │ white │ paula.white@example.com │ checked │
# +------------+-----------+-------------------------+---------+
# │ john │ baer │ john.baer@example.com │ checked │
# +------------+-----------+-------------------------+---------+
步驟 1.4:建立套件組合設定
本文的範例使用 Databricks 資產套件組合來定義建置、部署和執行 Python 滾輪檔案、兩個筆記本和 Python 程式代碼檔案的設定和行為。 Databricks Asset Bundles,一般稱為套件組合,可讓您將完整的資料、分析和 ML 專案表示為來源檔案的集合。 請參閱什麼是 Databricks Asset Bundles?。
若要設定本文範例的套件組合,請在存放庫的根目錄中建立名為 databricks.yml
的檔案。 在此範例 databricks.yml
檔案中,取代下列預留位置:
將 <bundle-name>
取代為套件組合的唯一程式設計名稱。 例如: azure-devops-demo
。
將 <job-prefix-name>
取代為某個字串,以協助唯一識別在此範例的 Azure Databricks 工作區中建立的作業。 例如: azure-devops-demo
。
將 <spark-version-id>
取代為作業叢集的 Databricks Runtime 版本識別碼,例如 13.3.x-scala2.12
。
將 <cluster-node-type-id>
更換為作業叢集的叢集節點類型 ID,例如 Standard_DS3_v2
。
請注意,dev
對應中的 targets
指定主機和相關部署行為。 在真實世界的實作中,您可以在自己的套件組合中為此目標提供不同的名稱。
以下是此範例檔案 databricks.yml
的內容:
# Filename: databricks.yml
bundle:
name: <bundle-name>
variables:
job_prefix:
description: A unifying prefix for this bundle's job and task names.
default: <job-prefix-name>
spark_version:
description: The cluster's Spark version ID.
default: <spark-version-id>
node_type_id:
description: The cluster's node type ID.
default: <cluster-node-type-id>
artifacts:
dabdemo-wheel:
type: whl
path: ./Libraries/python/dabdemo
resources:
jobs:
run-unit-tests:
name: ${var.job_prefix}-run-unit-tests
tasks:
- task_key: ${var.job_prefix}-run-unit-tests-task
new_cluster:
spark_version: ${var.spark_version}
node_type_id: ${var.node_type_id}
num_workers: 1
spark_env_vars:
WORKSPACEBUNDLEPATH: ${workspace.root_path}
notebook_task:
notebook_path: ./run_unit_tests.py
source: WORKSPACE
libraries:
- pypi:
package: pytest
run-dabdemo-notebook:
name: ${var.job_prefix}-run-dabdemo-notebook
tasks:
- task_key: ${var.job_prefix}-run-dabdemo-notebook-task
new_cluster:
spark_version: ${var.spark_version}
node_type_id: ${var.node_type_id}
num_workers: 1
spark_env_vars:
WORKSPACEBUNDLEPATH: ${workspace.root_path}
notebook_task:
notebook_path: ./dabdemo_notebook.py
source: WORKSPACE
libraries:
- whl: '/Workspace${workspace.root_path}/files/Libraries/python/dabdemo/dist/dabdemo-0.0.1-py3-none-any.whl'
targets:
mode: development
如需檔案語法的詳細資訊 databricks.yml
,請參閱 Databricks Asset Bundle 組態。
步驟 2:定義組建管線
Azure DevOps 提供雲端託管的使用者介面,以使用 YAML 定義 CI/CD 管線的階段。 如需 Azure DevOps 和管線的詳細資訊,請參閱 Azure DevOps 文件。
在此步驟中,您會使用 YAML 標記來定義建置管線,以建置部署產物。 若要將程式代碼部署至 Azure Databricks 工作區,您可以將此管線的組建成品指定為發行管線的輸入。 您稍後會定義此發行管線。
為了執行組建管線,Azure DevOps 提供雲端託管的隨選執行代理程式,可支援部署至 Kubernetes、VM、Azure Functions、Azure Web Apps,以及更多目標。 在此範例中,您會使用隨選代理程式來自動組建部署成品。
定義本文的建置管線範例,如下所示:
登入 Azure DevOps,然後按一下 [登入] 連結以開啟您的 Azure DevOps 專案。
如果顯示的是 Azure 入口網站而不是您的 Azure DevOps 專案,請按一下 >[更多服務] > [Azure DevOps 組織] [我的 Azure DevOps 組織],然後開啟您的 Azure DevOps 專案。
在側邊欄中按一下 [管線],然後在 [管線] 功能表中按一下 [管線]。
按一下 [新增管線] 按鈕,並遵循畫面上的指示。 (如果您已經有管線,則請按一下建立管線。) 在這些指示結束時,管線編輯器隨即開啟。 在這裡,您會在出現的 azure-pipelines.yml
檔案中定義組建管線指令碼。 如果在指示結尾看不到管線編輯器,請選取組建管線的名稱,然後按兩下 [ 編輯]。
您可以使用 Git 分支選取器
來自訂 Git 存放庫中每個分支的組建流程。 CI/CD 最佳做法是不要直接在存放庫的 main
分支中執行生產工作。 這個範例假設要使用的存放庫中,而不是 release
中,存在名為 main
的分支。
azure-pipelines.yml
組建管線指令碼預設會儲存在您與管線相關聯的遠端 Git 存放庫根目錄中。
使用下列定義覆寫管線檔案 azure-pipelines.yml
的起始內容,然後按兩下 [ 儲存]。
# Specify the trigger event to start the build pipeline.
# In this case, new code merged into the release branch initiates a new build.
trigger:
- release
# Specify the operating system for the agent that runs on the Azure virtual
# machine for the build pipeline (known as the build agent). The virtual
# machine image in this example uses the Ubuntu 22.04 virtual machine
# image in the Azure Pipeline agent pool. See
# https://learn.microsoft.com/azure/devops/pipelines/agents/hosted#software
pool:
vmImage: ubuntu-22.04
# Download the files from the designated branch in the remote Git repository
# onto the build agent.
steps:
- checkout: self
persistCredentials: true
clean: true
# Generate the deployment artifact. To do this, the build agent gathers
# all the new or updated code to be given to the release pipeline,
# including the sample Python code, the Python notebooks,
# the Python wheel library component files, and the related Databricks asset
# bundle settings.
# Use git diff to flag files that were added in the most recent Git merge.
# Then add the files to be used by the release pipeline.
# The implementation in your pipeline will likely be different.
# The objective here is to add all files intended for the current release.
- script: |
git diff --name-only --diff-filter=AMR HEAD^1 HEAD | xargs -I '{}' cp --parents -r '{}' $(Build.BinariesDirectory)
mkdir -p $(Build.BinariesDirectory)/Libraries/python/dabdemo/dabdemo
cp $(Build.Repository.LocalPath)/Libraries/python/dabdemo/dabdemo/*.* $(Build.BinariesDirectory)/Libraries/python/dabdemo/dabdemo
cp $(Build.Repository.LocalPath)/Libraries/python/dabdemo/setup.py $(Build.BinariesDirectory)/Libraries/python/dabdemo
cp $(Build.Repository.LocalPath)/*.* $(Build.BinariesDirectory)
displayName: 'Get Changes'
# Create the deployment artifact and then publish it to the
# artifact repository.
- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(Build.BinariesDirectory)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true
- task: PublishBuildArtifacts@1
inputs:
ArtifactName: 'DatabricksBuild'
步驟 3:定義發行管線
發行管線會將組建成品從組建管線部署到 Azure Databricks 環境。 將此步驟中的發行管線與上述步驟中的組建管線分開,可讓您建立組建而不需要部署它,或同時從多個組建部署成品。
在您的 Azure DevOps 專案中,於側邊欄的 管道 功能表中,點擊 發行。
步驟 3.1:定義發行管線的環境變數
此範例的發行管線依賴下列環境變數,您可以在 [變數] 索引標籤的 [管線變數] 區段中按一下 [新增],並設定 [階段 1] 範圍:
BUNDLE_TARGET
,應該符合 target
檔案中的 databricks.yml
名稱。 在本文中的範例中,這是 dev
。
DATABRICKS_HOST
,表示 Azure Databricks 工作區的個別工作區 URL,以 https://
開頭,例如 https://adb-<workspace-id>.<random-number>.azuredatabricks.net
。 請勿在 /
之後包含後置 .net
。
DATABRICKS_CLIENT_ID
,表示 Microsoft Entra ID 服務主體的應用程式 ID。