Skip to main content

Ruby application

You can build and test a Ruby application using a Linux platform on Harness Cloud or a self-hosted Kubernetes cluster build infrastructure.

This guide assumes you've created a Harness CI pipeline. For more information about creating pipelines, go to:

tip

If you don't have a Harness account yet, you can create one for free at app.harness.io.

Install dependencies

Run Bundler commands in a Run step to install dependencies in the build environment.

              - step:
type: Run
identifier: dependencies
name: Dependencies
spec:
shell: Sh
command: |-
bundle check || bundle install

Cache dependencies

You can cache your Ruby dependencies with Cache Intelligence. Add caching.enabled.true to your stage.spec and specify the cache paths (in paths and sharedPaths).

    - stage:
spec:
caching:
enabled: true
key: cache-{{ checksum "gemfile.lock" }}
paths:
- "vendor/bundle"
sharedPaths:
- vendor/bundle

Build and run tests

Add Run steps to run tests in Harness CI.

              - step:
type: Run
name: Run Ruby Tests
identifier: run_ruby_tests
spec:
shell: Sh
command: |-
bundle exec rake test

Visualize test results

If you want to view test results in Harness, your test reports must be in JUnit XML format and your steps must include the reports specification. The following examples use the Minitest JUnit Formatter. For more information and an RSpec example, go to Format test reports - Ruby.

              - step:
type: Run
name: Run Ruby Tests
identifier: run_ruby_tests
spec:
shell: Sh
command: |-
bundle exec rake test --junit
reports:
type: JUnit
spec:
paths:
- report.xml

Specify version

Ruby is pre-installed on Harness Cloud runners. For details about all available tools and versions, go to Platforms and image specifications.

If your application requires a specific Ruby version, add a Run step to install it.

Use the setup-ruby action in a GitHub Actions step to install the required Ruby version.

You will need a personal access token, stored as a secret, with read-only access for GitHub authentication.

Install one Ruby version
              - step:
type: Action
name: Install ruby
identifier: installruby
spec:
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
Use multiple Ruby versions
  1. Add a matrix looping strategy configuration to your stage.
    - stage:
strategy:
matrix:
rubyVersion:
- 3.2.2
- 2.7.8
  1. Reference the matrix variable in your steps.
              - step:
type: Action
name: Install ruby
identifier: installruby
spec:
uses: ruby/setup-ruby@v1
with:
ruby-version: <+ stage.matrix.rubyVersion >

Full pipeline examples

The following YAML examples describe pipelines that install dependencies, run tests, use caching, and build and push images to Docker Hub.

This pipeline uses Harness Cloud build infrastructure and Cache Intelligence.

If you copy this example, replace the placeholder values with appropriate values for your connector IDs, account/user names, and repo names. Depending on your project and organization, you may also need to replace projectIdentifier and orgIdentifier.

pipeline:
name: ruby
identifier: ruby
projectIdentifier: default
orgIdentifier: default
tags: {}
properties:
ci:
codebase:
connectorRef: YOUR_CODE_REPO_CONNECTOR_ID
repoName: YOUR_REPO_NAME
build: <+input>
stages:
- stage:
name: build
identifier: build
description: ""
type: CI
spec:
cloneCodebase: true
caching:
enabled: true
key: cache-{{ checksum "gemfile.lock" }}
paths:
- vendor/bundle
sharedPaths:
- vendor/bundle
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}
execution:
steps:
- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
shell: Sh
command: bundle install --path vendor/bundle
- step:
type: Run
name: Run Ruby Tests
identifier: run_ruby_tests
spec:
shell: Sh
command: bundle exec rake test --junit
reports:
type: JUnit
spec:
paths:
- report.xml
- step:
type: BuildAndPushDockerRegistry
name: BuildAndPushDockerRegistry_1
identifier: BuildAndPushDockerRegistry_1
spec:
connectorRef: YOUR_DOCKER_CONNECTOR_ID
repo: YOUR_DOCKER_HUB_USERNAME/YOUR_DOCKER_REPO_NAME
tags:
- <+pipeline.sequenceId>

Next steps

Now that you have created a pipeline that builds and tests a Ruby app, you could: