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

通过 Azure DevOps Projects 创建用于 .NET 的 CI/CD 管道Create a CI/CD pipeline for .NET with Azure DevOps Projects

使用 DevOps Projects 为 .NET Core 或 ASP.NET 应用程序配置持续集成 (CI) 和持续交付 (CD)。Configure continuous integration (CI) and continuous delivery (CD) for your .NET core or ASP.NET application with DevOps Projects. DevOps Projects 可以简化 Azure Pipelines 中生成和发布管道的初始配置。DevOps Projects simplifies the initial configuration of a build and release pipeline in Azure Pipelines.

如果没有 Azure 订阅,可以通过 Visual Studio Dev Essentials 免费获取一个。If you don't have an Azure subscription, you can get one free through Visual Studio Dev Essentials.

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

DevOps Projects 在 Azure DevOps 中创建 CI/CD 管道。DevOps Projects creates a CI/CD pipeline in Azure DevOps. 可以创建新的 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 in the Azure subscription of your choice.

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

  2. 在左窗格的左导航栏中选择“创建资源”图标,然后搜索“DevOps Projects”。 In the left pane, select Create a resource icon in the left navigation bar, and then search for DevOps Projects.

  3. 选择“创建” 。Select Create.

    启动持续交付

选择示例应用程序和 Azure 服务Select a sample application and Azure service

  1. 选择 .NET 示例应用程序。Select the .NET sample application. .NET 示例包括选择开源 ASP.NET 框架或跨平台 .NET Core 框架。The .NET samples include a choice of either the open-source ASP.NET framework or the cross-platform .NET Core framework.

    .NET Framework

  2. 此示例是一个 ASP.NET Core MVC 应用程序。This sample is an ASP.NET Core MVC application. 选择 .NET Core 应用程序框架,然后选择“下一步” 。Select the .NET Core application framework, then select Next.

  3. 选择“Windows Web 应用” 作为部署目标,然后选择“下一步” 。Select Windows Web App as a deployment target, then select Next. (可选)可以为部署选择其他 Azure 服务。Optionally, you can choose other Azure services for your deployment. 以前选择的应用程序框架规定了此处提供的 Azure 服务部署目标的类型。The application framework, which you chose previously, dictates the type of Azure service deployment target's available here.

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

  1. 输入项目名称Enter a Project name.

  2. 创建新的免费 Azure DevOps 组织或从下拉列表中选择现有组织。Create a new free Azure DevOps Organization or choose an existing organization from the dropdown.

  3. 选择 Azure 订阅,输入 Web 应用的名称或采用默认值,然后选择“完成” 。Select your Azure Subscription, enter a name for your Web app or take the default, then select Done. 几分钟后,Azure 门户中会显示 DevOps Projects 部署概述。After a few minutes, the DevOps Projects Deployment Overview is displayed in the Azure portal.

  4. 选择“转到资源” 以查看 DevOps Projects 仪表板。Select Go to resource to view the DevOps Project dashboard. 在右上角,将“项目” 固定在仪表板上,以便快速访问。In the upper right corner, pin the Project to your dashboard for quick access. 示例应用设置在 Azure DevOps 组织的存储库中。A sample app is set up in a repo in your Azure DevOps Organization. 执行生成,并将应用部署到 Azure。A build is executed, and your app is deployed to Azure.

  5. 在此仪表板中可以查看代码存储库、CI/CD 管道,以及 Azure 中的应用。The dashboard provides visibility into your code repo, your CI/CD pipeline, and your app in Azure. 在右侧的“Azure 资源”下,选择“浏览”以查看正在运行的应用。 At the right under Azure resources, select Browse to view your running app.

    仪表板视图

提交代码更改并执行 CI/CDCommit code changes and execute CI/CD

DevOps Projects 在 Azure Repos 或 GitHub 中创建了一个 Git 存储库。DevOps Projects created a Git repository in Azure Repos or GitHub. 若要查看存储库并对应用程序进行代码更改,请执行以下操作:To view the repository and make code changes to your application, do the following:

  1. 在 DevOps Projects 仪表板的左侧,选择 master 分库的链接。On the left of the DevOps Projects dashboard, select the link for your master branch. 该链接会打开新建 Git 存储库的视图。This link opens a view to the newly created Git repository.

  2. 在后续几个步骤中,可以使用 Web 浏览器直接对 master 分支进行代码更改并提交所做的更改。In the next few steps, you can use the web browser to make and commit code changes directly to the master branch. 还可以通过从存储库页右上角选择“克隆” ,在喜欢的 IDE 中克隆 Git 存储库。You can also clone your Git repository in your favorite IDE by selecting Clone from the top right of the repository page.

  3. 在左侧,将应用程序文件结构导航到 Application/aspnet-core-dotnet-core/Pages/Index.cshtmlOn the left, navigate the application file structure to Application/aspnet-core-dotnet-core/Pages/Index.cshtml.

  4. 选择“编辑”, 然后对 h2 标题进行更改。Select Edit, and then make a change to the h2 heading. 例如,键入“Azure DevOps Projects 快速入门”或进行一些其他的更改。 For example, type Get started right away with the Azure DevOps Projects or make some other change.

    代码编辑

  5. 选择“提交” ,留下注释,然后再次选择“提交” 。Select Commit, leave a comment and select Commit again.

  6. 在浏览器中,转到“Azure DevOps Projects”仪表板。In your browser, go to the Azure DevOps Project dashboard. 此时会看到一个生成正在进行。You should now see a build is in progress. 所做的更改会自动通过 CI/CD 管道进行生成和部署。The changes you made are automatically built and deployed via a CI/CD pipeline.

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

在上一步中,Azure DevOps Projects 自动配置了一个完整的 CI/CD 管道。In the previous step, Azure DevOps Projects automatically configured a full CI/CD pipeline. 根据需要浏览和自定义管道。Explore and customize the pipeline as needed. 请执行以下步骤,自行熟悉 Azure DevOps 的生成和发布管道。Take the following steps to familiarize yourself with the Azure DevOps build and release pipelines.

  1. 在 DevOps Projects 仪表板顶部,选择“生成管道”。 At the top of the DevOps Projects dashboard, select Build Pipelines.
    此链接会打开一个浏览器标签页,并打开新项目的 Azure DevOps 生成管道。This link opens a browser tab and the Azure DevOps build pipeline for your new project.

  2. 选择省略号 (...)。此操作会打开一个菜单,可以在其中启动多项活动,例如将新生成排队、暂停某个生成、编辑生成管道。Select the ellipsis (...). This action opens a menu where you can start several activities such as queuing 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 repository, restoring dependencies, and publishing outputs used that are 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.
    在“历史记录”窗格中,此时会看到最近针对生成所做的更改的审核线索。 In the History pane, you see an audit trail of your recent changes for the build. Azure Pipelines 会跟踪对生成管道所做的任何更改,并允许进行版本比较。Azure Pipelines keeps track of any changes that are made to the build pipeline, and it allows you to compare versions.

  8. 选择“触发器”。 Select Triggers.
    DevOps Projects 自动创建了一个 CI 触发器,每次向存储库提交内容都会启动新的生成。DevOps Projects automatically created a CI trigger, and every commit to the repository starts a new build. 可以选择在 CI 过程中包括或排除分库。You can optionally 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, then select Releases.
    DevOps Projects 会创建一个发布管道,用于管理到 Azure 的部署。DevOps Projects creates a release pipeline to manage deployments to Azure.

  11. 在左侧,选择发布管道旁边的省略号 (...),然后选择“编辑”。 On the left, 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 used for the artifact.

  13. 在“删除”图标的旁边,选择“持续部署触发器”。 Next to the Drop icon, select the Continuous deployment trigger.
    此发布管道有一个启用的 CD 触发器,每次有新的生成项目可用时,此触发器就会运行部署。This release pipeline has an enabled CD trigger, which runs 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. 在左侧,选择“任务”。 On the left, select Tasks.
    这些任务是部署过程执行的活动。The tasks are the activities that your deployment process performs. 在此示例中,已创建一个用于将项目部署到 Azure 应用服务的任务。In this example, a task was created to deploy to Azure App Service.

  15. 在右侧,选择“查看发布”。 On the right, select View releases. 此视图显示发布历史记录。This view shows a history of releases.

  16. 选择某个发布旁边的省略号 (...),然后选择“打开”。 Select the ellipsis (...) next to one of your releases, 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 the specific deployment.

  18. 选择“日志”。 Select Logs.
    日志包含有关部署过程的有用信息。The logs contain useful information about the deployment process. 日志可以在部署过程中以及部署以后查看。They can be viewed both during and after deployments.

清理资源Clean up resources

不再需要创建的 Azure 应用服务和其他相关资源时,可将其删除。You can delete Azure App Service and other related resources that you created when you don't need them anymore. 请使用 DevOps Projects 仪表板上的“删除”功能。 Use the Delete functionality on the DevOps Projects dashboard.

后续步骤Next steps

若要详细了解如何根据团队的需求修改生成和发布管道,请参阅以下教程:To learn more about modifying the build and release pipelines to meet the needs of your team, see this tutorial:

视频Videos