CI/CD on Databricks using Azure DevOps

Chalach Monkhontirapat
Equinox
Published in
7 min readFeb 26, 2024

Continuous Integration(CI) and Continuous Delivery(CD) on Azure Databricks using Azure DevOps

บนความนี้จะมาพาทำ CI/CD และ Unittest Pipeline สำหรับ Databricks กันน

#️⃣ CI/CD on Databricks using Azure DevOps

1️⃣ Setting Azure DevOps and Databricks Workspace

2️⃣ Implement Project for CI/CD Demo

3️⃣ Implement Continuous Integration(CI)

4️⃣ Implement Continuous Delivery(CD) for Dev Environment

5️⃣ Implement Continuous Delivery(CD) with Unittesting

6️⃣ Implement Continuous Delivery(CD) for Uat and Prod Environment

Continuous Integration(CI) and Continuous Delivery(CD) on Azure Databricks using Azure DevOps

1️⃣ Setting Azure DevOps and Databricks Workspace

เราจะเริ่มจากการ Setting Azure DevOps Repo ก่อนแล้วทำการ Clone Repo ไปที่ Databricks Workspace เริ่มจากสร้าง Project ภายใต้ Azure Organization อย่างในรูปก็จะชื่อ Project cicd-databricks-blog

Create Project Repository on Azure DevOps Organization

จากนั้นทำการกดปุ่ม Clone มุมขวาแล้ว Copy HTTPS Url ของ Repository เพื่อเอาไป Clone บน Databricks

Copy HTTPS Url Repository

จากนั้นไปที่ Databricks Workspace ไปที่ Repos กด Add Repo และนำ Url ที่ Copy ไว้ก่อนหน้านี้มาวางตามรูป

Clone Project from Azure DevOps Repository to Databricks Repo

จากนั้นกด Create Repo เราก็จะได้ Repo มาดังรูป พร้อม Branch main เป็น Branch default

ต่อไปเราจะทำการ Setup Project Structure สำหรับ Blog นี้กัน โดย Target ของเราสุดท้าย เราจะต้องการให้ Azure DevOps Pipeline ของเรา Deploy เข้าไปที่ Workspace Uat และ Prod ตาม Structure ด้านล่างนี้

Workspace/
├─ dev/
│ ├─ cicd-databricks-blog/
│ │ ├─ utils/
│ │ │ ├─ numbers.py
│ │ ├─ tests/
│ │ │ ├─ test-utils.py
│ │ ├─ src/
│ │ │ ├─ main.py
├─ uat/
│ ├─ cicd-databricks-blog/
│ │ ├─ utils/
│ │ │ ├─ numbers.py
│ │ ├─ tests/
│ │ │ ├─ test-utils.py
│ │ ├─ src/
│ │ │ ├─ main.py
├─ prod/
│ ├─ cicd-databricks-blog/
│ │ ├─ utils/
│ │ │ ├─ numbers.py
│ │ ├─ tests/
│ │ │ ├─ test-utils.py
│ │ ├─ src/
│ │ │ ├─ main.py

ไปที่ Databricks แล้วทำการสร้าง Folder และ Files ตาม Structure แล้วทำการ Commit ไปที่ Azure Repo

Initial Project for CI/CD

2️⃣ Implement Project for CI/CD Demo

Target ของหัวข้อนี้คือ เราจะทำการ Implement 3 files ดังนี้

  • utils/numbers.py : Implement function ในการ add numbers
  • tests/test-utils.py : Implement unittest ของ numbers.py
  • src/main.py : Implement การ add number โดยการเรียก function จาก utils

Implement add function

ทำการ Implement function บวกเลขง่ายๆ ตามรูปด้านล่าง ที่ utils/numbers.py

add numbers function

Implement unittest

ทำการ Implement unittest ของ utils numbers โดยเริ่มจาก

  • import utils
  • import unittest
  • implement test case
unittest numbers utils

Implement main function

ทำการ Implement main function ที่เรียกใช้ add function จาก numbers utils

  • import utils
  • implement main function
main function

3️⃣ Implement Continuous Integration(CI)

เริ่ม Implement CI Pipeline กัน โดยการทำ CI บน Databricks จะไม่เหมือนกับเวลาที่เราทำกับ App ทั่วไปที่เราจะเอา Unittest และ Build มาไว้ในนี้ ตรงส่วนของ CI จะไม่สามารถ Run unittest ได้ตรงๆ เพราะว่าตรงนี้เรายังไม่ได้เอา Source code ของเราเข้าไปไว้บน Workspace ของ Databricks เราทำอยู่บน Databricks Repo ดังนั้นในขา CI Pipeline เราจึงทำได้แค่ Build แล้วเอา Source code ของเราไปวางไว้ที่ Workspace

Target : Implement CI เพื่อ Build source code ของเรา

ไปที่ Azure DevOps Pipeline จากนั้น Ctrate New Pipeline ด้วย Azure Repo ของเรา จากนั้น Configure Pipeline ด้วย Starter pipeline เราจะได้ file azure-pipeline.yml มาที่ repo ของเรา

Create Azure Pipeline for Continuous Integration(CI)

หลังจากได้ file starter azure-pipeline.yml มาแล้วให้ลอง save and run ก็จะได้ตามนี้

azure-pipeline.yml — starter file
run Azure Pipeline with azure-pipeline.yml

มาถึงตรงนี้เราก็จะได้ Pipeline ว่างๆ ที่พร้อมใช้งานเรียบร้อยแล้ว หลังจากนี้เราต้องการเพิ่มส่วนของ Build source code ของเราเป็น Artifact โดยให้ทำการ Edit pipline จากนั้นทำการ Add task Publish Pipeline Artifacts เข้าไป

Add task Publish Pipeline Artifacts to pipeline

โดยการใส่ Parameter ตามรูปด้านล่างนี้

  • File path : เราจะเก็บไว้ที่ Pipeline Workspce
  • Artifacte name : ชื่อของ Build source code เรา เราจะตั้งว่า Notebook
  • Publish location : เราจะ Publish artifact ไปไว้ที่ไหน
azure-pipeline.yml — add publish pipeline artifact

จากนั้นทำการลอง Save and run pipeline ดู

Run pipeline success

จากนั้นหากเรากลับไปดูที่หน้า Pipeline Status เราก็จะเห็นว่า Pipeline Success และมีการ Publish Artifact ออกมา 1 ชุด

Pipeline status success and show publish artifact

ลอง Click เข้าไปที่ Published เราก็จะเห็นของที่ถูก Build มาจาก Pipeline ตามนี้

Published artifact

เท่านี้ก็จะเป็นการเสร็จในการทำ Continuous Integration Pipeline เรียบร้อยแล้ว

4️⃣ Implement Continuous Delivery(CD) for Dev Environment

หลังจาก Implement CI Pipeline ไป เราจะมาทำการ Implement CD Pipeline ต่อโดย

Target : Implement CD เพื่อนำ Artifact จาก CI Pipeline ไป Deploy ที่ Dev Environment

เริ่มต้นที่ Pipeline แล้วไปที่ Menu Releases จากนั้นกดที่ Add an artifact จะมีหน้าต่างขึ้นมาด้านขวามือ ให้เราเลือก Build อันแรก จะเป็นการเลือก Build source code จาก Build pipeline ที่เราทำการ Publish มาก่อนใน Step ก่อนหน้านี้

Setup release pipeline with artifact from pipeline workspace

จากนั้นให้เราทำการ Add stage แล้วเลือก Empty job เพราะเราจะทำการ Add task ของแต่ละ Stage ขึ้นมาเอง

Add Dev stage for release pipeline

หลังจากเรา Add Dev stage เพื่อทำการ Deploy artifact ของเราไปที่ Dev env แล้ว เราต้องทำการ Add task ของ stage เพื่อบอกว่าเราต้องการจะให้ Stage นี้ทำอะไรบ้าง

  • Task 1 : Config Databricks CLI เพื่อให้ Pipeline รู้ว่าจะไป Deploy ที่ Databricks workspace URL ไหน ด้วยสิทธิ์อะไร
  • Task 2 : Deploy Databricks Notebook เป็นการ Download and build artifact ไปที่ Path ที่เรากำหนด

เริ่มที่ Task 1 ของ Dev stage

ทำการ Add task Config Databricks CLI จากนั้น Config Workspace URL ของเรา กับ Token โดย Token นี้สามารถใช้ได้ 2 แบบ คือ Personal Access Token(PAT) กับ Service Principal โดยเรา Service Principal ไป Authen ก่อนแล้วค่อยเอา Token มากรอก แต่ ในบทความนี้จะใช้ PAT

Get Personal Access Token : https://docs.databricks.com/en/dev-tools/auth/pat.html

Get Service Pricipal Token : https://jiasli.github.io/azure-notes/aad/Service-Principal-get-token-with-curl.html

Task 1 : Config Databricks CLI

ต่อที่ Task 2 ของ Dev stage

ทำการ Set source ที่จะใช้ในการเอาไป Build ที่ Target path บน Databricks workspcae โดยต้องเลือก Source จาก Artifact ของเรา จากนั้นกำหนด Region ที่เราใช้ ต่อด้วยกำหนด Path ในตัวอย่างนี้จะให้ Deploy ไปที่ demo/dev แล้วทำการเลือก Option : Clean Workspace Folder เพื่อที่จะ Clear path ทุกครั้งที่เราทำการ Deploy

Task 2: Deploy Databricks Notebook

เท่านี้เราก็ทำการ Add task ใน Dev stage ของ Release pipeline เสร็จเรียบร้อยแล้ว ลองทำการ Run ดูก็จะได้ผลลัพธ์ตามภาพด้านล่างนี้

Run reluease pipeline Dev stage success

ถ้าเราไปดูที่ Databricks workspce ตาม Path ที่เราทำการ Config ไว้ ก็จะเห็นว่า Source code จากใน Azure Repo brach main ที่เราทำการ Build artifact จาก CI Pipeline ได้ถูกเอามาวางไว้ที่ demo/dev หรือ Dev environment ของเราผ่าน CD Pipeline or Release pipline เรียบร้อยแล้ว

Deploy source code to dev env success

5️⃣ Implement Continuous Delivery(CD) with Unittest

หลังจาก Implement CD Pipeline ไป เราจะมาทำการ Implement CD Pipeline ต่อโดย

Target : Implement CD เพื่อ Run unittesting ก่อนที่จะทำการ Deploy ไปที่ Uat และ Prod

Add unittest stage for release pipeline

ทำการ Add Unittest stage เหมือนกับการ Add Dev stage ใน step ก่อนหน้านี้จากนั้นเราต้องทำการ Add task ของ stage เพื่อบอกว่าเราต้องการจะให้ Stage นี้ทำอะไรบ้าง

  • Task 1 : Config Databricks CLI เพื่อให้ Pipeline รู้ว่าจะไป Deploy ที่ Databricks workspace URL ไหน ด้วยสิทธิ์อะไร
  • Task 2 : Excute unittest เพื่อดูว่า Source code ที่เรา build มานั้นมีอะไรผิดพลาดและผ่าน Unittest ที่เขียนมามั้ย

Task 1 ของ Unittest stage จะเหมือนกับ Dev stage เราสามารถทำเหมือนกันได้ทุกอย่าง

Task 2 ของ Unittest stage เราจะทำการ Add task execute unittest และกำหนด Notebook path ที่เราจะ Run เพื่อ Excute unittest และทำการกำหนด Existing cluster ที่จะใช้ Run โดยการเข้าไปที่หน้า Databricks -> Compute -> Cluster ที่ต้องการ -> Json(อยู่มุมบนขวา) จากนั้นทำการ Copy cluster id มาเพื่อทำการ Config ดังรูปข้างล่างนี้

Task 2: Execute Unittest

จากนั้นทำการ Run release pipeline ที่ทำการเพิ่ม Unittest ไปแล้ว

Run reluease pipeline Dev stage and Unittest stage success

โดยตัว Pipeline จะไปทำการสร้าง Databricks Workflow และทำการ Execute Notebook ตาม Path ที่เราได้ทำการ Config ไว้ใน Unittest stage\

Unittest stage create Databricks workflow for execute unittesting notebook

พอเรากดเข้าไปดูใน Databricks Workflow ก็จะเห็น Unittest Notebook ที่เราเขียนไว้ถูก Run success แล้ว

Databricks Workflow run unittest notebook success

6️⃣ Implement Continuous Delivery(CD) for Uat and Prod Environment

หลังจาก Implement CD Pipeline ในการ Deploy to Dev env และ ทำการ Unittest ไป เราจะมาทำการ Implement CD Pipeline ต่อโดย

Target : Implement CD เพื่อ Deploy to Uat and Prod environment

Add Uat and Prod stage for release pipeline

ทำการ Add Uat และ Prod stage เหมือนกับการ Add Dev stage ใน step ก่อนหน้านี้ จากนั้นทำการแก้ Config แค่ Target path ตอน Deploy โดยที่

Uat stage -> target path -> demo/uat

Prod stage -> target path -> demo/prod

Change target path for Uat and Prod

จากนั้นลอง Run release pipeline เมื่อ Pipline success แล้วเราก็จะเห็น Source code ของเรา Deploy แบบ E2E ตั้งแต่ Build -> Dev -> Uniitest -> Uat -> Prod

E2E pipeline deployment

--

--