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:
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.
- Harness Cloud
- Self-hosted
- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
shell: Sh
command: |-
bundle check || bundle install
- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
connectorRef: account.harnessImage
image: ruby:latest
command: |-
bundle check || bundle install
Cache dependencies
- Harness Cloud
- Self-hosted
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
With self-hosted build infrastructures, you can:
Here's an example of a pipeline with Save Cache to S3 and Restore Cache from S3 steps.
steps:
- step:
type: RestoreCacheS3
name: Restore Cache From S3
identifier: Restore_Cache_From_S3
spec:
connectorRef: AWS_connector
region: us-east-1
bucket: some_s3_bucket
key: cache-{{ checksum "gemfile.lock" }}
archiveFormat: Tar
- step:
type: Run
...
- step:
type: BuildAndPushDockerRegistry
...
- step:
type: SaveCacheS3
name: Save Cache to S3
identifier: Save_Cache_to_S3
spec:
connectorRef: AWS_connector
region: us-east-1
bucket: some_s3_bucket
key: cache-{{ checksum "gemfile.lock" }}
sourcePaths:
- vendor/bundle
archiveFormat: Tar
Build and run tests
Add Run steps to run tests in Harness CI.
- Harness Cloud
- Self-hosted
- step:
type: Run
name: Run Ruby Tests
identifier: run_ruby_tests
spec:
shell: Sh
command: |-
bundle exec rake test
- step:
type: Run
name: Run Ruby Tests
identifier: run_ruby_tests
spec:
connectorRef: account.harnessImage
image: ruby:latest
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.
- Harness Cloud
- Self-hosted
- 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: Run
name: Run Ruby Tests
identifier: run_ruby_tests
spec:
connectorRef: account.harnessImage
image: ruby:latest
shell: Sh
command: |-
bundle exec rake test --junit
reports:
type: JUnit
spec:
paths:
- report.xml
Specify version
- Harness Cloud
- Self-hosted
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
- Add a matrix looping strategy configuration to your stage.
- stage:
strategy:
matrix:
rubyVersion:
- 3.2.2
- 2.7.8
- 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 >
Specify the desired Ruby Docker image tag in your steps. There is no need for a separate install step when using Docker.
Use a specific Ruby version
- step:
type: Run
name: Ruby Version
identifier: rubyversion
spec:
connectorRef: account.harnessImage
image: ruby:latest
shell: Sh
command: |-
ruby --version
Use multiple Ruby versions
- Add a matrix looping strategy configuration to your stage.
- stage:
strategy:
matrix:
rubyVersion:
- 3.2.2
- 2.7.8
- Reference the matrix variable in the
image
field of your steps.
- step:
type: Run
name: Ruby Version
identifier: rubyversion
spec:
connectorRef: account.harnessImage
image: ruby:<+ stage.matrix.rubyVersion >
shell: Sh
command: |-
ruby --version
Full pipeline examples
The following YAML examples describe pipelines that install dependencies, run tests, use caching, and build and push images to Docker Hub.
- Harness Cloud
- Self-hosted
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>
This pipeline uses self-hosted Kubernetes cluster build infrastructure and Save and Restore Cache from S3 steps.
If you copy this example, replace the placeholder values with appropriate values for your connector IDs, account/user names, repo names, and other settings. Depending on your project and organization, you may also need to replace projectIdentifier
and orgIdentifier
.
pipeline:
name: ruby-k8s
identifier: ruby_k8s
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
execution:
steps:
- step:
type: RestoreCacheS3
name: Restore Cache From S3
identifier: Restore_Cache_From_S3
spec:
connectorRef: YOUR_AWS_CONNECTOR_ID
region: us-east-1 ## Set to your bucket's AWS region
bucket: YOUR_AWS_BUCKET_NAME
key: cache-{{ checksum "gemfile.lock" }}
archiveFormat: Tar
- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
shell: Sh
command: bundle install --path vendor/bundle
connectorRef: account.harnessImage
image: ruby:latest
- step:
type: Run
name: Run Ruby Tests
identifier: run_ruby_tests
spec:
shell: Sh
command: bundle exec rake test --junit
connectorRef: account.harnessImage
image: ruby:latest
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>
- step:
type: SaveCacheS3
name: Save Cache to S3
identifier: Save_Cache_to_S3
spec:
connectorRef: YOUR_AWS_CONNECTOR_ID
region: us-east-1 ## Set to your bucket's AWS region
bucket: YOUR_AWS_BUCKET_NAME
key: cache-{{ checksum "gemfile.lock" }}
sourcePaths:
- vendor/bundle
archiveFormat: Tar
infrastructure:
type: KubernetesDirect
spec:
connectorRef: YOUR_KUBERNETES_CLUSTER_CONNECTOR_ID
namespace: YOUR_KUBERNETES_NAMESPACE
automountServiceAccountToken: true
nodeSelector: {}
os: Linux
Next steps
Now that you have created a pipeline that builds and tests a Ruby app, you could:
- Create triggers to automatically run your pipeline.
- Add steps to build and upload artifacts.
- Add a step to build and push an image to a Docker registry.
- Explore other ways to optimize and enhance CI pipelines.