教程:将具有 CI/CD 的应用程序部署到 Service Fabric 群集Tutorial: Deploy an application with CI/CD to a Service Fabric cluster

本教程是一个系列的第四部分,介绍了如何使用 Azure Pipelines 为 Azure Service Fabric 应用程序设置持续集成和部署。This tutorial is part four of a series and describes how to set up continuous integration and deployment for an Azure Service Fabric application using Azure Pipelines. 需要一个现有的 Service Fabric 应用程序,并使用在生成 .NET 应用程序中创建的应用程序作为示例。An existing Service Fabric application is needed, the application created in Build a .NET application is used as an example.

在该系列的第三部分中,你会学习如何:In part three of the series, you learn how to:

  • 向项目中添加源代码管理Add source control to your project
  • 在 Azure 管道中创建生成管道Create a build pipeline in Azure Pipelines
  • 在 Azure 管道中创建发布管道Create a release pipeline in Azure Pipelines
  • 自动部署和升级应用程序Automatically deploy and upgrade an application

在此系列教程中,你会学习如何:In this tutorial series you learn how to:

先决条件Prerequisites

在开始学习本教程之前:Before you begin this tutorial:

下载投票示例应用程序Download the Voting sample application

如果未生成本系列教程的第一部分中的投票示例应用程序,可以下载它。If you did not build the Voting sample application in part one of this tutorial series, you can download it. 在命令窗口中,运行以下命令,将示例应用程序存储库克隆到本地计算机。In a command window, run the following command to clone the sample app repository to your local machine.

git clone https://github.com/Azure-Samples/service-fabric-dotnet-quickstart

准备一个发布配置文件Prepare a publish profile

你已创建了一个应用程序并已将该应用程序部署到了 Azure,现在可以设置持续集成了。Now that you've created an application and have deployed the application to Azure, you're ready to set up continuous integration. 首先,在应用程序中准备一个发布配置文件,供要在 Azure Pipelines 中执行的部署进程使用。First, prepare a publish profile within your application for use by the deployment process that executes within Azure Pipelines. 应当将发布配置文件配置为以之前创建的群集为目标。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....

在应用程序项目中选择一个要用于持续集成工作流的目标配置文件,例如 Cloud。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.

推送配置文件

将你的 Visual Studio 解决方案共享到一个新的 Azure DevOps Git 存储库Share your Visual Studio solution to a new Azure DevOps Git repo

将你的应用程序源文件共享到 Azure DevOps 中的一个项目,以便可以生成内部版本。Share your application source files to a 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.

在“团队资源管理器”中的“推送”视图中,在“推送到 Azure DevOps”下选择“发布 Git 存储库”按钮。 In the Push view in Team Explorer, select the Publish Git Repo button under Push to Azure DevOps.

推送 Git 存储库

验证你的电子邮件地址并在“Azure DevOps 域”下拉列表中选择你的帐户。 Verify your email and select your account in the Azure DevOps Domain drop-down. 输入存储库名称并选择“发布存储库”。 Enter your repository name and select Publish repository.

推送 Git 存储库

发布存储库会在你的帐户中创建一个与本地存储库同名的新项目。Publishing the repo creates a new project in your account with the same name as the local repo. 若要在现有项目中创建存储库,请单击“存储库名称”旁边的“高级”并选择一个项目。 To create the repo in an existing project, click Advanced next to Repository name and select a project. 可以通过选择“在 Web 上查看”在 Web 上查看代码。 You can view your code on the web by selecting See it on the web.

使用 Azure Pipelines 配置持续交付Configure Continuous Delivery with Azure Pipelines

Azure Pipelines 生成管道描述了由按顺序执行的一组生成步骤组成的工作流。An Azure Pipelines build pipeline describes a workflow that is composed of a set of build steps that are executed sequentially. 创建一个生成管道,以生成要部署到 Service Fabric 群集的 Service Fabric 应用程序包和其他项目。Create a build pipeline that produces a Service Fabric application package, and other artifacts, to deploy to a Service Fabric cluster. 请详细了解 Azure Pipelines 生成管道Learn more about Azure Pipelines build pipelines.

Azure Pipelines 发布管道描述了将应用程序程序包部署到群集的工作流。An Azure Pipelines release pipeline describes a workflow that deploys an application package to a cluster. 一起使用时,生成管道和发布管道将执行从开始到结束的整个工作流,即一开始只有源文件,结束时群集中会有一个运行的应用程序。When used together, the build pipeline and release pipeline execute the entire workflow starting with source files to ending with a running application in your cluster. 请详细了解 Azure Pipelines 发布管道Learn more about Azure Pipelines release pipelines.

创建生成管道Create a build pipeline

打开 Web 浏览器并导航到你的新项目:https://<myaccount>.visualstudio.com/Voting/Voting%20Team/_git/VotingOpen a web browser and navigate to your new project at: https://<myaccount>.visualstudio.com/Voting/Voting%20Team/_git/Voting.

选择“管道”选项卡,接着选择“生成”,然后单击“新建管道” 。Select the Pipelines tab, then Builds, then click New Pipeline.

新建管道

选择 Azure Repos Git 作为源,选择“Voting” 团队项目、“Voting” 存储库和 master 默认分库作为手动的和计划的生成。Select Azure Repos Git as source, Voting Team project, Voting Repository, and master Default branch for manual and scheduled builds. 然后单击“继续” 。Then click Continue.

选择存储库

在“选择模板”中,选择“Azure Service Fabric 应用程序”模板,然后单击“应用” 。In Select a template, select the Azure Service Fabric application template and click Apply.

选择“生成模板”

在“任务”中,输入“Hosted VS2017”作为代理池。 In Tasks, enter "Hosted VS2017" as the Agent pool.

选择任务

在“触发器”下,选中“启用持续集成”来启用持续集成。 Under Triggers, enable continuous integration by checking Enable continuous integration. 在“分库筛选器” 中, ,“分库规格” 默认为“master”。Within Branch filters, the Branch specification defaults to master. 选择“保存并排队”以手动启动生成 。Select Save and 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 pipeline that deploys your application to a cluster.

创建发布管道Create a release pipeline

依次选择“管道”选项卡、“发布”、“+ 新建管道” 。Select the Pipelines tab, 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->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.chinaeast.cloudapp.chinacloudapi.cn:19000”(或要部署到的群集的终结点)。Specify a connection name of "mysftestcluster" and a cluster endpoint of "tcp://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn: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.

接下来,向管道添加一个生成项目,以便发布管道可以找到生成输出。Next, add a build artifact to the pipeline so the release pipeline can find the output from the build. 依次选择“管道”和“项目”->“+添加”。 Select Pipeline and Artifacts->+Add. 在“源(生成定义)” 中,选择之前创建的生成管道。In Source (Build definition), select the build pipeline 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 pipeline.

启用触发器

选择“+ 发布” -> “创建发布” -> “创建”,手动创建发布 。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. 打开 Web 浏览器并导航到 http://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn:19080/Explorer/Open a web browser and navigate to http://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn: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. 通过从右下角的状态栏中选择“挂起的更改”图标(Commit changes to your local Git repository by selecting the pending changes icon (挂起的),将更改提交到本地 Git 存储库。) from the status bar in the bottom right.

在“团队资源管理器”的“更改”视图中,添加一条消息来说明所做的更新,然后提交更改。 On the Changes view in Team Explorer, add a message describing your update and commit your changes.

全部提交

在“团队资源管理器”中选择“未发布的更改”状态栏图标(未发布的更改)或“同步”视图。Select the unpublished changes status bar icon (Unpublished changes) or the Sync view in Team Explorer. 选择“推送”以更新 Azure Pipelines 中的代码。 Select Push to update your code in Azure Pipelines.

推送更改

将更改推送到 Azure Pipelines 会自动触发生成。Pushing the changes to Azure Pipelines automatically triggers a build. 当生成管道成功完成时,会自动创建一个发布,并将开始升级群集上的应用程序。When the build pipeline successfully completes, a release is automatically created and starts upgrading the application on the cluster.

若要检查生成进度,请在 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 pipeline that deploys your application to a cluster.

验证部署是否已成功以及应用程序是否正在群集中运行。Verify that the deployment succeeded and the application is running in the cluster. 打开 Web 浏览器并导航到 http://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn:19080/Explorer/Open a web browser and navigate to http://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn: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 pipeline
  • 创建发布管道Create a release pipeline
  • 自动部署和升级应用程序Automatically deploy and upgrade an application