教學課程:將具有 CI/CD 的容器應用程式部署到 Service Fabric 叢集Tutorial: Deploy a container application with CI/CD to a Service Fabric cluster

本教學課程是系列中的第二部分,說明如何使用 Visual Studio 和 Azure DevOps 設定 Azure Service Fabric 容器應用程式的持續整合和部署。This tutorial is part two of a series and describes how to set up continuous integration and deployment for an Azure Service Fabric container application using Visual Studio and Azure DevOps. 需要現有的 Service Fabric 應用程式,在將 Windows 容器中的 .NET 應用程式部署到 Azure Service Fabric 中建立的應用程式會作為範例。An existing Service Fabric application is needed, the application created in Deploy a .NET application in a Windows container to Azure Service Fabric is used as an example.

在本系列的第二部分中,您將瞭解如何:In part two of the series, you learn how to:

  • 將原始檔控制新增至專案Add source control to your project
  • 在 Visual Studio Team Explorer 中建立組建定義Create a build definition in Visual Studio Team Explorer
  • 在 Visual Studio Team Explorer 中建立發行定義Create a release definition in Visual Studio Team Explorer
  • 自動部署和升級應用程式Automatically deploy and upgrade an application

必要條件Prerequisites

開始進行本教學課程之前:Before you begin this tutorial:

下載發行設定檔Prepare a publish profile

您現在已經部署容器應用程式,接下來可以設定持續整合。Now that you've deployed a container application, you're ready to set up continuous integration. 首先,在應用程式中準備部署程序使用的發行設定檔 (於 Azure DevOps 內執行)。First, prepare a publish profile within your application for use by the deployment process that executes within Azure DevOps. 發行設定檔應設定為以先前建立的叢集為目標。The publish profile should be configured to target the cluster that you've previously created. 啟動 Visual Studio,並開啟現有的 Service Fabric 應用程式專案。Start Visual Studio and open an existing Service Fabric application project. 在 [方案總管] 中,以滑鼠右鍵按一下應用程式並選取 [發佈...] 。In Solution Explorer, right-click the application and select Publish....

在您的應用程式專案內選擇要使用於持續整合工作流程 (例如雲端) 的目標設定檔。Choose a target profile within your application project to use for your continuous integration workflow, for example Cloud. 指定叢集連線端點。Specify the cluster connection endpoint. 勾選升級應用程式核取方塊,讓您的應用程式對於 Azure DevOps 中的每個部署升級。Check the Upgrade the Application checkbox so that your application upgrades for each deployment in Azure DevOps. 按一下 [儲存] 超連結,將設定儲存至發行設定檔,然後按一下 [取消] 關閉對話方塊。Click the Save hyperlink to save the settings to the publish profile and then click Cancel to close the dialog box.

推送設定檔

向新的 Azure DevOps Git 儲存機制共用 Visual Studio 解決方案Share your Visual Studio solution to a new Azure DevOps Git repo

向 Azure DevOps 中的 Team 專案共用應用程式原始檔,以便產生組建。Share your application source files to a team project in Azure DevOps so you can generate builds.

Visual Studio 右上角的狀態列上,選取 [新增至原始檔控制] -> [Git] ,建立專案的新本機 Git 儲存機制。Create a new local Git repo for your project by selecting Add to Source Control -> Git on the status bar in the lower right-hand corner of Visual Studio.

在 [Team Explorer] 的 [推送] 檢視中,選取 [推送至 Azure DevOps] 下的 [發佈 Git 儲存機制] 按鈕。In the Push view in Team Explorer, select the Publish Git Repo button under Push to Azure DevOps.

推送 Git 儲存機制

確認電子郵件,並在 [帳戶] 下拉式清單中選取組織。Verify your email and select your organization in the Account drop-down. 如果您還沒有組織,請先設定一個。You may have to set up an organization if you don't already have one. 輸入您的儲存機制名稱,並選取 [發佈儲存機制] 。Enter your repository name and select Publish repository.

推送 Git 儲存機制

發佈存放庫將在帳戶中建立與本機存放庫名稱相同的新 Team 專案。Publishing the repository creates a new team project in your account with the same name as the local repo. 若要在現有的 Team 專案中建立存放庫,請按一下存放庫名稱旁邊的 [進階] ,並選取 Team 專案。To create the repository in an existing team project, click Advanced next to Repository name and select a team project. 您可以選取在網路上檢視,在網路上檢視您的程式碼。You can view your code on the web by selecting See it on the web.

設定 Azure Pipelines 的持續傳遞Configure Continuous Delivery with Azure Pipelines

Azure DevOps 組建定義描述由一組循序執行的組建步驟所組成的工作流程。An Azure DevOps build definition describes a workflow that is composed of a set of build steps that are executed sequentially. 建立產生 Service Fabric 應用程式封裝的組建定義,以及其他構件,以便部署到 Service Fabric 叢集。Create a build definition that produces a Service Fabric application package, and other artifacts, to deploy to a Service Fabric cluster. 深入了解 Azure DevOps 組建定義Learn more about Azure DevOps build definitions.

Azure DevOps 發行定義描述將應用程式封裝部署到叢集的工作流程。An Azure DevOps release definition describes a workflow that deploys an application package to a cluster. 一起使用時,組建定義和發行定義可以執行整個工作流程,從來源檔案開始,並以叢集中的執行中應用程式結束。When used together, the build definition and release definition execute the entire workflow starting with source files to ending with a running application in your cluster. 深入了解 Azure DevOps 發行定義Learn more about Azure DevOps release definitions.

建立組建定義Create a build definition

在網頁瀏覽器中瀏覽至 https://dev.azure.com 並選取您的組織,然後選取新增專案以開啟新的 Team 專案。Open your new team project by navigating to https://dev.azure.com in a web browser and selecting your organization, followed by the new project.

選取左窗格中的 [管線] 選項,然後按一下 [新增管線] 。Select the Pipelines option on the left panel, then click New Pipeline.

注意

如果您未看到組建定義範本,請確定 [新的 YAML 管線建立體驗] 功能已關閉。If you do not see the build definition template, make sure the New YAML pipeline creation experience feature is turned off. 這項功能設定於 DevOps 帳戶的 [預覽功能] 區段內。This feature is configured within the Preview Features section of your DevOps account.

新增管線

選取 [Azure Repos Git] 作為來源,然後依序選取 Team 專案名稱、專案存放庫、主要預設分支或手動和排程組建。Select Azure Repos Git as source, your Team project name, your project Repository, and master Default branch or manual and scheduled builds. 然後按一下 [繼續] 。Then click Continue.

在 [選取範本] 中,選取 [含 Docker 支援的 Azure Service Fabric 應用程式] 範本,然後按一下 [套用] 。In Select a template, select the Azure Service Fabric application with Docker support template and click Apply.

選擇組建範本

在 [工作] 中,選取 [Hosted VS2017] 作為 [代理程式集區] 。In Tasks, select Hosted VS2017 as the Agent pool.

選取工作

按一下 [標記映像] 。Click Tag images.

在 [容器登錄類型] 中,選取 [Azure Container Registry] 。In Container Registry Type, select Azure Container Registry. 選取 [Azure 訂用帳戶] ,然後按一下 [授權] 。Select an Azure Subscription, then click Authorize. 選取 [Azure Container Registry] 。Select an Azure Container Registry.

選取 Docker Tag 映像

按一下 [推送映像] 。Click Push images.

在 [容器登錄類型] 中,選取 [Azure Container Registry] 。In Container Registry Type, select Azure Container Registry. 選取 [Azure 訂用帳戶] ,然後按一下 [授權] 。Select an Azure Subscription, then click Authorize. 選取 [Azure Container Registry] 。Select an Azure Container Registry.

選取 Docker Push 映像

在 [觸發程序] 索引標籤下方,透過核取 [啟用持續整合] 來啟用持續整合。Under the Triggers tab, enable continuous integration by checking Enable continuous integration. 在 [分支篩選] 內按一下 [+ 新增] ,[分支規格] 將會預設為 [主要] 。Within Branch filters, click + Add, and the Branch specification will default to master.

在 [儲存組建管線和佇列] 對話方塊中,按一下 [儲存並加入佇列] 以手動啟動組建。In the Save build pipeline and queue dialog, click Save & queue to manually start a build.

選取觸發程序

推送或簽入時也會觸發組建。Builds also trigger upon push or check-in. 若要檢查組建進度,請切換到 [組建] 索引標籤。您確認組建執行成功,請定義將應用程式部署至叢集的發行定義。To check your build progress, switch to the Builds tab. Once you verify that the build executes successfully, define a release definition that deploys your application to a cluster.

建立發行定義Create a release definition

選取左窗格中的 [管線] 選項,然後依序選取 [發行] 和 [+ 新增管線] 。Select the Pipelines option on the left panel, then Releases, then + New pipeline. 在 [選取範本] 中,從清單中選取 [Azure Service Fabric 部署] 範本,然後選取 [套用] 。In Select a template, select the Azure Service Fabric Deployment template from the list and then Apply.

選擇發行範本

選取 [工作] 和 [環境 1] ,然後選取 [+ 新增] 來新增叢集連線。Select Tasks, then Environment 1, and then +New to add a new cluster connection.

新增叢集連線

在 [新增 Service Fabric 連線] 檢視中,選取 [憑證式] 或 [Azure Active Directory] 驗證。In the Add new Service Fabric Connection view select Certificate Based or Azure Active Directory authentication. 指定連線名稱 "mysftestcluster" 和叢集端點 "tcp://mysftestcluster.southcentralus.cloudapp.azure.com:19000" (或您要部署的目標叢集端點)。Specify a connection name of "mysftestcluster" and a cluster endpoint of "tcp://mysftestcluster.southcentralus.cloudapp.azure.com:19000" (or the endpoint of the cluster you are deploying to).

若是憑證式驗證,請新增伺服器憑證的伺服器憑證指紋 (用來建立叢集的伺服器憑證)。For certificate based authentication, add the Server certificate thumbprint of the server certificate used to create the cluster. 在 [用戶端憑證] 中,新增 Base-64 編碼的用戶端憑證檔案。In Client certificate, add the base-64 encoding of the client certificate file. 請參閱該欄位的快顯說明,了解如何取得以 Base-64 編碼表示的憑證。See the help pop-up on that field for info on how to get that base-64 encoded representation of the certificate. 以及新增憑證的密碼Also add the Password for the certificate. 如果您沒有個別的用戶端憑證,您可以使用叢集或伺服器憑證。You can use the cluster or server certificate if you don't have a separate client certificate.

若是 Azure Active Directory 認證,請新增伺服器憑證的伺服器憑證指紋 (用來建立叢集的伺服器憑證),以及在 [使用者名稱] 和 [密碼] 欄位中新增要用來連線到叢集的認證。For Azure Active Directory credentials, add the Server certificate thumbprint of the server certificate used to create the cluster and the credentials you want to use to connect to the cluster in the Username and Password fields.

按一下 [新增] 以儲存叢集連線。Click Add to save the cluster connection.

在 [代理程式階段] 底下,按一下 [部署 Service Fabric 應用程式] 。Under Agent Phase, click Deploy Service Fabric Application. 按一下 [Docker 設定] ,然後按一下 [設定 Docker 設定] 。Click Docker Settings and then click Configure Docker settings. 在 [登錄認證來源] 中,選取 [Azure Resource Manager 服務連線] 。In Registry Credentials Source, select Azure Resource Manager Service Connection. 然後選取 [Azure 訂用帳戶] 。Then select your Azure subscription.

發行管線代理程式

接下來,將組建成品新增至管線,讓發行定義可以從組建中找到輸出。Next, add a build artifact to the pipeline so the release definition can find the output from the build. 選取 [管線] 和 [成品] ->[+ 新增] 。Select Pipeline and Artifacts->+Add. 在 [來源 (組建定義)] 中,選取您先前建立的組建定義。In Source (Build definition), select the build definition you created previously. 按一下 [新增] 以儲存組建成品。Click Add to save the build artifact.

新增成品

啟用持續部署觸發程序,以便在組建完成時自動建立發行。Enable a continuous deployment trigger so that a release is automatically created when the build completes. 按一下成品中的閃電圖示、啟用觸發程序,然後按一下 [儲存] 以儲存發行定義。Click the lightning icon in the artifact, enable the trigger, and click Save to save the release definition.

啟用觸發程序

選取 [+ 發行] -> [建立發行] -> [建立] 可手動建立發行。Select + Release -> Create a Release -> Create to manually create a release. 您可以在 [發行] 索引標籤中監視發行進度。You can monitor the release progress in the Releases tab.

確認部署成功,而且應用程式在叢集中執行。Verify that the deployment succeeded and the application is running in the cluster. 開啟瀏覽器並巡覽至 http://mysftestcluster.southcentralus.cloudapp.azure.com:19080/Explorer/Open a web browser and navigate to http://mysftestcluster.southcentralus.cloudapp.azure.com:19080/Explorer/. 請注意應用程式版本,在此範例中是「1.0.0.20170616.3」。Note the application version, in this example it is "1.0.0.20170616.3".

認可並推送變更,觸發發行程序Commit and push changes, trigger a release

將某些程式碼變更簽入到 Azure DevOps,以確認持續整合管線正常運作。To verify that the continuous integration pipeline is functioning by checking in some code changes to Azure DevOps.

您撰寫程式碼時,Visual Studio 會自動追蹤您的變更。As you write your code, your changes are automatically tracked by Visual Studio. 認可本機 Git 儲存機制的變更,方法是從右下方的狀態列選取暫止變更圖示 (Commit changes to your local Git repository by selecting the pending changes icon (Pending)。) from the status bar in the bottom right.

在 Team Explorer 的變更檢視中,加入描述更新的訊息,並認可變更。On the Changes view in Team Explorer, add a message describing your update and commit your changes.

全部認可

選取 [未發行的變更] 狀態列圖示 (未發行的變更) 或 Team Explorer 中的 [同步] 檢視。Select the unpublished changes status bar icon (Unpublished changes) or the Sync view in Team Explorer. 選取 [推送] 更新 Azure DevOps 中的程式碼。Select Push to update your code in Azure DevOps.

推送變更

自動將變更推送至 Azure DevOps 觸發組建。Pushing the changes to Azure DevOps automatically triggers a build. 組建定義成功完成時,發行就會自動建立,並開始升級叢集上的應用程式。When the build definition successfully completes, a release is automatically created and starts upgrading the application on the cluster.

若要檢查組建進度,請切換到 Team Explorer Visual Studio 中的 [組建] 索引標籤。To check your build progress, switch to the Builds tab in Team Explorer in Visual Studio. 您確認組建執行成功,請定義將應用程式部署至叢集的發行定義。Once you verify that the build executes successfully, define a release definition that deploys your application to a cluster.

確認部署成功,而且應用程式在叢集中執行。Verify that the deployment succeeded and the application is running in the cluster. 開啟瀏覽器並巡覽至 http://mysftestcluster.southcentralus.cloudapp.azure.com:19080/Explorer/Open a web browser and navigate to http://mysftestcluster.southcentralus.cloudapp.azure.com:19080/Explorer/. 請注意應用程式版本,在此範例中是 "1.0.0.20170815.3"。Note the application version, in this example it is "1.0.0.20170815.3".

Service Fabric Explorer

更新應用程式Update the application

在應用程式中進行程式碼變更。Make code changes in the application. 在先前的步驟儲存並認可變更。Save and commit the changes, following the previous steps.

應用程式的升級開始後,即可注意 Service Fabric Explorer 中的升級進度:Once the upgrade of the application begins, you can watch the upgrade progress in Service Fabric Explorer:

Service Fabric Explorer

應用程式升級可能需要幾分鐘的時間。The application upgrade may take several minutes. 升級完成時,應用程式將執行下一個版本。When the upgrade is complete, the application will be running the next version. 在此範例中為 "1.0.0.20170815.4"。In this example "1.0.0.20170815.4".

Service Fabric Explorer

後續步驟Next steps

在本教學課程中,您已了解如何:In this tutorial, you learned how to:

  • 將原始檔控制新增至專案Add source control to your project
  • 建立組建定義Create a build definition
  • 建立發行定義Create a release definition
  • 自動部署和升級應用程式Automatically deploy and upgrade an application

在本教學課程的下一個部分,了解如何設定監視您的容器In the next part of the tutorial, learn how to set up monitoring for your container.