Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
GitHub Docs

Building and testing Ruby

You can create a continuous integration (CI) workflow to build and test your Ruby project.

Introduction

This guide shows you how to create a continuous integration (CI) workflow that builds and tests a Ruby application. If your CI tests pass, you may want to deploy your code or publish a gem.

Prerequisites

We recommend that you have a basic understanding of Ruby, YAML, workflow configuration options, and how to create a workflow file. For more information, see:

Using a Ruby workflow template

To get started quickly, add a workflow template to the.github/workflows directory of your repository.

GitHub provides a workflow template for Ruby that should work for most Ruby projects. The subsequent sections of this guide give examples of how you can customize this workflow template.

  1. On GitHub, navigate to the main page of the repository.

  2. Under your repository name, click Actions.

    Screenshot of the tabs for the "github/docs" repository. The "Actions" tab is highlighted with an orange outline.

  3. If you already have a workflow in your repository, clickNew workflow.

  4. The "Choose a workflow" page shows a selection of recommended workflow templates. Search for "ruby".

  5. Filter the selection of workflows by clickingContinuous integration.

  6. On the "Ruby" workflow, clickConfigure.

  7. Edit the workflow as required. For example, change the Ruby versions you want to use.

    Note

    • This workflow template contains an action that is not certified by GitHub. Actions provided by third parties are governed by separate terms of service, privacy policy, and support documentation.
    • If you use actions from third parties you should use a version specified by a commit SHA. If the action is revised and you want to use the newer version, you will need to update the SHA. You can specify a version by referencing a tag or a branch, however the action may change without warning. For more information, seeSecure use reference.
  8. ClickCommit changes.

    Theruby.yml workflow file is added to the.github/workflows directory of your repository.

Specifying the Ruby version

The easiest way to specify a Ruby version is by using theruby/setup-ruby action provided by the Ruby organization on GitHub. The action adds any supported Ruby version toPATH for each job run in a workflow. For more information and available Ruby versions, seeruby/setup-ruby.

Using Ruby'sruby/setup-ruby action is the recommended way of using Ruby with GitHub Actions because it ensures consistent behavior across different runners and different versions of Ruby.

Thesetup-ruby action takes a Ruby version as an input and configures that version on the runner.

steps:-uses:actions/checkout@v5-uses:ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1with:ruby-version:'3.1'# Not needed with a .ruby-version file-run:bundleinstall-run:bundleexecrake

Alternatively, you can check a.ruby-version file into the root of your repository andsetup-ruby will use the version defined in that file.

Testing with multiple versions of Ruby

You can add a matrix strategy to run your workflow with more than one version of Ruby. For example, you can test your code against the latest patch releases of versions 3.1, 3.0, and 2.7.

strategy:matrix:ruby-version: ['3.1','3.0','2.7']

Each version of Ruby specified in theruby-version array creates a job that runs the same steps. The${{ matrix.ruby-version }} context is used to access the current job's version. For more information about matrix strategies and contexts, seeWorkflow syntax for GitHub Actions andContexts reference.

The full updated workflow with a matrix strategy could look like this:

# This workflow uses actions that are not certified by GitHub.# They are provided by a third-party and are governed by# separate terms of service, privacy policy, and support# documentation.# GitHub recommends pinning actions to a commit SHA.# To get a newer version, you will need to update the SHA.# You can also reference a tag or branch, but the action may change without warning.name:RubyCIon:push:branches: [main ]pull_request:branches: [main ]jobs:test:runs-on:ubuntu-lateststrategy:matrix:ruby-version: ['3.1','3.0','2.7']steps:-uses:actions/checkout@v5-name:SetupRuby${{matrix.ruby-version}}uses:ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1with:ruby-version:${{matrix.ruby-version}}-name:Installdependenciesrun:bundleinstall-name:Runtestsrun:bundleexecrake

Installing dependencies with Bundler

Thesetup-ruby action will automatically install bundler for you. The version is determined by yourgemfile.lock file. If no version is present in your lockfile, then the latest compatible version will be installed.

steps:-uses:actions/checkout@v5-uses:ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1with:ruby-version:'3.1'-run:bundleinstall

Caching dependencies

Thesetup-ruby actions provides a method to automatically handle the caching of your gems between runs.

To enable caching, set the following.

steps:-uses:ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1with:bundler-cache:true

This will configure bundler to install your gems tovendor/cache. For each successful run of your workflow, this folder will be cached by GitHub Actions and re-downloaded for subsequent workflow runs. A hash of yourgemfile.lock and the Ruby version are used as the cache key. If you install any new gems, or change a version, the cache will be invalidated and bundler will do a fresh install.

Caching without setup-ruby

For greater control over caching, you can use theactions/cache action directly. For more information, seeDependency caching reference.

steps:-uses:actions/cache@v4with:path:vendor/bundlekey:${{runner.os}}-gems-${{hashFiles('**/Gemfile.lock')}}restore-keys:|      ${{ runner.os }}-gems--name:Bundleinstallrun:|    bundle config path vendor/bundle    bundle install --jobs 4 --retry 3

If you're using a matrix build, you will want to include the matrix variables in your cache key. For example, if you have a matrix strategy for different ruby versions (matrix.ruby-version) and different operating systems (matrix.os), your workflow steps might look like this:

steps:-uses:actions/cache@v4with:path:vendor/bundlekey:bundle-use-ruby-${{matrix.os}}-${{matrix.ruby-version}}-${{hashFiles('**/Gemfile.lock')}}restore-keys:|      bundle-use-ruby-${{ matrix.os }}-${{ matrix.ruby-version }}--name:Bundleinstallrun:|    bundle config path vendor/bundle    bundle install --jobs 4 --retry 3

Matrix testing your code

The following example matrix tests all stable releases and head versions of MRI, JRuby and TruffleRuby on Ubuntu and macOS.

# This workflow uses actions that are not certified by GitHub.# They are provided by a third-party and are governed by# separate terms of service, privacy policy, and support# documentation.# GitHub recommends pinning actions to a commit SHA.# To get a newer version, you will need to update the SHA.# You can also reference a tag or branch, but the action may change without warning.name:MatrixTestingon:push:branches: [main ]pull_request:branches: [main ]jobs:test:runs-on:${{matrix.os}}-lateststrategy:fail-fast:falsematrix:os: [ubuntu,macos]ruby: [2.5,2.6,2.7,head,debug,jruby,jruby-head,truffleruby,truffleruby-head]continue-on-error:${{endsWith(matrix.ruby,'head')||matrix.ruby=='debug'}}steps:-uses:actions/checkout@v5-uses:ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1with:ruby-version:${{matrix.ruby}}-run:bundleinstall-run:bundleexecrake

Linting your code

The following example installsrubocop and uses it to lint all files. For more information, seeRuboCop. You canconfigure Rubocop to decide on the specific linting rules.

# This workflow uses actions that are not certified by GitHub.# They are provided by a third-party and are governed by# separate terms of service, privacy policy, and support# documentation.# GitHub recommends pinning actions to a commit SHA.# To get a newer version, you will need to update the SHA.# You can also reference a tag or branch, but the action may change without warning.name:Lintingon: [push]jobs:test:runs-on:ubuntu-lateststeps:-uses:actions/checkout@v5-uses:ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1with:ruby-version:'2.6'-run:bundleinstall-name:Rubocoprun:rubocop-fgithub

Specifying-f github means that the RuboCop output will be in GitHub's annotation format. Any linting errors will show inline in theFiles changed tab of the pull request that introduces them.

Publishing Gems

You can configure your workflow to publish your Ruby package to any package registry you'd like when your CI tests pass.

You can store any access tokens or credentials needed to publish your package using repository secrets. The following example creates and publishes a package toGitHub Package Registry andRubyGems.

# This workflow uses actions that are not certified by GitHub.# They are provided by a third-party and are governed by# separate terms of service, privacy policy, and support# documentation.# GitHub recommends pinning actions to a commit SHA.# To get a newer version, you will need to update the SHA.# You can also reference a tag or branch, but the action may change without warning.name:RubyGemon:# Manually publishworkflow_dispatch:# Alternatively, publish whenever changes are merged to the `main` branch.push:branches: [main ]pull_request:branches: [main ]jobs:build:name:Build+Publishruns-on:ubuntu-latestpermissions:packages:writecontents:readsteps:-uses:actions/checkout@v5-name:SetupRuby2.6uses:ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1with:ruby-version:'2.6'-run:bundleinstall-name:PublishtoGPRrun:|          mkdir -p $HOME/.gem          touch $HOME/.gem/credentials          chmod 0600 $HOME/.gem/credentials          printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials          gem build *.gemspec          gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gemenv:GEM_HOST_API_KEY:"Bearer ${{secrets.GITHUB_TOKEN}}"OWNER:${{github.repository_owner}}-name:PublishtoRubyGemsrun:|          mkdir -p $HOME/.gem          touch $HOME/.gem/credentials          chmod 0600 $HOME/.gem/credentials          printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials          gem build *.gemspec          gem push *.gemenv:GEM_HOST_API_KEY:"${{secrets.RUBYGEMS_AUTH_TOKEN}}"

[8]ページ先頭

©2009-2025 Movatter.jp