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

教程:使用 Azure DevOps Projects 为现有的代码创建 CI/CD 管道Tutorial: Create a CI/CD pipeline for your existing code 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.

将能够:You will:

  • 使用 DevOps Projects 创建 CI/CD 管道Use DevOps Projects to create a CI/CD pipeline
  • 配置对 GitHub 存储库的访问权限并选择一个框架Configure access to your GitHub repo and choose a framework
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 提交对 GitHub 所做的更改并将其自动部署到 AzureCommit changes to GitHub and automatically deploy them to Azure
  • 检查 Azure Pipelines CI/CD 管道Examine the Azure Pipelines CI/CD pipeline
  • 配置 Azure Application Insights 监视Configure Azure Application Insights monitoring
  • 清理资源Clean up resources

先决条件Prerequisites

  • Azure 订阅。An Azure subscription. 可以通过 Visual Studio Dev Essentials 免费获取一个。You can get one free through Visual Studio Dev Essentials.
  • 访问 GitHub 或外部 Git 存储库,其中包含 .NET、Java、PHP、Node、Python 或静态 Web 代码。Access to a GitHub or external Git repo that contains .NET, Java, PHP, Node, Python, or static web code.

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

Azure DevOps Projects 在 Azure Pipelines 中创建 CI/CD 管道。Azure 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. Azure DevOps Projects 还可以在所选的 Azure 订阅中创建 Azure 资源。Azure DevOps Projects also creates Azure resources 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. 选择“引入自己的代码”,然后选择“下一步”。Select Bring your own code, and then select Next.

配置对 GitHub 存储库的访问权限并选择一个框架Configure access to your GitHub repo and choose a framework

  1. 选择 GitHub 或外部 Git 存储库,然后选择你的存储库以及包含应用程序的分库。Select either GitHub or an external Git repo, and then select your repo and the branch that contains your application.

  2. 选择 Web 框架,然后选择“下一步”。Select your web framework, and then select Next.

    .NET Framework

    以前选择的应用程序框架规定了此处提供的 Azure 服务部署目标的类型。The application framework, which you chose previously, dictates the type of Azure service deployment target that's available here.

  3. 选择目标服务,然后选择“下一步”。Select the target service, 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.

    a.在“解决方案资源管理器”中,右键单击项目文件夹下的“引用”文件夹,并单击“添加引用”。a. 输入 Azure DevOps 中项目的名称。Enter a name for your project in Azure DevOps.

    b.b. 选择 Azure 订阅和位置,输入应用程序的名称,然后选择“完成”。Select your Azure subscription and location, enter a name for your application, and then select Done.

    几分钟后,DevOps Projects 仪表板会显示在 Azure 门户中。After a few minutes, the DevOps Projects dashboard is displayed in the Azure portal. 首先会在 Azure DevOps 组织的存储库中设置示例应用程序,然后执行生成并将应用程序部署到 Azure。A sample application is set up in a repo in your Azure DevOps organization, a build is executed, and your application is deployed to Azure. 可以通过此仪表板查看 GitHub 代码存储库、CI/CD 管道以及 Azure 中的应用程序。This dashboard provides visibility into your GitHub code repo, the CI/CD pipeline, and your application in Azure.

  2. 选择“浏览”,查看正在运行的应用程序。Select Browse to view your running application.

    DevOps Projects 仪表板视图

Azure DevOps Projects 会自动配置 CI 生成和发布触发器。Azure DevOps Projects automatically configures a CI build and release trigger. 代码保留在 GitHub 存储库或其他外部存储库中。Your code remains in your GitHub repo or another external repo.

提交对 GitHub 所做的更改并将其自动部署到 AzureCommit changes to GitHub 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. 每次对 GitHub 存储库进行更改都会在 Azure DevOps 中启动一个生成,CD 管道可以执行到 Azure 的部署。Each change to the GitHub repo starts a build in Azure DevOps, and a CD pipeline executes a deployment to Azure.

  1. 对应用程序进行更改,然后将更改提交到 GitHub 存储库。Make a change to your application, and then commit the change to your GitHub repo.
    片刻之后,系统会在 Azure Pipelines 中启动生成。After a few moments, a build starts in Azure Pipelines. 可以使用 Azure DevOps 组织通过 DevOps Projects 仪表板或浏览器监视生成状态。You can monitor the build status in the DevOps Projects dashboard, or you can monitor it in the browser with your Azure DevOps organization.

  2. 生成完成后,刷新应用程序以验证更改。After the build is completed, refresh your application to verify your changes.

检查 Azure Pipelines CI/CD 管道Examine the Azure Pipelines CI/CD pipeline

Azure DevOps Projects 会自动在 Azure Pipelines 中配置一个 CI/CD 管道。Azure DevOps Projects automatically configures a CI/CD pipeline in Azure Pipelines. 根据需要浏览和自定义管道。Explore and customize the pipeline as needed. 若要了解生成和发布管道,请执行以下操作:To familiarize yourself with the build and release pipelines, 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.
    此时会看到最近针对生成所做的更改的审核线索。You see 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.
    Azure DevOps Projects 会自动创建一个 CI 触发器,每次向存储库提交内容都会启动新的生成。Azure 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.

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

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

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

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

  14. 在左侧,选择“任务”。At the left, select Tasks.
    任务是部署过程执行的活动。Tasks are the activities that your deployment process executes. 在此示例中,已创建一个用于将项目部署到 Azure 应用服务的任务。In this example, a task was created to deploy to the Azure App service.

  15. 在右侧选择“查看发布”,以显示发布历史记录。At the right, select View releases to display a history of releases.

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

  17. 选择“提交”。Select Commits.
    此视图显示与此部署关联的代码提交。This view shows code commits that are associated with this deployment.

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

配置 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. Azure DevOps Projects 会自动为应用程序配置 Application Insights 资源。Azure 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. 若要退出时间范围,请选择“x”。To exit the time range, select x.

  4. 选择“警报”,然后选择“添加指标警报”。Select Alerts, and then select Add metric alert.

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

  6. 在“源警报触发条件”下拉列表中,选择你的应用服务资源In the Source Alter on drop-down list, select your App Service resource.

  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

不再需要本教程中创建的 Azure 应用服务和相关资源时,可将其删除。When they are no longer needed, you can delete the Azure App service and related resources that you created in this tutorial. 请使用 DevOps Projects 仪表板上的“删除”功能来这样做。To do so, use the Delete functionality on the DevOps Projects dashboard.

后续步骤Next steps

当你在本教程中配置 CI/CD 过程后,系统已在 Azure DevOps Projects 中自动创建了生成和发布管道。When you configured your CI/CD process in this tutorial, a build and release pipeline were automatically created in Azure DevOps Projects. 可以根据你的团队的需求修改这些生成和发布管道。You can modify these build and release pipelines to meet the needs of your team. 你已了解如何:You learned how to:

  • 使用 DevOps Projects 创建 CI/CD 管道Use DevOps Projects to create a CI/CD pipeline
  • 配置对 GitHub 存储库的访问权限并选择一个框架Configure access to your GitHub repo and choose a framework
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 提交对 GitHub 所做的更改并将其自动部署到 AzureCommit changes to GitHub and automatically deploy them to Azure
  • 检查 Azure Pipelines CI/CD 管道Examine the Azure Pipelines CI/CD pipeline
  • 配置 Azure Application Insights 监视Configure Azure Application Insights monitoring
  • 清理资源Clean up resources

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