Table of Contents
Continuous Integration (CI) is a vital part of modern software development, helping teams automate testing and deployment processes. For Ruby on Rails projects, integrating CI with GitHub Actions streamlines development workflows, ensures code quality, and accelerates delivery. This article guides you through configuring CI for your Rails application using GitHub Actions.
Understanding GitHub Actions and Ruby on Rails
GitHub Actions is a powerful automation tool integrated into GitHub, allowing developers to create custom workflows for building, testing, and deploying code. Ruby on Rails, a popular web framework, benefits greatly from automated testing to maintain code quality and catch bugs early.
Setting Up Your Rails Project for CI
Before configuring GitHub Actions, ensure your Rails project is ready for automation:
- Include a Gemfile with all dependencies.
- Have a database configuration set up, typically using SQLite or PostgreSQL.
- Write comprehensive test cases using RSpec or Minitest.
- Ensure your project runs smoothly locally with bundle install and rails test.
Creating a GitHub Actions Workflow
In your Rails project repository, create a new directory for workflows:
.github/workflows
Inside this directory, create a file named ci.yml. This file defines your CI workflow.
Sample Workflow Configuration
Below is a sample configuration for running tests on Ubuntu with Ruby 3.1:
name: Ruby on Rails CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:13
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: password
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
- name: Install dependencies
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Set up database
run: |
cp config/database.yml.github-actions config/database.yml
bundle exec rails db:create
bundle exec rails db:migrate
- name: Run tests
env:
RAILS_ENV: test
run: |
bundle exec rails test
Configuring Database for CI
Ensure your config/database.yml is compatible with CI environment. For example, create a database.yml.github-actions with settings like:
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: postgres
password: password
host: localhost
Best Practices for CI with Rails
- Run tests on multiple Ruby and Rails versions using matrix strategies.
- Cache dependencies like
bundlerto speed up builds. - Include static code analysis tools such as RuboCop.
- Automate deployment steps after successful tests.
Conclusion
Configuring CI for your Ruby on Rails project with GitHub Actions enhances development efficiency, improves code quality, and accelerates delivery. By setting up workflows that automatically run tests and checks, your team can catch issues early and maintain high standards throughout the development lifecycle.