练习 - 使用代理生成应用程序

已完成

现在,生成代理正在运行并可接收生成作业,我们看看它的实际效果。 在本单元中,你将修改我们提供的基本生成配置,以使用你自己的代理而不是 Microsoft 托管代理来生成 Space Game 网站

注意

在执行上一个模块创建在 Azure 上运行的生成代理中的步骤后立即运行以下步骤。

在本单元结束时,可从 Microsoft Azure DevOps 组织中删除代理池(可选步骤)。

从 GitHub 中提取分支

在本部分中,你将从 GitHub 提取 build-agent 分支,签出或切换到该分支。

此分支包含了之前模块中使用的 Space Game 项目,还有一个便于开始操作的 Azure Pipelines 配置。

  1. 在 Visual Studio Code 中打开集成终端。

  2. 要从 Microsoft 的存储库中下载名为 build-agent 的分支,并切换到该分支,请运行以下 git fetchgit checkout 命令:

    git fetch upstream build-agent
    git checkout -B build-agent upstream/build-agent
    

    回想一下,“upstream”指的是 Microsoft GitHub 存储库。 项目的 Git 配置能够识别上游远程库,因为当你从 Microsoft 的存储库中创建该项目的分支并在本地克隆它时,就建立了这种关系。

    稍后,你会将此分支推送到 GitHub 存储库(称作 origin)。

  3. 可选择在 Visual Studio Code 中打开 azure-pipelines.yml 文件,熟悉初始配置

    该配置与你在使用 Azure Pipelines 创建生成管道模块中创建的基本配置类似。 它只生成应用程序的发布配置。

修改生成配置

在本部分中,你将修改生成配置,从使用 Microsoft 托管代理切换到使用自己代理池中的代理。

  1. 在 Visual Studio Code 中,打开 azure-pipelines.yml 文件,然后查找 pool 部分

    pool:
      vmImage: 'ubuntu-20.04'
      demands:
      - npm
    
  2. 修改 pool 部分,如下所示:

    pool:
      name: 'MyAgentPool'
      demands:
      - npm
    

    此版本使用 name 指定你的代理池,即“MyAgentPool”。 它保持 demands 部分不变,指定生成代理必须安装 Node.js 包管理器 npm。

  3. 在集成终端中,将 azure-pipelines.yml 添加到索引,提交更改,然后将分支推送到 GitHub。

    git add azure-pipelines.yml
    git commit -m "Use private agent pool"
    git push origin build-agent
    

观察 Azure Pipelines 使用生成代理

观察该生成在管道中使用生成代理运行。

  1. 在 Azure DevOps 中,转到“Space Game - Web - Agent”项目。

  2. 在“项目”页或左窗格中,选择“Pipelines”。

  3. 从“最近运行的管道”中选择你的管道,然后选择最近的运行(当你更新管道以使用 MyAgentPool 池时启动)。

  4. 选择“作业”并跟踪每个步骤的运行。

    从“Initialize job”任务中,可以看到该生成使用了生成代理。

    A screenshot of Azure Pipelines running the build. The Initialize job task shows that it's running the build on the private agent named MyLinxuAgent.

可选:删除生成池

为方便将来参考,可将生成池配置保留在你的 Azure DevOps 组织中,但请记住,在本模块结束时执行清理步骤后,托管代理的 VM 将不再可用。

事实上,Azure DevOps 会检测代理是否处于脱机状态。 当下次使用“MyAgentPool”池将某个生成排入队列时,Azure Pipelines 会检查是否有可用代理。

A screenshot of the agent pool in Azure DevOps showing that the build agent is offline.

可选择从 Azure DevOps 中删除生成池配置。 以下是操作方法:

  1. 在 Azure DevOps 中,转到“Space Game - Web - Agent”项目。

  2. 选择“Project settings”。

  3. 在“管道”下,选择“代理池”

    A screenshot of the project settings in Azure DevOps showing the location of the Agent pools menu item.

  4. 在 MyAgentPool 下,选择垃圾桶图标,然后选择“Delete”

    A screenshot of Azure DevOps showing the location of where to remove the agent from the agent pool.