练习 - 创建管道

已完成

此时,Mara 已为 Space Game 网站定义了生成配置。 现在轮到你了。 你要做的是创建管道并生成第一个生成工件。

如你所见,Mara 使用 YAML 文件定义生成。 创建管道时,进程会提示你提供 YAML 文件。 项目还没有此文件。

如果没有为项目提供初始 YAML 文件,Azure Pipelines 可根据你的应用类型为你创建一个该文件。 在这里,你将生成一个 ASP.NET Core 应用,但 Azure Pipelines 也为其他项目类型(包括 Java、Go 等)提供了入门生成配置。

创建管道

  1. 在 Azure DevOps 中,转到你的项目。

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

  3. 选择“创建管道(如果这不是项目中的第一个管道,则选择“新建管道”)。

  4. 在“Connect”选项卡上,选择“GitHub”。

    系统出现提示时,请输入 GitHub 凭据。

  5. 在“Select”选项卡上,选择“mslearn-tailspin-spacegame-web”存储库。

  6. 若要安装 Azure Pipelines 应用,系统可能会将你重定向到 GitHub。 如果是这样,请滚动到底部,选择“批准并安装”

  7. 在“Configure”选项卡上,选择“ASP.NET Core”。

    注意

    如果看不到此选项,请选择“Show more”。 不要选择“ASP.NET Core (.NET Framework)”。

    Screenshot of locating ASP.NET Core from the list of provided application types.

  8. 记下“Review”选项卡上的初始生成配置。

    Screenshot of Azure Pipelines showing the initial build configuration.

    这是 Azure DevOps 根据你的应用类型 ASP.NET Core 为你提供的一个非常基本的配置。 默认配置使用 Microsoft 托管代理。

    将文本 vmImage: ubuntu-latest 替换为 name: Default(如果在设置 Codespaces 存储库机密时指定了其他池,则替换为代理池的名称)。

  9. 在“Review”选项卡上,选择“Save and run”。 若要将更改提交到 GitHub 并启动管道,请选择“直接提交到主分支”,然后再次选择“保存并运行”。 如果系统提示使用 This pipeline needs permission to access a resource before this run can continue 之类的消息授予权限,请选择“查看”并按照提示允许访问。

监视管道运行

在“Jobs”下选择“Job”。 接下来,跟踪生成过程的每个步骤。 若要在生成完成后查看文本文件形式的作业输出,还可以选择“查看原始日志”。

如果管道无法快速启动,请验证 Codespaces 是否仍在运行。 Codespaces 将在 30 分钟后关闭,可能需要重新启动。

如果管道状态仍为“已排队”,并且片刻后未转换为“正在运行”请检查并行作业并请求免费授权。 如果你无权访问并行作业,可以使用 Codespaces 启动模块。

在此处你可看到生成定义创建的步骤。 它会准备 VM、从 GitHub 提取最新源代码,然后生成应用。

Screenshot of Azure Pipelines showing output from the initial build configuration.

此配置是一个很好的开始,因为现在有了添加生成任务的位置。 但是,需要对其进行更新来满足 Tailspin 团队的需求,例如缩小 JavaScript 和 CSS 文件。

提示

查看电子邮件。 你可能已收到包含运行结果的生成通知。 可以使用这些通知让团队成员了解生成的完成时间以及各项生成的成败。

添加生成任务

现在,你已拥有可用的生成过程,可以开始添加生成任务。

请注意,当前使用的是 main 分支。 若要保留你的工作,现在需要创建一个名为 build-pipeline 的分支。 该分支提供了一个试验位置,使生成完全不会影响团队的其他成员。

可以直接从 Azure Pipelines 向 azure-pipelines.yml 添加生成任务。 Azure Pipelines 将更改直接提交到分支。 在此处,你将在本地更改 azure-pipelines.yml,并将更改推送或上传到 GitHub。 这样做可以练习 Git 技能。 推送更改时,查看管道自动生成应用。

在实际操作中,你可能会一次添加一个生成任务,推送所做的更改,并查看生成运行情况。 在这里,你将一次添加我们之前标识的所有生成任务。

备注

你即将运行一些 Git 命令。 如果不太熟悉 Git,也无需担心。 我们将演示具体的操作步骤。 还会在未来的模块中详细介绍 Git 相关内容。

  1. 在 Visual Studio Code 中,转到集成终端。 确保转到存储库中的 main 分支,然后完成这些步骤。

  2. 运行以下 git pull 命令,从 GitHub 提取最新更改并更新 main 分支。

    git pull origin main
    

    从输出中可以看到,Git 提取了一个名为 azure-pipelines.yml 的文件。 这是 Azure Pipelines 为你创建的起始管道配置。 设置管道时,Azure Pipelines 会将此文件添加到 GitHub 存储库。

  3. 运行 git checkout 命令,创建名为 build-pipeline 的分支:

    git checkout -B build-pipeline
    
  4. 在 Visual Studio Code 中,按如下所示更改 azure-pipelines.yml:

    trigger:
    - '*'
    
    pool:
      name: 'Default' # Replace Default with the name of your agent pool if you used a different pool
    
    variables:
      buildConfiguration: 'Release'
    
    steps:
    - task: UseDotNet@2
      displayName: 'Use .NET SDK 6.x'
      inputs:
        packageType: sdk
        version: '6.x'
    
    - task: Npm@1
      displayName: 'Run npm install'
      inputs:
        verbose: false
    
    - script: './node_modules/.bin/node-sass Tailspin.SpaceGame.Web/wwwroot --output Tailspin.SpaceGame.Web/wwwroot'
      displayName: 'Compile Sass assets'
    
    - task: gulp@1
      displayName: 'Run gulp tasks'
    
    - script: 'echo "$(Build.DefinitionName), $(Build.BuildId), $(Build.BuildNumber)" > buildinfo.txt'
      displayName: 'Write build info'
      workingDirectory: Tailspin.SpaceGame.Web/wwwroot
    
    - task: DotNetCoreCLI@2
      displayName: 'Restore project dependencies'
      inputs:
        command: 'restore'
        projects: '**/*.csproj'
    
    - task: DotNetCoreCLI@2
      displayName: 'Build the project - Release'
      inputs:
        command: 'build'
        arguments: '--no-restore --configuration Release'
        projects: '**/*.csproj'
    
    trigger:
    - '*'
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      buildConfiguration: 'Release'
    
    steps:
    - task: UseDotNet@2
      displayName: 'Use .NET SDK 6.x'
      inputs:
        packageType: sdk
        version: '6.x'
    
    - task: Npm@1
      displayName: 'Run npm install'
      inputs:
        verbose: false
    
    - script: './node_modules/.bin/node-sass Tailspin.SpaceGame.Web/wwwroot --output Tailspin.SpaceGame.Web/wwwroot'
      displayName: 'Compile Sass assets'
    
    - task: gulp@1
      displayName: 'Run gulp tasks'
    
    - script: 'echo "$(Build.DefinitionName), $(Build.BuildId), $(Build.BuildNumber)" > buildinfo.txt'
      displayName: 'Write build info'
      workingDirectory: Tailspin.SpaceGame.Web/wwwroot
    
    - task: DotNetCoreCLI@2
      displayName: 'Restore project dependencies'
      inputs:
        command: 'restore'
        projects: '**/*.csproj'
    
    - task: DotNetCoreCLI@2
      displayName: 'Build the project - Release'
      inputs:
        command: 'build'
        arguments: '--no-restore --configuration Release'
        projects: '**/*.csproj'
    

    steps 部分下,可以看到映射到我们之前标识的每个脚本命令的生成任务。

    Azure Pipelines 提供了映射到许多常见生成活动的内置生成任务。 例如,DotNetCoreCLI@2 任务映射到 dotnet 命令行实用工具。 管道使用 DotNetCoreCLI@2 两次:一次用于还原或安装项目的依赖项,一次用于生成项目。

    请注意,并非所有生成活动都映射到内置任务。 例如,没有任何内置任务运行 node-sass 实用工具或将生成信息写入到文本文件。 若要运行常规系统命令,请使用 CmdLine@2script 任务。 该管道使用 script 任务,因为它是 CmdLine@2 的常见快捷方式。

    在将有关生成的信息写入文件的生成步骤中,请注意以下元素:

    • $(Build.DefinitionName)
    • $(Build.BuildId)
    • $(Build.BuildNumber)

    这些元素是系统提供的内置变量,可在管道中使用:

    • $(Build.DefinitionName) 是生成管道的名称。 例如“SpaceGame-Web-CI”。
    • $(Build.BuildId) 是已完成的生成的数字标识符,例如 115。
    • $(Build.BuildNumber) 是已完成的生成的名称。 可以配置格式,但默认情况下,生成号包括当前日期,后跟当天的生成号。 例如,生成号为“20190329.1”。

    也可以定义自己的变量,此操作很快就可以完成。

    你可能还注意到第一个生成步骤是 UseDotNet@2 任务。 Mara 记得生成脚本没有安装所需的生成工具。 尽管生成代理附带了多个 .NET SDK 版本,但此任务使管道作者能够轻松指定需要在生成代理上使用的版本。

  5. 从集成终端运行以下 Git 命令可将 azure-pipelines.yml 添加到索引,提交更改,并将更改推送到 GitHub。 这些步骤类似于之前执行的步骤。

    提示

    请记住在运行这些 Git 命令之前保存 azure-pipelines.yml。

    git add azure-pipelines.yml
    git commit -m "Add build tasks"
    git push origin build-pipeline
    

    这一次,将 build-pipeline 分支(而不是 main 分支)推送到 GitHub。

    将分支推送到 GitHub 会触发 Azure Pipelines 中的生成过程。

  6. 在 Azure Pipelines 中,转到生成。 为此,请在页面一侧选择“Pipelines”,然后选择你的管道。 你会看到提交消息,还会看到生成正在使用 build-pipeline 分支中的代码运行。

    Screenshot of Azure Pipelines showing the run history, including the branch you recently pushed to GitHub.

    提示

    如果没有立即看到该生成,请稍等片刻或刷新页面。

  7. 选择生成,然后选择“作业”并在生成任务运行时跟踪它们。

    例如,运行 gulp@1 任务以执行缩小 JavaScript 和 CSS 资产的 gulp 任务时会发生以下情况:

    Screenshot of tracing the gulp tasks in Azure Pipelines.

    如果任何步骤失败,你都会在输出中看到错误,以便诊断和修复失败。

    之前,你运行了更小的生成配置。 这一次,生成完成后,你可看到生成应用所需的一组更完整的任务。

    Screenshot of Azure Pipelines showing the complete list of build tasks.

  8. 完成生成后,请选择任何步骤来查看生成的总体进度。 可以从此处跳转到生成日志或 GitHub 上的相关更改。

    Screenshot of Azure Pipelines showing the complete list of build tasks. The Run gulp task is selected.