Caching Ruby gems in GitHub Actions using ruby ​​/ setup-ruby or actions / cache

How to load Ruby gems from the cache into GitHub Actions to speed up the launch of a project build in this continuous integration (CI) system? Getting all the dependencies of a Ruby on Rails (RoR) project up and running in no time can help reduce the time it takes to run tests for that project. This is where caching comes in handy. Ruby gems needed in a project can be cached using GitHub Actions, so they can be loaded much faster when the CI pipeline starts up than before. There are two ways to cache Ruby gems that are applicable when using CI GitHub Actions. One of them provides for the application ruby/setup-ruby



, and the second - actions/cache



.









Actions/cache β€”



Actions / cache is a popular solution that can be used to place data in the cache and to retrieve it from the cache the next time the project build process starts in a CI system. This action is often used for RoR projects that also use an action to control Ruby versions in GitHub Actions actions/setup-ruby



.



Let's consider an example of a configuration file for organizing caching in GitHub Actions, which uses actions/cache



:



# .github/workflows/main.yml
name: Main
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/cache@v2
        with:
          path: vendor/bundle
          key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-gems-

      - name: Bundle install
        env:
          RAILS_ENV: test
        run: |
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3

      
      





Let's analyze the main features of this file:



  • You need to specify the path to the directory that you want to cache. In our case, it is - vendor/bundle



    .
  • In addition, here we generate a unique key



    cache key ( ), using information about the operating system version and file Gemfile.lock



    . Changing the operating system version or installing a new gem Gemfile.lock



    that causes the change will generate a new value key



    .
  • Next, we need to configure the bundler so that it installs all our Ruby gems into the directory vendor/bundle



    .
  • Here you can use the following bundler parameters:


If you are interested in taking a look at the complete YAML files with GitHub Actions settings for Rail projects - here , here , there , here are some of our articles on this topic.



Ruby / setup-ruby - installing Ruby and caching gems



Above, we mentioned that it is often used in RoR projects actions/setup-ruby



. But the action actions/setup-ruby



was deprecated. It is recommended to use an action instead of it these days ruby/setup-ruby



. In it, among other features, there is also the ability to cache data. This is what the configuration file looks like when it is intended to be used ruby/setup-ruby



:



# .github/workflows/main.yml
name: Main
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    - uses: ruby/setup-ruby@v1
      with:
        #     .ruby-version
        ruby-version: 2.7
        #  'bundle install'     
        bundler-cache: true

    #  RSpec-
    - run: bundle exec rspec

      
      





As you can see, this option for organizing gems caching is simpler than what we described in the previous section. And here, in addition, we have at our disposal the Ruby version control system. In essence, if we talk about caching gems, it all comes down to adding a line to the configuration file bundler-cache: true



.



But what you can find out by referring to the documentation for ruby/setup-ruby



:



, , , . , -, , - β€” . , , actions/cache ( β€” , , , .lock-, , , ABI- ruby-head ). , , , bundler-cache: true…





We've covered two ways to cache Ruby gems in CI GitHub Actions. We hope that anyone who needs to speed up building Ruby projects has found some food for thought here. But, of course, there are other ways to speed up CI pipelines, for example, parallel execution of tests.



Are you using GitHub Actions?






All Articles