本文將引導您如何與 GitHub Copilot 互動,以產生在 PostgreSQL 資料庫上執行 CRUD 作業的本機 Django Web 應用程式。 接下來,它會引導您如何與 GitHub Copilot for Azure 互動,將 Web 應用程式和資料庫部署至 Azure App Service 和 Azure PostgreSQL 彈性伺服器 (以及數個支援的 Azure 服務)。
您建立的特定應用程式是簡單的連絡人管理應用程式,具有具有清單詳細資料樣式架構的 CRUD 作業。
備註
使用大型語言模型 (LLM) 產生應用程式可能會產生不一致的結果。 您的結果取決於 LLM 模型、您的指示等。 本指南的重點是幫助您了解如何獲得更好的結果。 然而,每次您瀏覽此範例時,您都會得到(可能)截然不同的結果。
先決條件
Azure 帳戶和 Azure 訂用帳戶的存取權。 如需如何設定的詳細資訊,請參閱 Azure 帳戶的定價頁面。
GitHub 帳戶和 GitHub Copilot 訂用帳戶。 如需如何設定它們的詳細資訊,請參閱分別在 GitHub 上建立帳戶 和 GitHub Copilot 快速入門。
Visual Studio Code。 如需如何下載和安裝的詳細資訊,請參閱 設定 Visual Studio Code。
GitHub Copilot 擴充功能和 GitHub Copilot 聊天擴充功能。 如需如何安裝這些延伸模組的指示,請參閱在 VS Code 中設定 GitHub Copilot 和 在 VS Code 中開始使用 GitHub Copilot Chat。
這很重要
GitHub Copilot 是由 GitHub 管理的個別訂用帳戶。 如需 GitHub Copilot 訂用帳戶和支援的相關問題,請參閱 開始使用 GitHub Copilot 方案。
Visual Studio Code 的 Python 延伸模組。 如需如何安裝延伸模組的指示,請參閱 安裝 Python 和 Python 延伸模組。
PostgreSQL,包括 pgAdmin(可從 PostgreSQL Windows 安裝程式取得)
Git Bash (可從 適用於 Windows 的 Git 安裝程式取得)
PostgreSQL for Visual Studio Code (預覽) 延伸模組。 如需安裝和使用延伸模組的指示,請參閱 快速入門:使用 Visual Studio Code 預覽版的 PostgreSQL 延伸模組連線和查詢資料庫。
Azure CLI。 如需如何安裝 Azure CLI 的指示,請參閱如何 安裝 Azure CLI。
Azure 開發人員 CLI (
azd)。 如需如何安裝azd的指示,請參閱 安裝或更新 Azure 開發人員 CLI。
準備聊天會話
在 Visual Studio Code 中,使用標題列中的 [切換聊天] 按鈕,或選取 Ctrl+Alt+i 以開啟 [聊天] 視窗。 使用「新增聊天」圖示來建立新的聊天會話。
在聊天區域中,選取
Agent[模式]。 在撰寫本文時,Claude Sonnet 4產生最佳結果。 使用可用於程式碼產生的最佳模型。
驗證您的組態
確保您的 CLI 工具和 Visual Studio Code 已更新、正確配置並正確運行,以改善您的結果。
在新的聊天中,輸入以下提示:
I want to create a new Django website that stores data in PostgreSQL. Then, I'll want to deploy that new website to Azure. Do I have everything installed on my local computer that you will need to perform these tasks?GitHub Copilot 將請求執行一系列命令列檢查的權限,以確保您已安裝工具以及這些工具的最新版本。
在您的終端機中,使用命令
az --upgrade更新 Azure CLI 。在您的終端機中,使用命令安裝適用於 Azure CLI 的服務連接器無密碼延伸模組
az extension add --name serviceconnector-passwordless --upgrade在 Visual Studio Code 中,將預設終端機設定為 Git Bash。 前往檔案 > 首選項 > 設置,然後在“搜尋設定”中,鍵入“預設設定檔:Windows”並選擇“Git Bash”。 您可能需要重新啟動 Visual Studio Code 才能使此設定生效。
備註
使用 Git Bash 並不是絕對必要的,但在撰寫本文時,它會產生最佳結果。
在 Visual Studio Code 中,使用 PostgreSQL for Visual Studio Code (預覽版) 延伸模組,並流覽至
contacts資料庫。在 Visual Studio Code 中,使用 Azure 延伸模組,並確定您已登入 Azure 帳戶和訂用帳戶。 當您在主要側邊欄中開啟 Azure 延伸模組時,您應該能夠檢視現有的訂用帳戶和資源。
為新的應用程式檔案建立新的資料夾,並在 Visual Studio Code 中將其作為工作區開啟。
設定本機資料庫
雖然 GitHub Copilot 幾乎能夠執行開發人員通常執行的任何應用程式開發任務,但如果您以較小的步驟執行某些任務,您將獲得最佳結果。 若要改善結果,請先建立資料庫並設定驗證和授權,然後再使用 GitHub Copilot。
建立新的聊天,並使用下列提示:
On my PostgreSQL server localhost, please create a new database named market. Then create a new user <db-username> with password `<password>` and give that user full rights (create tables and other db objects, CRUD data) to the new market database. Please do the work, and only prompt me when you are unable to do it yourself.將 和
<db-username>分別取代<password>為您想要的資料庫使用者名稱和密碼。在 Windows 電腦上,建議的安全最佳作法是將資料庫使用者名稱和密碼儲存在本機檔案中:
%APPDATA%\postgresql\pgpass.conf這通常會解析硬碟上的以下位置:
c:\Users\<username>\AppSettings\Roaming\postgresql\pgpass.conf替換
<username>為您的 Windows 用戶名。檔案應使用下列格式:
localhost:5432:<database-name>:<database-user>:<password>這假設您正在本機電腦上使用 PostgreSQL 執行個體,而且它託管在預設連接埠 (5432) 上。
取代為
<database-name>contacts和 取代 和<db-username>為您<password>在上一個步驟中使用的認證。如需檔案的詳細資訊
pgpass.conf,請參閱 PostgreSQL 的文件。將檔案的
pgpass.conf路徑新增至 PATH 環境變數。測試連線以確保其正常運作。 使用 psql CLI 透過下列命令進行測試:
psql -h localhost -U <db-username> -d contacts取代
<db-username>為檔案中的pgpass.conf資料庫使用者名稱區段。如果設定
pgpass.conf不正確,您會看到輸入密碼的提示。
使用 GitHub Copilot 產生應用程式
首先,您提供有關在本機電腦上建置和測試應用程式的指示和指導。
在 Visual Studio Code 中,使用標題列中的 [切換聊天] 按鈕來開啟 [聊天] 視窗。 使用「新增聊天」圖示來建立新的聊天會話。
在聊天區域中,選取
Agent[模式]。 在撰寫本文時,Claude Sonnet 4產生最佳結果。 使用可用於程式碼產生的最佳模型。使用下列提示開始產生應用程式:
I want you to create a simple Contact Manager application using Django and PostgreSQL. This should be a CRUD application, so create web pages that display a list of contacts, view details, add a new contact, edit or delete a contact. Each Contact is comprised of a contact's Name, Address, and Phone number. Since this is a Python / Django project please make sure to work inside of a virtual environment (venv). I've already created a PostgreSQL database at `localhost` named `contacts`. There are no tables yet. For local development in PostgreSQL, I'm using a `pgpass.conf` file and I have tested that it works. Prefer Git Bash in the terminal. Beyond that, if there's anything I need to do, please include instructions. But I want you to do as much as you can on your own.提示具有以下特點:
- 您要建立的應用程式類型。 在本例中,是聯絡人管理應用程式。
- 要使用的技術。 在本例中,Django 和 PostgreSQL。
- 您要產生的網站架構。 在這種情況下,CRUD 樣式的應用程序具有列出所有聯繫人的頁面,並允許您向下切入到特定聯繫人。
- 問題網域的詳細資訊。 在此情況下,您需要提供要應用程式管理的資料欄位,包括聯絡人的姓名、地址和電話號碼。
- 有關資料庫的具體說明。 在此情況下,您會指示 GitHub Copilot 使用您已建立的特定資料庫,並提供資料庫的狀態,以及如何互動
- 有關環境的具體說明。 在此情況下,您會指示它使用 Git Bash。 您還告訴它您希望在 Python 環境 (venv) 中執行工作,這是最佳實踐。 GitHub Copilot 可能會自行選擇這些選項,但明確說明會使流程順利進行。
- 明確期待您讓它儘可能多地自動完成工作。 否則,GitHub Copilot 可能會提供指示供您採取。
- 對指令/上下文的明確期望。 如果它需要您執行其他操作,您可以設定期望需要它透過提供指示和指導來幫助您。
這很重要
當 GitHub Copilot 使用終端機建立新的虛擬環境時,Visual Studio Code 會偵測到並
venv顯示對話方塊,詢問您是否要使用它。 忽略該對話方塊。 它消失了。 允許 GitHub Copilot 專門用於此作業的終端機。GitHub Copilot 會使用內建終端機和 Visual Studio Code 環境來:
- 建立 Python 虛擬環境
- 安裝程式庫和其他相依性
- 產生程式碼檔案
- 產生資料庫表格
- 產生自述檔案以取得進一步指示
- 建立測試資料
- 啟動本機網頁伺服器
- 測試網站(使用簡單瀏覽器或 curl)
由於 LLM 生成代碼的方式,它使用的命令和生成的內容每次都不同。
使用 GitHub Copilot for Azure 部署至 Azure
GitHub Copilot 在本機產生網站之後,您將撰寫提示,要求 GitHub Copilot 對網站進行變更以準備部署,然後執行部署。 適用於 Azure 的 GitHub Copilot 延伸模組會藉由建立 Bicep 檔案,然後使用 CLI 執行 azd 這些檔案來處理此要求。
使用下列提示...您可以複製到記事本並更改括號中的任何值,例如 <resource-group-name> , <region-name> 然後複製並粘貼到 GitHub Copilot 聊天中:
Please help me deploy this Django app to Azure.
First, create and use a Resource Group named "<resource-group-name>" in the "<region-name>" region.
Second, create an Azure App Service for the Django app in the new "<resource-group-name>" in the "<region-name>" region.
Third, create a new Azure Database for PostgreSQL flexible server named "<server-name>" in a resource group named "<resource-group-name>" in my subscription "<subscription-id>". Use my current account ("<account-id>") as the Microsoft Entra administrator. For the PostgreSQL "Administrator login", use "<azure-database-username>" and password "<azure-database-password>". Use a "development" class database configuration in the "<region-name>" region. Create a new database named "contact_manager_db" and migrate all tables and data from the local version of "contact_manager_db". The local database uses the username "<local-database-username>" and password "<local-database-password>". Add my IP address as a firewall rule, allow public access to this resource through the internet using a public IP address, and allow public access from any Azure service within Azure to this server.
Fourth, use Service Connector (and any other services you need to make this configuration work successfully) to connect the web app to the database. You may need to modify the application code to accommodate Service Connector. Also, please ensure a secure connection between the Azure App Service web site and the Azure PostgreSQL Flexible Server.
Please choose the least expensive options.
If you are prompted for an environment, use the name "contacts-env". Configure my firewall to allow my IP address. Beyond that, if there's anything
I need to do, please include instructions. But I want you to do as much as you can on your own.
Before you start:
- Do you have any questions that need to be clarified?
- Please create a plan for deployment in the form of a TODO list, and then update the TODO list as you progress. Do not start until I have a chance to review your plan and tell you to proceed.
提示具有以下特點:
- 您要使用的特定服務。 在此情況下,您會告訴它您想要使用 Azure App Service、Azure PostgreSQL 彈性伺服器、服務連接器。 您還向它發出“做您需要做的任何其他事情”的指示,以確保它正常工作。
- 具體服務選項。 在此情況下,您會指出您想要針對每項服務使用最便宜的選項。
- 暗示可能的後續步驟。 在此情況下,您建議需要修改一些程式碼才能使用 Service Connector。
- 提前預測決策。 在此情況下,您會提供所需設定的答案,例如環境名稱
azd, - 明確期望您希望它自己做盡可能多的工作。 否則,它可能會提供說明供您採取。
- 對指令/上下文的明確期望。 設定期望,當它要求您採取行動時,您需要幫助和指導。
- 詢問是否需要任何澄清。 這通常會暴露出潛在的問題,例如邊緣情況或不明確的說明。
- 請求具有待辦事項清單的計劃。 讓您確信 GitHub Copilot for Azure 瞭解指派,並計劃依預期執行指派。
GitHub Copilot 會使用內建終端機和 Visual Studio Code 環境來:
- 更新程式碼檔以容納 Service Connector
- 生成 Bicep 檔案
- 執行
azdCLI - 測試部署
- 如有必要,請使用日誌或其他
與 GitHub Copilot 互動
GitHub Copilot 在執行許多工作之前需要您的輸入。 暫停輸入是您指導 GitHub Copilot 修正路線的機會,以防止錯誤或根據您的喜好自訂產生的輸出。
當它工作時,您可以使用按鈕觀看並同意它向您 Continue 詢問的大部分問題。
這很重要
如果您收到非預期的結果,請使用新的聊天會話重新啟動。
有時,您需要提供意見。 系統會提示您輸入,但有幾個不同的時刻:
- 使用者認證 - 如果終端機中的目前作業需要使用者名稱或密碼,
- 決定時刻 - 有時,GitHub Copilot 會在清單中為您提供數個選項,並詢問您偏好哪一個。
- 命令面板 - 有時,GitHub Copilot 會使用延伸模組的功能,且選項會顯示在命令面板中。 做出正確的選擇後,GitHub Copilot 將繼續進行。
-
互動式登入 - Azure CLI 和
azdCLI 需要您進行驗證,並起始數個驗證機制之一。
測試並要求變更
當 GitHub Copilot 完成時,它可能會認為網站完整且功能正常。 不過,您的測試可能會發現問題,或預期之外/不受歡迎的應用程式功能。
使用盡可能詳細描述問題的提示。 例如,如果應用程式無法運作,請提供盡可能多的資訊,包括確切的錯誤訊息 和預期結果。
中斷流程
有時,您可能會注意到 GitHub Copilot 要么陷入嘗試重複執行相同任務的循環中,要么陷入永不返回的進程中。 例如,在診斷網站問題時,GitHub Copilot 可能想要執行類似以下命令:
az webapp log tail
當 GitHub Copilot 停滯時,您可以透過下列其中一種方式中斷 GitHub Copilot:
- 按著控制+丙
- 使用聊天中的暫停按鈕
- 結束聊天會話並開始新的聊天
這很重要
結束聊天會話會破壞會話期間建立的所有內容,這可能是可取的,也可能不是。
若要為其提供剛才發生之事的背景資訊,並將其推向可能的解決方案,您可以在中斷 GitHub Copilot 之後立即新增提示,例如:
You were just getting the logs from Azure App Service but it did not return
so you got stuck. Try to interrupt after a minute once you get what you need
from the logs.