|
| 1 | +--- |
| 2 | +title: Building and testing Ruby |
| 3 | +intro: You can create a continuous integration (CI) workflow to build and test your Ruby project. |
| 4 | +product: '{% data reusables.gated-features.actions %}' |
| 5 | +versions: |
| 6 | + free-pro-team: '*' |
| 7 | + enterprise-server: '>=2.22' |
| 8 | +--- |
| 9 | + |
| 10 | +{% data reusables.actions.enterprise-beta %} |
| 11 | +{% data reusables.actions.enterprise-github-hosted-runners %} |
| 12 | + |
| 13 | +### Introduction |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +### Prerequisites |
| 18 | + |
| 19 | +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: |
| 20 | + |
| 21 | +- [Learn {% data variables.product.prodname_actions %}](/actions/learn-github-actions) |
| 22 | +- [Ruby in 20 minutes](https://www.ruby-lang.org/en/documentation/quickstart/) |
| 23 | + |
| 24 | +### Starting with the Ruby workflow template |
| 25 | + |
| 26 | +{% data variables.product.prodname_dotcom %} provides a Ruby workflow template that will work for most Ruby projects. For more information, see the [Ruby workflow template](https://github.com/actions/starter-workflows/blob/master/ci/ruby.yml). |
| 27 | + |
| 28 | +To get started quickly, add the template to the `.github/workflows` directory of your repository. |
| 29 | + |
| 30 | +{% raw %} |
| 31 | +```yaml |
| 32 | +name: Ruby |
| 33 | + |
| 34 | +on: |
| 35 | + push: |
| 36 | + branches: [ $default-branch ] |
| 37 | + pull_request: |
| 38 | + branches: [ $default-branch ] |
| 39 | + |
| 40 | +jobs: |
| 41 | + test: |
| 42 | + |
| 43 | + runs-on: ubuntu-latest |
| 44 | + |
| 45 | + steps: |
| 46 | + - uses: actions/checkout@v2 |
| 47 | + - name: Set up Ruby |
| 48 | + # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, |
| 49 | + # change this to (see https://github.com/ruby/setup-ruby#versioning): |
| 50 | + # uses: ruby/setup-ruby@v1 |
| 51 | + uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0 |
| 52 | + with: |
| 53 | + ruby-version: 2.6 |
| 54 | + - name: Install dependencies |
| 55 | + run: bundle install |
| 56 | + - name: Run tests |
| 57 | + run: bundle exec rake |
| 58 | +``` |
| 59 | +{% endraw %} |
| 60 | +
|
| 61 | +### Specifying the Ruby version |
| 62 | +
|
| 63 | +The easiest way to specify a Ruby version is by using the `ruby/setup-ruby` action provided by the Ruby organization on GitHub. The action adds any supported Ruby version to `PATH` for each job run in a workflow. For more information see, the [`ruby/setup-ruby`](https://github.com/ruby/setup-ruby). |
| 64 | + |
| 65 | +Using either Ruby's `ruby/setup-ruby` action or GitHub's `actions/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. |
| 66 | + |
| 67 | +The `setup-ruby` action takes a Ruby version as an input and configures that version on the runner. |
| 68 | + |
| 69 | +{% raw %} |
| 70 | +```yaml |
| 71 | +steps: |
| 72 | +- uses: actions/checkout@v2 |
| 73 | +- uses: ruby/setup-ruby@v1 |
| 74 | + with: |
| 75 | + ruby-version: 2.6 # Not needed with a .ruby-version file |
| 76 | +- run: bundle install |
| 77 | +- run: bundle exec rake |
| 78 | +``` |
| 79 | +{% endraw %} |
| 80 | + |
| 81 | +Alternatively, you can check a `.ruby-version` file into the root of your repository and `setup-ruby` will use the version defined in that file. |
| 82 | + |
| 83 | +### Testing with multiple versions of Ruby |
| 84 | + |
| 85 | +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 2.7, 2.6, and 2.5. The 'x' is a wildcard character that matches the latest patch release available for a version. |
| 86 | + |
| 87 | +{% raw %} |
| 88 | +```yaml |
| 89 | +strategy: |
| 90 | + matrix: |
| 91 | + ruby-version: [2.7.x, 2.6.x, 2.5.x] |
| 92 | +``` |
| 93 | +{% endraw %} |
| 94 | + |
| 95 | +Each version of Ruby specified in the `ruby-version` array creates a job that runs the same steps. The {% raw %}`${{ matrix.ruby-version }}`{% endraw %} context is used to access the current job's version. For more information about matrix strategies and contexts, see "Workflow syntax for GitHub Actions" and "Context and expression syntax for GitHub Actions." |
| 96 | + |
| 97 | +The full updated workflow with a matrix strategy could look like this: |
| 98 | + |
| 99 | +{% raw %} |
| 100 | +```yaml |
| 101 | +name: Ruby CI |
| 102 | +
|
| 103 | +on: |
| 104 | + push: |
| 105 | + branches: [ $default-branch ] |
| 106 | + pull_request: |
| 107 | + branches: [ $default-branch ] |
| 108 | +
|
| 109 | +jobs: |
| 110 | + test: |
| 111 | +
|
| 112 | + runs-on: ubuntu-latest |
| 113 | +
|
| 114 | + strategy: |
| 115 | + matrix: |
| 116 | + ruby-version: [2.7.x, 2.6.x, 2.5.x] |
| 117 | +
|
| 118 | + steps: |
| 119 | + - uses: actions/checkout@v2 |
| 120 | + - name: Set up Ruby ${{ matrix.ruby-version }} |
| 121 | + # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, |
| 122 | + # change this to (see https://github.com/ruby/setup-ruby#versioning): |
| 123 | + # uses: ruby/setup-ruby@v1 |
| 124 | + uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0 |
| 125 | + with: |
| 126 | + ruby-version: ${{ matrix.ruby-version }} |
| 127 | + - name: Install dependencies |
| 128 | + run: bundle install |
| 129 | + - name: Run tests |
| 130 | + run: bundle exec rake |
| 131 | +``` |
| 132 | +{% endraw %} |
| 133 | + |
| 134 | +### Installing dependencies with Bundler |
| 135 | + |
| 136 | +The `setup-ruby` action will automatically install bundler for you. The version is determined by your `gemfile.lock` file. If no version is present in your lockfile, then the latest compatible version will be installed. |
| 137 | + |
| 138 | +{% raw %} |
| 139 | +```yaml |
| 140 | +steps: |
| 141 | +- uses: actions/checkout@v2 |
| 142 | +- uses: ruby/setup-ruby@v1 |
| 143 | + with: |
| 144 | + ruby-version: 2.6 |
| 145 | +- run: bundle install |
| 146 | +``` |
| 147 | +{% endraw %} |
| 148 | + |
| 149 | +#### Caching dependencies |
| 150 | + |
| 151 | +The `setup-ruby` actions provides a method to automatically handle the caching of your gems between runs. |
| 152 | + |
| 153 | +To enable caching, set the following. |
| 154 | + |
| 155 | +{% raw %} |
| 156 | +```yaml |
| 157 | +steps: |
| 158 | +- uses: ruby/setup-ruby@v1 |
| 159 | + with: |
| 160 | + bundler-cache: true |
| 161 | +``` |
| 162 | +{% endraw %} |
| 163 | + |
| 164 | +This will configure bundler to install your gems to `vendor/cache`. For each successful run of your workflow, this folder will be cached by Actions and re-downloaded for subsequent workflow runs. A hash of your gemfile.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. |
| 165 | + |
| 166 | +**Caching without setup-ruby** |
| 167 | + |
| 168 | +For greater control over caching, you can use the `actions/cache` Action directly. For more information, see "[Caching dependencies to speed up your workflow](/actions/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows)." |
| 169 | + |
| 170 | +{% raw %} |
| 171 | +```yaml |
| 172 | +steps: |
| 173 | +- uses: actions/cache@v2 |
| 174 | + with: |
| 175 | + path: vendor/bundle |
| 176 | + key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }} |
| 177 | + restore-keys: | |
| 178 | + ${{ runner.os }}-gems- |
| 179 | +- name: Bundle install |
| 180 | + run: | |
| 181 | + bundle config path vendor/bundle |
| 182 | + bundle install --jobs 4 --retry 3 |
| 183 | +``` |
| 184 | +{% endraw %} |
| 185 | + |
| 186 | +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: |
| 187 | + |
| 188 | +{% raw %} |
| 189 | +```yaml |
| 190 | +steps: |
| 191 | +- uses: actions/cache@v2 |
| 192 | + with: |
| 193 | + path: vendor/bundle |
| 194 | + key: bundle-use-ruby-${{ matrix.os }}-${{ matrix.ruby-version }}-${{ hashFiles('**/Gemfile.lock') }} |
| 195 | + restore-keys: | |
| 196 | + bundle-use-ruby-${{ matrix.os }}-${{ matrix.ruby-version }}- |
| 197 | +- name: Bundle install |
| 198 | + run: | |
| 199 | + bundle config path vendor/bundle |
| 200 | + bundle install --jobs 4 --retry 3 |
| 201 | +``` |
| 202 | +{% endraw %} |
| 203 | + |
| 204 | +### Matrix testing your code |
| 205 | + |
| 206 | +The following example matrix tests all stable releases and head versions of MRI, JRuby and TruffleRuby on Ubuntu and macOS. |
| 207 | + |
| 208 | +{% raw %} |
| 209 | +```yaml |
| 210 | +name: Matrix Testing |
| 211 | +
|
| 212 | +on: |
| 213 | + push: |
| 214 | + branches: [ $default-branch ] |
| 215 | + pull_request: |
| 216 | + branches: [ $default-branch ] |
| 217 | +
|
| 218 | +jobs: |
| 219 | + test: |
| 220 | + runs-on: ${{ matrix.os }}-latest |
| 221 | + strategy: |
| 222 | + fail-fast: false |
| 223 | + matrix: |
| 224 | + os: [ubuntu, macos] |
| 225 | + ruby: [2.5, 2.6, 2.7, head, debug, jruby, jruby-head, truffleruby, truffleruby-head] |
| 226 | + continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }} |
| 227 | + steps: |
| 228 | + - uses: actions/checkout@v2 |
| 229 | + - uses: ruby/setup-ruby@v1 |
| 230 | + with: |
| 231 | + ruby-version: ${{ matrix.ruby }} |
| 232 | + - run: bundle install |
| 233 | + - run: bundle exec rake |
| 234 | +``` |
| 235 | +{% endraw %} |
| 236 | + |
| 237 | +### Linting your code |
| 238 | + |
| 239 | +The following example installs `rubocop` and uses it to lint all files. For more information, see [Rubocop](https://github.com/rubocop-hq/rubocop). You can [configure Rubocop](https://docs.rubocop.org/rubocop/configuration.html) to decide on the specific linting rules. |
| 240 | + |
| 241 | +{% raw %} |
| 242 | +```yaml |
| 243 | +name: Linting |
| 244 | +
|
| 245 | +on: [push] |
| 246 | +
|
| 247 | +jobs: |
| 248 | + test: |
| 249 | + runs-on: ubuntu-latest |
| 250 | + steps: |
| 251 | + - uses: actions/checkout@v2 |
| 252 | + - uses: ruby/setup-ruby@v1 |
| 253 | + with: |
| 254 | + ruby-version: 2.6 |
| 255 | + - run: bundle install |
| 256 | + - name: Rubocop |
| 257 | + run: rubocop |
| 258 | +``` |
| 259 | +{% endraw %} |
| 260 | + |
| 261 | +### Publishing Gems |
| 262 | + |
| 263 | +You can configure your workflow to publish your Ruby package to any package registry you'd like when your CI tests pass. |
| 264 | + |
| 265 | +You can store any access tokens or credentials needed to publish your package using repository secrets. The following example creates and publishes a package to `GitHub Package Registry` and `RubyGems`. |
| 266 | + |
| 267 | +{% raw %} |
| 268 | +```yaml |
| 269 | +
|
| 270 | +name: Ruby Gem |
| 271 | +
|
| 272 | +on: |
| 273 | + # Manually publish |
| 274 | + workflow_dispatch: |
| 275 | + # Alternatively, publish whenever changes are merged to the default branch. |
| 276 | + push: |
| 277 | + branches: [ $default-branch ] |
| 278 | + pull_request: |
| 279 | + branches: [ $default-branch ] |
| 280 | +
|
| 281 | +jobs: |
| 282 | + build: |
| 283 | + name: Build + Publish |
| 284 | + runs-on: ubuntu-latest |
| 285 | +
|
| 286 | + steps: |
| 287 | + - uses: actions/checkout@v2 |
| 288 | + - name: Set up Ruby 2.6 |
| 289 | + uses: ruby/setup-ruby@v1 |
| 290 | + with: |
| 291 | + ruby-version: 2.6 |
| 292 | + - run: bundle install |
| 293 | +
|
| 294 | + - name: Publish to GPR |
| 295 | + run: | |
| 296 | + mkdir -p $HOME/.gem |
| 297 | + touch $HOME/.gem/credentials |
| 298 | + chmod 0600 $HOME/.gem/credentials |
| 299 | + printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials |
| 300 | + gem build *.gemspec |
| 301 | + gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem |
| 302 | + env: |
| 303 | + GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}" |
| 304 | + OWNER: ${{ github.repository_owner }} |
| 305 | +
|
| 306 | + - name: Publish to RubyGems |
| 307 | + run: | |
| 308 | + mkdir -p $HOME/.gem |
| 309 | + touch $HOME/.gem/credentials |
| 310 | + chmod 0600 $HOME/.gem/credentials |
| 311 | + printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials |
| 312 | + gem build *.gemspec |
| 313 | + gem push *.gem |
| 314 | + env: |
| 315 | + GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}" |
| 316 | +``` |
| 317 | +{% endraw %} |
| 318 | + |
0 commit comments