Build and test Ruby apps

Azure Pipelines

This guidance explains how to automatically build Ruby projects.

Get started

Follow these instructions to set up a pipeline for a Ruby app.

  1. The code in the following repository is a simple Ruby app. To get started, fork this repo to your GitHub account.

    https://github.com/MicrosoftDocs/pipelines-ruby
    
  2. Sign in to your Azure DevOps organization and navigate to your project.

  3. In your project, navigate to the Pipelines page. Then choose the action to create a new pipeline.

  4. Walk through the steps of the wizard by first selecting GitHub as the location of your source code.

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

  6. When the list of repositories appears, select your Ruby sample repository.

  7. Azure Pipelines will analyze the code in your repository and recommend Ruby template for your pipeline. Select that template.

  8. Azure Pipelines will generate a YAML file for your pipeline. Select Save and run, then select Commit directly to the master branch, and then choose Save and run again.

  9. A new run is started. Wait for the run to finish.

When you're done, you'll 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 topic, 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://docs.microsoft.com/azure/devops/pipelines/ecosystems/ruby
pool:
  vmImage: 'ubuntu-16.04' # other options: 'macOS-10.13', 'vs2017-win2016'

steps:
- task: UseRubyVersion@0
  inputs:
    versionSpec: '>= 2.4'
    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. 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 an image and push to container registry

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