Azure DevOps 服務 |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 的基本知識。 如需詳細資訊,請參閱 建立您的第一個管線。 - 權限: - 若要建立管線:您必須位於 參與者 群組中,且群組必須將 [建立組建管線 ] 許可權設定為 [允許]。 Project Administrators 群組的成員可以管理管線。 - 若要建立服務連線:您必須具有服務連線的系統管理員或建立者角色。 |
| GitHub | - GitHub 帳戶。 - 用來授權 Azure Pipelines 的 GitHub 服務連線 。 |
| 產品 | 要求 |
|---|---|
| Azure DevOps | - Azure DevOps 專案。 - 已安裝 Ruby 的自我裝載代理程式。 若要建立一個代理程式,請參閱 自我裝載代理程式。 - YAML 和 Azure Pipelines 的基本知識。 如需詳細資訊,請參閱 建立您的第一個管線。 - 權限: - 若要建立管線:您必須位於 參與者 群組中,且群組必須將 [建立組建管線 ] 許可權設定為 [允許]。 Project Administrators 群組的成員可以管理管線。 - 若要建立服務連線:您必須具有服務連線的系統管理員或建立者角色。 |
| GitHub | - GitHub 帳戶。 - 用來授權 Azure Pipelines 的 GitHub 服務連線 。 |
建立 Azure Pipelines
請遵循下列步驟來設定 Ruby 應用程式的管線。
登入您的 Azure DevOps 組織並前往您的專案。
移至管線>新增管線。
選取 [GitHub ] 作為原始程式碼的位置。
系統可能會將您重新導向至 GitHub 以進行登入。 若是如此,請輸入 GitHub 認證。
選取您的 Ruby 範例存放庫。
請選擇您的管線範本
Ruby。會產生 YAML 檔案。 選取 [儲存並直接執行>認可至主要分支],然後選取 [ 儲存並再次執行 ]。
等候流程執行完成。 對於一個基本的 Ruby 項目,這通常需要 2-5 分鐘。
您現在的存放庫中有一個有效的 YAML 檔案 (azure-pipelines.yml),當您推送程式碼變更時,該檔案會自動執行。 可以自訂此檔案以滿足您專案的特定需求。
小提示
若要變更本文所述的 YAML 檔案,請在 [管道] 頁面上選取管道,然後選取 [編輯] 以編輯該檔案。
建置環境
您可以使用 Azure Pipelines 來建置 Ruby 專案,而不需要設定您自己的任何基礎結構。 Ruby 會預安裝在 Azure Pipelines 中 Microsoft裝載的代理程式 上。 您可以使用 Linux、macOS 或 Windows 代理程式來執行組建。
如需預安裝之 Ruby 的確切版本,請參閱 Microsoft裝載的代理程式。 若要在裝載Microsoft代理程式上安裝特定版本的 Ruby,請將 [使用 Ruby 版本 ] 工作新增至管線的開頭。
使用特定的 Ruby 版本
新增 [使用 Ruby 版本] 工作,在管線中設定 Ruby 版本。 此代碼段會將 Ruby 3.4 或更新版本新增至路徑,並設定要使用的後續管線工作。 若要查看 Microsoft 裝載的代理程式上預先安裝了哪些 Ruby 版本,請參閱 軟體。
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 會讀取您的 Gemfile 和 Gemfile.lock ,以確保管道中安裝的 gem 版本與開發環境中的 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'