Build and test Ruby apps

Azure DevOps Services

This article explains how to automatically build Ruby projects.

Create the Azure Pipelines

Do the following steps to set up a pipeline for a Ruby app.

  1. Sign in to your Azure DevOps organization and go to your project.

  2. Select Pipelines > New pipeline.

  3. Select GitHub as the location of your source code.

    You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.

  4. Select your Ruby sample repository.

  5. Select the Ruby template for your pipeline.

  6. A YAML file gets generated. Select Save and run > Commit directly to the main branch, and then choose Save and run again.

  7. Wait for the run to finish.

You have a working YAML file (azure-pipelines.yml) in your repository that's ready for you to customize.

Tip

To make changes to the YAML file as described in this article, select the pipeline in the Pipelines page, and then Edit the azure-pipelines.yml file.

Build environment

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://learn.microsoft.com/azure/devops/pipelines/ecosystems/ruby
pool:
  vmImage: 'ubuntu-latest' # other options: 'macOS-latest', 'windows-latest'

steps:
- task: UseRubyVersion@0
  inputs:
    versionSpec: '>= 2.5'
    addToPath: true

Install Rails

To install Rails, add the following snippet to your azure-pipelines.yml file.

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

Install dependencies

To use Bundler to install dependencies, add the following snippet to your azure-pipelines.yml file.

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

Run Rake

To execute Rake in the context of the current bundle (as defined in your Gemfile), add the following snippet to your azure-pipelines.yml file.

- 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. You get a rich test reporting experience that you can use for 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 get 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 so, 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 an image and push to container registry

For your Ruby app, you can also build an image and push it to a container registry.