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

使用 Azure DevOps Projects 创建用于 Node.js 的 CI/CD 管道Create a CI/CD pipeline for Node.js with Azure DevOps Projects

Azure DevOps Projects 提供一种简化的体验,适合在 Azure Pipelines 中为 Node.js 应用创建 Azure 资源并设置持续集成 (CI) 和持续交付 (CD) 管道。Azure DevOps Projects presents a simplified experience that creates Azure resources and sets up a continuous integration (CI) and continuous delivery (CD) pipeline for your Node.js app 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 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 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, and then search for DevOps Projects.

    开始“持续交付”配置

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

  1. 选择 Node.js 示例应用程序。Select the Node.js sample application.
    Node.js 示例包括一个选项,可以从多个应用程序框架中进行选择。The Node.js samples include a choice of several application frameworks.

  2. 默认的示例框架为 Express.js。The default sample framework is Express.js. 保留默认设置,然后选择“下一步”。Leave the default setting, and then select Next.
    “Windows 上的 Web 应用”是默认的部署目标。Web App on Windows is the default deployment target. 以前选择的应用程序框架规定了此处提供的 Azure 服务部署目标的类型。The application framework, which you chose previously, dictates the type of Azure service deployment target available here.

  3. 保留默认服务,然后选择“下一步”。Leave the default 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 choose an existing organization.

    a.在“解决方案资源管理器”中,右键单击项目文件夹下的“引用”文件夹,并单击“添加引用”。a. 为项目选择一个名称。Choose a name for your project.

    b.b. 依次选择 Azure 订阅和位置、应用程序的名称、“完成”。Select your Azure subscription and location, choose 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 repository in your Azure DevOps organization, a build is executed, and your application is deployed to Azure. 可以通过此仪表板查看代码存储库、CI/CD 管道以及 Azure 中的应用程序。This dashboard provides visibility into your code repository, the CI/CD pipeline, and your application in Azure.

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

    仪表板视图

DevOps Projects 自动配置了 CI 生成和发布触发器。DevOps Projects automatically configured a CI build and release trigger. 现在可以与 Node.js 应用团队协作,通过 CI/CD 过程自动将最新工作部署到网站。You're now ready to collaborate with a team on a Node.js app with a CI/CD process that automatically deploys your latest work to your web site.

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

DevOps Projects 在 Azure Repos 或 GitHub 中创建一个 Git 存储库。DevOps Projects creates a Git repository in Azure Repos or GitHub. 请执行以下步骤,以便查看存储库并对应用程序进行代码更改。Take the following steps to view the repository and make code changes to your application.

  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. 若要查看存储库克隆 URL,请在浏览器右上角选择“克隆”。To view the repository clone URL, select Clone on the top right of the browser.
    可以在最常用的 IDE 中克隆 Git 存储库。You can clone your Git repository in your favorite IDE. 在后续几个步骤中,可以使用 Web 浏览器直接对 master 分库进行代码更改并提交所做的更改。In the next few steps, you can use the web browser to make and commit code changes directly to the master branch.

  3. 在浏览器左侧转到 views/index.pug 文件。On the left side of the browser, go to the views/index.pug file.

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

  5. 选择“提交”并保存更改。Select Commit, and then save your changes.

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

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

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

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

  2. 指向“状态”字段,然后选择省略号 (...)。Point to the Status field, and then 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 a variety of tasks such as fetching sources from the Git repository, restoring dependencies, and publishing outputs 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, and 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 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

当你配置 CI/CD 过程时,系统自动创建了生成和发布管道。When you configured your CI/CD process, build and release pipelines were automatically created. 可以根据你的团队的需求修改这些生成和发布管道。You can modify these build and release pipelines to meet the needs of your team. 若要详细了解 CI/CD 管道,请参阅:To learn more about the CI/CD pipeline, see:

视频Videos