iT邦幫忙

1

CI/CD for SQL Database Project

資料庫專案開發跟程式開發一樣屬於專案開發流程的一環, 這篇文件將介紹如何使用微軟 DevOps/TFS 工具, 實作資料庫專案開發的 CI/CD 應用. 實務上 SQL database project 很少直接做 CD, 需考慮較多的因素與不同的資料庫 Continuous Delivery 的設計考量.

1. 環境準備

1.1 DevOps 環境

CI/CD 可以使用各種 DevOps 平台, 本文件將使用 Microsoft 的 DevOps/TFS 作為平台. 若使用 Team Foundation Server, 建議使用 2017 版本以上.

1.2 開發環境

資料庫專案開發需要準備 Visual Studio SSDT 開發環境, 建議使用 VS 2015 以上版本, 本文件使用 VS 2017 並安裝 SSDT 元件

1.3 SQL Server 環境

資料庫開發專案, 需要部署到 SQL Server 上, 本文件使用 SQL Server 2016 為資料庫環境

2. 資料庫專案建立

2.1 建立資料庫專案

(1) 建立 DevOps 專案

a. 在 DevOps/TFS server 上建立新的 team project, 可以選擇 git 或 TFVC 作為 version control 類型

https://ithelp.ithome.com.tw/upload/images/20190326/20111871wi2j9bYtyb.png

b. 建立完成後, 可以直接 clone 到 Visual Studio 中

https://ithelp.ithome.com.tw/upload/images/20190326/20111871bebAiSP7Zb.png

c. 確認從 Visual Studio 開啟

https://ithelp.ithome.com.tw/upload/images/20190326/20111871J1BGwGfZNt.png

https://ithelp.ithome.com.tw/upload/images/20190326/20111871f7LXYrChbI.png

(2) 建立 Visual Studio SQL Server 專案

Temp Explorer > Solutions > New 建立新的 SQL Server Database Project

https://ithelp.ithome.com.tw/upload/images/20190326/20111871MdyNuqKO4S.png

2.2 匯入資料庫

a. 在專案上按右鍵, 選取 Import > Database

https://ithelp.ithome.com.tw/upload/images/20190326/20111871a4WBXiwstC.png

b. 選取 Select Connection, 並且將 ** Import Settings** 先取消, Maximum files per folder 設定為 100, 避免匯入過多的物件

https://ithelp.ithome.com.tw/upload/images/20190326/201118718WqoHxMtO3.png

c. 設定連線資訊, 選擇 Server Name 以及 Database Name

https://ithelp.ithome.com.tw/upload/images/20190326/20111871TiXlrK2CBN.png

d. 匯入完成後, 專案產生相對應的資料庫物件

https://ithelp.ithome.com.tw/upload/images/20190326/20111871ko9cgMs0pj.png

https://ithelp.ithome.com.tw/upload/images/20190326/20111871fllBkmzGSi.png

2.3 產生 Profile

a. 在專案上按右鍵, 選取 Publish

https://ithelp.ithome.com.tw/upload/images/20190326/201118719gPveYZ69q.png

b. 選取 Edit, 選取連線資訊, 再 Save Profile As

https://ithelp.ithome.com.tw/upload/images/20190326/20111871qp8hp5lsmg.png

c. 將 Profile 檔案儲存於專案下

https://ithelp.ithome.com.tw/upload/images/20190326/20111871DZ4AnbQzVq.png

2.4 Commit source code

將 Source code commit, 請記得將 Database1.publish.xml 也 commit 進去

https://ithelp.ithome.com.tw/upload/images/20190326/20111871TL3tLQHmHI.png

3. 建立 CI Build definition

3.1 建立 Build definition

a. 開啟 DevOps/TFS, 進入專案, 選取 Build & Release, 選取 New definition

https://ithelp.ithome.com.tw/upload/images/20190326/20111871iIqjFCBsyd.png

b. 選取 .NET Desktop template, 如果沒有可以選擇相關的 .NET template, 也可以從 Empty template 建立起

https://ithelp.ithome.com.tw/upload/images/20190326/201118719rnoOw1KrM.png

3.2 建立 Tasks

a. 先移除不需要的 tasks (VsTest, Publish symbols), 按右鍵選取 Remove selected task(s)

https://ithelp.ithome.com.tw/upload/images/20190326/20111871jTgOpJqSvx.png

b. 在 Copy Files to: task, Contents 參數中加入 **\Database1.publish.xml, 目的將 Database1.Publish.xml 檔案複製到 drop 目錄

https://ithelp.ithome.com.tw/upload/images/20190326/201118719AfAHEdmd9.png

3.3 啟用 CI

選取 Trigger 標籤, 啟用 Continuous Integration, 設定值使用預設值

https://ithelp.ithome.com.tw/upload/images/20190326/20111871hJzZ6Gc8xK.png

3.4 測試 Build

a. 儲存並開始 Queue

https://ithelp.ithome.com.tw/upload/images/20190326/20111871eJZOWVyRp3.png

b. 開始 Queue

https://ithelp.ithome.com.tw/upload/images/20190326/20111871Tb1eZYsbAH.png

c. 確認 Build 結果

https://ithelp.ithome.com.tw/upload/images/20190326/20111871SBWs8KoFTT.png

4. 建立 CD Release definition

4.1 建立 Release definition

a. 選取 Build & Release, 選取 Releases 標籤, 點選 New definition

https://ithelp.ithome.com.tw/upload/images/20190326/20111871N2gOjmHyDO.png

b. 先選取空白 template

https://ithelp.ithome.com.tw/upload/images/20190326/2011187161rYcP7gWa.png

c. 選取 Build 成品來源, 並啟用 Continuous deployment

https://ithelp.ithome.com.tw/upload/images/20190326/20111871RtNkkkN5m8.png

4.2 建立 Tasks

a. 加入一個遮罩的變數儲存密碼, 選取 Variables 標籤, 新增一個 pwd 名稱變數儲存密碼, 並記得將該變數設定為 鎖定

https://ithelp.ithome.com.tw/upload/images/20190326/20111871mSthc7jVff.png

b. 選取 Add Task

https://ithelp.ithome.com.tw/upload/images/20190326/20111871jao0tNL22l.png

b. 加入一個 Windows Machine File Copy task 來將成品複製到 SQL Server 上

https://ithelp.ithome.com.tw/upload/images/20190326/20111871A3DQ6DaSlt.png

c. 在 Source 上選取成品目錄, 輸入 SQL Server 名稱, 帳號密碼資訊, 其中密碼可以使用已建立的變數

https://ithelp.ithome.com.tw/upload/images/20190326/20111871VXKsdBqSV5.png

d. 再新增一個 Task, 選取 WinRM - SQL Server DB Deployment Task, 如果找不到, 可以到 Market place 上搜尋 WinRM IIS deployment 進行安裝

https://ithelp.ithome.com.tw/upload/images/20190326/20111871PZC8dk8LEc.png

e. 輸入以下的參數:

(a) Machines: SQL Server 名稱

(b) Admin Login: 管理者帳號

(c) Password: 使用變數 pwd

(4) Protocol: 使用 HTTP (TCP Port: 5985)

(6) DACPAC file: SQL Server 上在前一步驟複製過來的位置 (c:\drop\bin\Release\Database1.dacpac)

(7) Database Name: 資料庫名稱

(8) Publish profile 檔案: 前一步驟複製過來的位置 (c:\drop\Database1.publish.xml)

https://ithelp.ithome.com.tw/upload/images/20190326/20111871OHqt74zBjY.png

4.3 測試 Release

a. 將 Release definition 儲存後並 release

https://ithelp.ithome.com.tw/upload/images/20190326/20111871vgNPFMQMW2.png

b. 執行結果

https://ithelp.ithome.com.tw/upload/images/20190326/20111871f64FrX15nH.png

5. 驗證 CI/CD

5.1 修改 Source

在 Visual Studio 專案中修改 Stored Procedure

https://ithelp.ithome.com.tw/upload/images/20190326/20111871wxK2Z5pVnd.png

5.2 Commit changes

Commit and sync to team project

https://ithelp.ithome.com.tw/upload/images/20190326/20111871hIq3QCNR56.png

5.3 驗證 CI

CI 將自動被 Trigger

https://ithelp.ithome.com.tw/upload/images/20190326/201118714TrDcoiJdt.png

5.4 驗證 CD

CD 也根據 CI 完成後自動被 Trgger

https://ithelp.ithome.com.tw/upload/images/20190326/20111871ZsmPK2vC0Q.png


Reference


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言