生成和测试 Ruby 应用

Azure DevOps Services |Azure DevOps Server |Azure DevOps Server 2022 |Azure DevOps Server 2020

了解如何使用 Azure Pipelines 自动生成、测试和部署 Ruby 应用程序。

先决条件

Ruby 预安装在适用于 Linux、macOS 和 Windows 的 Microsoft 托管代理 上。 无需设置更多内容即可生成 Ruby 项目。 若要查看预安装的 Ruby 版本,请参阅 软件

产品 要求
Azure DevOps - Azure DevOps 项目
- 能够在 Microsoft 托管的代理上运行管道。 可以购买 并行作业 ,也可以请求免费层。
- 对 YAML 和 Azure Pipelines 的基本知识。 有关详细信息,请参阅 创建第一个管道
- 权限:
     - 若要创建管道:必须位于 “参与者 ”组中,并且该组需要将 “创建生成管道 ”权限设置为“允许”。 项目管理员组的成员可以管理管道。
    - 若要创建服务连接:必须具有服务连接的管理员创建者角色。
GitHub - GitHub 帐户。
- 用于授权 Azure Pipelines 的 GitHub 服务连接
产品 要求
Azure DevOps - Azure DevOps 项目
- 安装了 Ruby 的自行托管代理。 若要创建一个代理,请参阅自托管代理
- 对 YAML 和 Azure Pipelines 的基本知识。 有关详细信息,请参阅 创建第一个管道
- 权限:
    - 若要创建管道:必须位于 “参与者 ”组中,并且该组需要将 “创建生成管道 ”权限设置为“允许”。 项目管理员组的成员可以管理管道。
    - 若要创建服务连接:必须具有服务连接的管理员创建者角色。
GitHub - GitHub 帐户。
- 用于授权 Azure Pipelines 的 GitHub 服务连接

创建 Azure Pipelines

按照以下步骤为 Ruby 应用设置管道。

  1. 登录到你的 Azure DevOps 组织,并转到你的项目。

  2. 转到 “管道>新建管道”。

  3. 选择 GitHub 作为源代码位置。

    可能会重定向到 GitHub 进行登录。 如果是这样,请输入 GitHub 凭据。

  4. 选择 Ruby 示例存储库。

  5. 选择管道的 Ruby 模板。

  6. 将生成 YAML 文件。 选择“保存”并>提交到主分支,然后选择“保存”并再次运行

  7. 等待管道运行完成。 对于基本 Ruby 项目,这通常需要 2-5 分钟。

现在,存储库中有一个工作 YAML 文件(azure-pipelines.yml),可在推送代码更改时自动运行。 可以自定义此文件以满足项目的特定需求。

小窍门

若要更改本文中所述的 YAML 文件,请在 “管道 ”页上选择管道,然后选择“ 编辑 ”文件 azure-pipelines.yml

构建环境

可以使用 Azure Pipelines 生成 Ruby 项目,而无需设置自己的任何基础结构。 Ruby 预安装在 Azure Pipelines 中的 Microsoft托管代理 上。 可以使用 Linux、macOS 或 Windows 代理来运行生成。

有关预安装的 Ruby 的确切版本,请参阅 Microsoft托管的代理。 若要在Microsoft托管代理上安装特定版本的 Ruby,请将 Use Ruby 版本 任务添加到管道的开头。

使用特定的 Ruby 版本

添加 “使用 Ruby 版本 ”任务以在管道中设置 Ruby 版本。 此代码片段将 Ruby 3.4 或更高版本添加到路径,并设置要使用的后续管道任务。 若要查看哪些 Ruby 版本预安装在Microsoft托管代理上,请参阅 软件

pool:
  vmImage: 'ubuntu-latest' 

steps:
- task: UseRubyVersion@0 
  inputs:
    versionSpec: '>= 3.4' 
    addToPath: true
  displayName: 'Set Ruby version'

安装 Rails

若要安装 Rails,请将以下代码片段添加到 azure-pipelines.yml 文件。

- script: gem install rails && rails -v
  displayName: 'Install Rails'

安装依赖项

使用 Bundler 安装项目的 gem 依赖项。 Bundler 会读取你的 GemfileGemfile.lock,以确保在流水线中安装与开发环境中相同版本的 gem。

- script: |
    gem install bundler
    bundle install --retry=3 --jobs=4
  displayName: 'Install dependencies with Bundler'

标志 --retry=3 会重试失败的安装最多 3 次;而 --jobs=4 则允许并行安装 gem,从而加快构建速度。

运行 Rake

若要在当前捆绑包的上下文中执行 Rake(如 Gemfile 中定义),请将以下代码片段添加到 azure-pipelines.yml 文件中。

- script: bundle exec rake
  displayName: 'bundle exec rake'

发布测试结果

示例代码包括使用 RSpec 编写的单元测试。 在上一步中运行 Rake 时,它将执行 RSpec 测试。 Rakefile 中的 RSpec RakeTask 配置为使用 RspecJUnitFormatter 生成 JUnit 样式的结果。

添加 “发布测试结果 ”任务,将 JUnit 样式的测试结果发布到服务器。

- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testResultsFiles: '**/test-*.xml'
    testRunTitle: 'Ruby tests'

发布代码覆盖率结果

示例代码使用 SimpleCov 在单元测试运行时收集代码覆盖率数据。 SimpleCov 配置为使用 Cobertura 和 HTML 报表格式化程序。

添加 “发布代码覆盖率结果 ”任务,将代码覆盖率结果发布到服务器。 这样就可以在生成摘要中查看覆盖率指标,并下载 HTML 报表以供进一步分析。

- task: PublishCodeCoverageResults@2
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
    reportDirectory: '$(System.DefaultWorkingDirectory)/**/coverage'
    failIfCoverageEmpty: true 

完整示例管道

下面是一个完整的 azure-pipelines.yml 文件,演示本文介绍的所有概念。 此管道:

  • 在主分支发生更改时触发
  • 使用 Ubuntu 作为生成环境
  • 设置指定的 Ruby 版本
  • 安装项目依赖项
  • 使用 Rake 运行测试
  • 发布测试结果和代码覆盖率指标
# Ruby pipeline example
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  rubyVersion: '3.4.7'

steps:
- task: UseRubyVersion@0
  inputs:
    versionSpec: '$(rubyVersion)'
    addToPath: true
  displayName: 'Set Ruby version to $(rubyVersion)'

- script: |
    gem install bundler
    bundle install --retry=3 --jobs=4
  displayName: 'Install dependencies'

- script: bundle exec rake
  displayName: 'Run tests with Rake'

- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testResultsFiles: '**/test-*.xml'
    testRunTitle: 'Ruby tests'
  displayName: 'Publish test results'

- task: PublishCodeCoverageResults@2
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
    reportDirectory: '$(System.DefaultWorkingDirectory)/**/coverage'
    failIfCoverageEmpty: true
  displayName: 'Publish code coverage'