Build and test Ruby apps in Azure Pipelines
This guidance explains how to use Azure Pipelines to automatically build Ruby projects with CI/CD pipelines.
For a working example of how to build a Ruby project, import (into Azure Repos or TFS) or fork (into GitHub) this repo:
The sample code includes an
azure-pipelines.yml file at the root of the repository.
You can use this file to build the project.
Follow all the instructions in Create your first pipeline to create a build pipeline for the sample project.
You can use Azure Pipelines to build your Ruby projects without needing to set up any infrastructure of your own. Ruby is preinstalled on Microsoft-hosted agents in Azure Pipelines. You can use Linux, macOS, or Windows agents to run your builds.
For the exact versions of Ruby that are preinstalled, refer to Microsoft-hosted agents. To install a specific version of Ruby on Microsoft-hosted agents, add the Use Ruby Version task to the beginning of your pipeline.
Use a specific Ruby version
Add the Use Ruby Version task to set the version of Ruby used in your pipeline. This snippet adds Ruby 2.4 or later to the path and sets subsequent pipeline tasks to use it.
# https://docs.microsoft.com/azure/devops/pipelines/languages/ruby pool: vmImage: 'ubuntu-16.04' # other options: 'macOS-10.13', 'vs2017-win2016' steps: - task: UseRubyVersion@0 inputs: versionSpec: '>= 2.4' addToPath: true
To install Rails, add the following snippet to your
- script: gem install rails && rails -v displayName: 'gem install rails'
To use Bundler to install dependencies, add the following snippet to your
- script: | gem install bundler bundle install --retry=3 --jobs=4 displayName: 'bundle install'
To execute Rake in the context of the current bundle (as defined in your Gemfile), add the following snippet to your
- script: bundle exec rake displayName: 'bundle exec rake'
Publish test results
The sample code includes unit tests written using RSpec. When Rake is run by the previous step, it runs the RSpec tests. The RSpec RakeTask in the Rakefile has been configured to produce JUnit style results using the RspecJUnitFormatter.
Add the Publish Test Results task to publish JUnit style test results to the server. When you do this, you get a rich test reporting experience that can be used for easily troubleshooting any failed tests and for test timing analysis.
- task: PublishTestResults@2 condition: succeededOrFailed() inputs: testResultsFiles: '**/test-*.xml' testRunTitle: 'Ruby tests'
Publish code coverage results
The sample code uses SimpleCov to collect code coverage data when unit tests are run. SimpleCov is configured to use Cobertura and HTML report formatters.
Add the Publish Code Coverage Results task to publish code coverage results to the server. When you do this, coverage metrics can be seen in the build summary and HTML reports can be downloaded for further analysis.
- task: PublishCodeCoverageResults@1 inputs: codeCoverageTool: Cobertura summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml' reportDirectory: '$(System.DefaultWorkingDirectory)/**/coverage'
Build a container image
You can also build and publish a Docker container image for your Ruby app. For more information, see Docker.