您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Azure DevOps Projects 将 ASP.NET 应用部署到 Azure 虚拟机Tutorial: Deploy your ASP.NET app to Azure virtual machines by using Azure DevOps Projects

Azure DevOps Projects 提供一种简化的体验,你在其中既可使用现有的代码和 Git 存储库,也可选择一个示例应用程序,以便创建连接到 Azure 的持续集成 (CI) 和持续交付 (CD) 管道。Azure DevOps Projects presents a simplified experience where you can bring your existing code and Git repo or choose a sample application to create a continuous integration (CI) and continuous delivery (CD) pipeline to Azure.

DevOps Projects 还可以:DevOps Projects also:

  • 自动创建 Azure 资源,例如新的 Azure 虚拟机 (VM)。Automatically creates Azure resources, such as a new Azure virtual machine (VM).
  • 在 Azure DevOps 中创建并配置一个发布管道,其中包括用于 CI 的生成管道。Creates and configures a release pipeline in Azure DevOps that includes a build pipeline for CI.
  • 为 CD 设置发布管道。Sets up a release pipeline for CD.
  • 创建用于监视的 Azure Application Insights 资源。Creates an Azure Application Insights resource for monitoring.

在本教程中,将:In this tutorial, you will:

  • 使用 DevOps Projects 部署 ASP.NET 应用Use DevOps Projects to deploy your ASP.NET app
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 检查 CI 管道Examine the CI pipeline
  • 检查 CD 管道Examine the CD pipeline
  • 提交对 Azure Repos 所做的更改并将其自动部署到 AzureCommit changes to Azure Repos and automatically deploy them to Azure
  • 配置 Azure Application Insights 监视Configure Azure Application Insights monitoring
  • 清理资源Clean up resources

先决条件Prerequisites

使用 DevOps Projects 部署 ASP.NET 应用Use DevOps Projects to deploy your ASP.NET app

DevOps Projects 在 Azure Pipelines 中创建 CI/CD 管道。DevOps Projects creates a CI/CD pipeline in Azure Pipelines. 可以创建新的 Azure DevOps 组织,或使用现有的组织。You can create a new Azure DevOps organization or use an existing organization. DevOps Projects 还可在所选的 Azure 订阅中创建 Azure 资源,例如虚拟机。DevOps Projects also creates Azure resources such as virtual machines in the Azure subscription of your choice.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左窗格中,选择“新建”。In the left pane, select New.

  3. 在搜索框中键入 DevOps Projects,然后选择“创建”。In the search box, type DevOps Projects, and then select Create.

    DevOps Projects 仪表板

  4. 依次选择“.NET”、“下一步”。Select .NET, and then select Next.

  5. 在“选择应用程序框架”下选择“ASP.NET”,然后选择“下一步”。Under Choose an application Framework, select ASP.NET, and then select Next.
    在前述步骤中选择的应用程序框架规定了此处可用的 Azure 服务部署目标的类型。The application framework, which you chose in a previous step, dictates the type of Azure service deployment target that's available here.

  6. 选择虚拟机,然后选择“下一步”。Select the virtual machine, and then select Next.

配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription

  1. 创建新的 Azure DevOps 组织,或选择现有的组织。Create a new Azure DevOps organization or select an existing organization.

  2. 输入 Azure DevOps 项目的名称。Enter a name for your Azure DevOps project.

  3. 选择你的 Azure 订阅服务。Select your Azure subscription services.
    (可选)可以选择“更改”,然后输入其他配置详细信息,例如 Azure 资源的位置。Optionally, you can select Change and then enter more configuration details, such as the location of the Azure resources.

  4. 为新的 Azure 虚拟机资源输入虚拟机名称、用户名和密码,然后选择“完成”。Enter a virtual machine name, username, and password for your new Azure virtual machine resource, and then select Done.
    几分钟后,该 Azure 虚拟机即可准备就绪。After a few minutes, the Azure virtual machine will be ready. 将在你的 Azure DevOps 组织的存储库中设置一个示例 ASP.NET 应用程序,执行生成和发布,然后将应用程序部署到新建的 Azure VM。A sample ASP.NET application is set up in a repo in your Azure DevOps organization, a build and release is executed, and your application is deployed to the newly created Azure VM.

    完成上述过程后,DevOps Projects 仪表板会显示在 Azure 门户中。After it's completed, the DevOps Projects dashboard is displayed in the Azure portal. 也可以从 Azure 门户中的“所有资源”直接导航到仪表板。You can also navigate to the dashboard directly from All resources in the Azure portal.

    在此仪表板中可以查看 Azure DevOps 代码存储库、CI/CD 管道,以及 Azure 中正在运行的应用程序。The dashboard provides visibility into your Azure DevOps code repo, your CI/CD pipeline, and your running application in Azure.

    仪表板视图

DevOps Projects 会自动配置 CI 生成和发布触发器,用于将代码更改部署到存储库。DevOps Projects automatically configures a CI build and release trigger that deploys code changes to your repo. 可以进一步在 Azure DevOps 中配置其他选项。You can further configure additional options in Azure DevOps. 若要查看正在运行的应用程序,请选择“浏览”。To view your running application, select Browse.

检查 CI 管道Examine the CI pipeline

DevOps Projects 会自动在 Azure Pipelines 中配置一个 CI/CD 管道。DevOps Projects automatically configured a CI/CD pipeline in Azure Pipelines. 可以浏览和自定义此管道。You can explore and customize the pipeline. 若要了解生成管道,请执行以下操作:To familiarize yourself with the build pipeline, do the following:

  1. 在 DevOps Projects 仪表板顶部,选择“生成管道”。At the top of the DevOps Projects dashboard, select Build Pipelines.
    浏览器标签页会显示新项目的生成管道。A browser tab displays the build pipeline for your new project.

  2. 指向“状态”字段,然后选择省略号 (...)。Point to the Status field, and then select the ellipsis (...).
    菜单中会显示多个选项,例如,将新生成排队、暂停某个生成,以及编辑生成管道。A menu displays several options, such as queueing a new build, pausing a build, and editing the build pipeline.

  3. 选择“编辑”。Select Edit.

  4. 在此窗格中,可以检查生成管道的各种任务。In this pane, you can examine the various tasks for your build pipeline.
    该生成会执行各种任务,例如,从 Git 存储库提取源、还原依赖项,以及发布用于部署的输出。The build performs various tasks, such as fetching sources from the Git repo, restoring dependencies, and publishing outputs used for deployments.

  5. 在生成管道的顶部,选择生成管道名称。At the top of the build pipeline, select the build pipeline name.

  6. 将生成管道的名称更改为更具描述性的名称,选择“保存并排队”,然后选择“保存”。Change the name of your build pipeline to something more descriptive, select Save & queue, and then select Save.

  7. 在生成管道名称下,选择“历史记录”。Under your build pipeline name, select History.
    此窗格显示最近针对生成所做的更改的审核线索。This pane displays an audit trail of your recent changes for the build. Azure DevOps 会跟踪对生成管道所做的任何更改,并允许进行版本比较。Azure DevOps keeps track of any changes made to the build pipeline, and it allows you to compare versions.

  8. 选择“触发器”。Select Triggers.
    DevOps Projects 会自动创建一个 CI 触发器,每次向存储库提交内容都会启动新的生成。DevOps Projects automatically creates a CI trigger, and every commit to the repo starts a new build. (可选)可以选择在 CI 过程中包括或排除分支。Optionally, you can choose to include or exclude branches from the CI process.

  9. 选择“保留期”。Select Retention.
    可以根据方案指定策略,以保留或删除特定数目的生成。Depending on your scenario, you can specify policies to keep or remove a certain number of builds.

检查 CD 管道Examine the CD pipeline

DevOps Projects 会自动创建并配置从 Azure DevOps 组织部署到 Azure 订阅所要执行的步骤。DevOps Projects automatically creates and configures the necessary steps to deploy from your Azure DevOps organization to your Azure subscription. 这些步骤包括配置 Azure 服务连接,以便在 Azure 订阅中进行 Azure DevOps 身份验证。These steps include configuring an Azure service connection to authenticate Azure DevOps to your Azure subscription. 自动化还会创建一个 CD 管道,用于提供到 Azure 虚拟机的 CD。The automation also creates a CD pipeline, which provides the CD to the Azure virtual machine. 若要详细了解 Azure DevOps CD 管道,请执行以下操作:To learn more about the Azure DevOps CD pipeline, do the following:

  1. 依次选择“生成和发布”、“发布”。Select Build and Release, and then select Releases.
    DevOps Projects 会创建一个发布管道用于管理到 Azure 的部署。DevOps Projects creates a release pipeline to manage deployments to Azure.

  2. 选择发布管道旁边的省略号 (...),然后选择“编辑”。Select the ellipsis (...) next to your release pipeline, and then select Edit.
    发布管道包含一个管道,用于定义发布过程。The release pipeline contains a pipeline, which defines the release process.

  3. 在“项目”下选择“删除”。Under Artifacts, select Drop.
    在前述步骤中检查过的生成管道将生成用于项目的输出。The build pipeline you examined in previous steps produces the output that's used for the artifact.

  4. 在“删除”图标的旁边,选择“持续部署触发器”。Next to the Drop icon, select Continuous deployment trigger.
    此发布管道有一个已启用的 CD 触发器,每次有新的生成项目可用时,此触发器就会执行部署。This release pipeline has an enabled CD trigger, which executes a deployment each time a new build artifact is available. (可选)可以禁用此触发器,这样就需要手动执行部署。Optionally, you can disable the trigger so that your deployments require manual execution.

  5. 在左侧选择“任务”,然后选择你的环境。At the left, select Tasks, and then select your environment.
    任务是部署过程执行的活动,已按阶段分组。Tasks are the activities that your deployment process executes, and they're grouped in phases. 此发布管道包括两个阶段:This release pipeline happens in two phases:

    • 第一阶段包含“Azure 资源组部署”任务,该任务执行以下操作:The first phase contains an Azure Resource Group Deployment task that does two things:
      • 配置用于部署的 VMConfigures the VM for deployment
      • 将新 VM 添加到某个 Azure DevOps 部署组。Adds the new VM to an Azure DevOps deployment group. Azure DevOps 中的 VM 部署组管理部署目标计算机的逻辑组。The VM deployment group in Azure DevOps manages logical groups of deployment target machines.
    • 在第二阶段中,某个“IIS Web 应用管理”任务会在 VM 上创建 IIS 网站。In the second phase, an IIS Web App Manage task creates an IIS website on the VM. 在此阶段,还会创建另一个用于部署站点的“IIS Web 应用部署”任务。A second IIS Web App Deploy task is created to deploy the site.
  6. 在右侧选择“查看发布”,以显示发布历史记录。At the right, select View releases to display a history of releases.

  7. 选择某个发布旁边的省略号 (...),然后选择“打开”。Select the ellipsis (...) next to a release, and then select Open.
    可以浏览多个菜单,例如发布摘要、关联的工作项和测试。You can explore several menus, such as a release summary, associated work items, and tests.

  8. 选择“提交”。Select Commits.
    此视图显示与此部署关联的代码提交。This view shows code commits that are associated with this deployment. 请对发布进行比较,查看不同部署之间的提交差异。Compare releases to view the commit differences between deployments.

  9. 选择“日志”。Select Logs.
    日志包含有关部署过程的有用信息。The logs contain useful information about the deployment process. 可以在部署期间和之后查看日志。You can view them both during and after deployments.

提交对 Azure Repos 所做的更改并将其自动部署到 AzureCommit changes to Azure Repos and automatically deploy them to Azure

现在,可以使用可将最新工作部署到网站的 CI/CD 过程,与某个团队展开应用协作。You're now ready to collaborate with a team on your app by using a CI/CD process that automatically deploys your latest work to your website. 每次对 Git 存储库进行更改都会在 Azure DevOps 中启动一个生成,CD 管道可以执行到 Azure 的部署。Each change to the Git repo starts a build in Azure DevOps, and a CD pipeline executes a deployment to Azure. 遵循本部分所述的过程,或使用其他方法提交对存储库所做的更改。Follow the procedure in this section, or use another technique to commit changes to your repo. 代码更改会启动 CI/CD 过程,并自动在 Azure VM 上部署对 IIS 网站所做的更改。The code changes initiate the CI/CD process and automatically deploy your changes to the IIS website on the Azure VM.

  1. 在左窗格中选择“代码”,然后转到你的存储库。In the left pane, select Code, and then go to your repo.

  2. 转到 Views\Home 目录,选择 Index.cshtml 文件旁边的省略号 (...),然后选择“编辑”。Go to the Views\Home directory, select the ellipsis (...) next to the Index.cshtml file, and then select Edit.

  3. 对该文件进行更改,例如,在某个 div 标记内部添加一些文本。Make a change to the file, such as adding some text within one of the div tags.

  4. 在右上角选择“提交”,然后再次选择“提交”以推送更改。At the top right, select Commit, and then select Commit again to push your change.
    片刻之后,某个生成会在 Azure DevOps 中启动,并且某个发布将会执行以部署更改。After a few moments, a build starts in Azure DevOps and a release executes to deploy the changes. 使用 Azure DevOps 组织通过 DevOps Projects 仪表板或浏览器监视生成状态。Monitor the build status in the DevOps Projects dashboard or in the browser with your Azure DevOps organization.

  5. 发布完成后,刷新应用程序以验证更改。After the release is completed, refresh your application to verify your changes.

配置 Azure Application Insights 监视Configure Azure Application Insights monitoring

可以使用 Azure Application Insights 轻松监视应用程序的性能和使用情况。With Azure Application insights, you can easily monitor your application's performance and usage. DevOps Projects 会自动为应用程序配置 Application Insights 资源。DevOps Projects automatically configures an Application Insights resource for your application. 你可以根据需要进一步配置各种警报和监视功能。You can further configure various alerts and monitoring capabilities as needed.

  1. 在 Azure 门户中,转到 DevOps Projects 仪表板。In the Azure portal, go to the DevOps Projects dashboard.

  2. 在右下角,选择应用对应的“Application Insights”链接。At the lower right, select the Application Insights link for your app.
    此时会打开“Application Insights”窗格。The Application Insights pane opens. 此视图包含应用的使用情况、性能和可用性监视信息。This view contains usage, performance, and availability monitoring information for your app.

    “Application Insights”窗格

  3. 依次选择“时间范围”、“过去一小时”。Select Time range, and then select Last hour. 若要筛选结果,请选择“更新”。To filter the results, select Update.
    现在可以查看过去 60 分钟的所有活动。You can now view all activity from the last 60 minutes.

  4. 若要退出时间范围,请选择“x”。To exit the time range, select x.

  5. 依次选择“警报”、“添加指标警报”。Select Alerts, and then select Add metric alert.

  6. 输入警报的名称。Enter a name for the alert.

  7. 在“指标”下拉列表中,检查各种警报指标。In the Metric drop-down list, examine the various alert metrics.
    默认警报针对服务器响应时间大于 1 秒的情况。The default alert is for a server response time greater than 1 second. 可以轻松地配置各种警报,以便改进应用的监视功能。You can easily configure a variety of alerts to improve the monitoring capabilities of your app.

  8. 选中“通过电子邮件通知所有者、参与者和读取者”复选框。Select the Notify via Email owners, contributors, and readers check box.
    (可选)可以在显示警报时,通过执行 Azure 逻辑应用来执行其他操作。Optionally, you can perform additional actions when an alert is displayed by executing an Azure logic app.

  9. 选择“确定”以创建警报。Select OK to create the alert.
    片刻之后,警报就会在仪表板上显示为活动状态。After a few moments, the alert appears as active on the dashboard.

  10. 退出“警报”区域,返回到“Application Insights”窗格。Exit the Alerts area, and go back to the Application Insights pane.

  11. 依次选择“可用性”、“添加测试”。Select Availability, and then select Add test.

  12. 输入测试名称,然后选择“创建”。Enter a test name, and then select Create.
    此时会创建简单的 ping 测试来验证应用程序的可用性。A simple ping test is created to verify the availability of your application. 数分钟后,测试结果可用,此时 Application Insights 仪表板会显示可用性状态。After a few minutes, test results are available, and the Application Insights dashboard displays an availability status.

清理资源Clean up resources

测试后,可以清理资源,以避免产生费用。If you are testing, you can avoid accruing billing charges by cleaning up your resources. 不再需要本教程中创建的 Azure 虚拟机和相关资源时,可将其删除。When they are no longer needed, you can delete the Azure virtual machine and related resources that you created in this tutorial. 为此,可以使用 DevOps Projects 仪表板上的“删除”功能。To do so, use the Delete functionality on the DevOps Project dashboard.

重要

以下过程会永久删除资源。The following procedure permanently deletes resources. “删除”功能会销毁 DevOps Projects 中的项目在 Azure 和 Azure DevOps 中创建的数据,删除后无法检索这些数据。The Delete functionality destroys the data that's created by the project in DevOps Projects in both Azure and Azure DevOps, and you will be unable to retrieve it. 请只在仔细阅读提示后才使用此过程。Use this procedure only after you've carefully read the prompts.

  1. 在 Azure 门户中,转到 DevOps Projects 仪表板。In the Azure portal, go to the DevOps Projects dashboard.
  2. 在右上角选择“删除”。At the top right, select Delete.
  3. 出现提示时,请选择“是”以永久删除这些资源。At the prompt, select Yes to permanently delete the resources.

可以选择根据所在团队的需求修改这些生成和发布管道。You can optionally modify these build and release pipelines to meet the needs of your team. 也可以将此 CI/CD 模式用作其他管道的模板。You can also use this CI/CD pattern as a template for your other pipelines.

后续步骤Next steps

本教程介绍了如何:In this tutorial, you learned how to:

  • 使用 DevOps Projects 部署 ASP.NET 应用Use DevOps Projects to deploy your ASP.NET app
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 检查 CI 管道Examine the CI pipeline
  • 检查 CD 管道Examine the CD pipeline
  • 提交对 Azure Repos 所做的更改并将其自动部署到 AzureCommit changes to Azure Repos and automatically deploy them to Azure
  • 配置 Azure Application Insights 监视Configure Azure Application Insights monitoring
  • 清理资源Clean up resources

若要详细了解 CI/CD 管道,请参阅:To learn more about the CI/CD pipeline, see: