RSpec Tests Config for Github Actions in Ruby on Rails Project
Are you thinking about migrating a Ruby on Rails project CI pipeline to GitHub Actions? Learn how to configure the Rails app to run RSpec tests using GitHub Actions.
Join the DZone community and get the full member experience.Join For Free
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-rubyaction 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 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.
It’s recommended to use
ruby/setup-ruby instead of outdated
How to Configure Postgres on GitHub Actions
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 the Redis Docker container to start the 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.
You can see that for RSpec we also use a
knapsack_pro Ruby gem flag
KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES. It allows to automatically detect slow test files and split them between parallel jobs.
You can learn more about it in a separate article explaining how to run slow RSpec files on Github Actions with parallel jobs by doing an auto split of the spec file by test examples.
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.
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.
Published at DZone with permission of Artur Trzop. See the original article here.
Opinions expressed by DZone contributors are their own.