Are you thinking about migrating a Ruby on Rails project CI pipeline to Github Actions? You will learn how to configure the Rails app to run RSpec tests using Github Actions.
This article covers a few things for Github Actions YAML config:
how to use ruby/setup-ruby action to install Ruby gems with bundler and automatically cache gems. This way you can load Ruby gems for your project from the cache and run CI build fast.
how to use Postgres on Github Actions
how to use Redis on Github Actions
how to use Github Actions build matrix to run parallel jobs and execute RSpec tests spread across multiple jobs to save time
Github Actions YML config for Rails application
ruby/setup-ruby action
ruby/setup-ruby is an action that you can use to install a particular Ruby programming language version. It allows you to cache Ruby gems based on your Gemfile.lock out of the box.
To use Postgres on Github Actions you need to set up a service for Postgres. I recommend using additional options that will configure Postgres to use RAM instead of disk. This way your database can run faster in a testing environment.
In the config below, we also pass the settings for doing a health check to ensure the database is up and running before you start running tests.
How to configure Redis on Github Actions
You can use Redis Docker container to start Redis server on Github Actions. See how simple it is:
How to use Github Actions build matrix to run tests with parallel jobs
You can use the build matrix in Github Actions to run multiple jobs at the same time.
You will need to split test files between these parallel jobs. For that, you can use Knapsack Pro with Queue Mode to distribute tests evenly between the jobs. This way you can ensure the proper amount of tests is executed on each job and the workload is well balanced between the jobs. Simply speaking this way you can make sure the CI build is as fast as possible - it has optimal execution time.
Full YML config for Github Actions and Ruby on Rails project
Here is the full configuration of the CI pipeline for Github Actions. You can use it to run tests for your Rails project.
Summary
You’ve just learned how to set up your Rails application on Github Actions. I hope this will help you if you migrate your project from a different CI server to Github Actions.
You can learn more about Knapsack Pro and how it can help you run tests fast using parallel jobs on CI. It works with RSpec, Cucumber, Minitest, and other Ruby test runners. Knapsack Pro can also work with JavaScript test runners and has a native API integration.