Table of Contents
Continuous Integration and Continuous Deployment (CI/CD) are essential practices for modern software development, especially when working with Ruby on Rails applications. Automating your build, test, and deployment processes with GitHub Actions can significantly improve your development workflow. This guide walks you through setting up Rails CI/CD with GitHub Actions step by step.
Prerequisites
- A GitHub account with a repository for your Rails project
- Basic knowledge of Git and GitHub workflows
- Ruby on Rails application set up locally
- Docker installed (optional but recommended)
Step 1: Prepare Your Rails Application
Ensure your Rails application is ready for CI/CD. Commit all recent changes and push to your GitHub repository. Make sure your project has a Gemfile and Gemfile.lock up to date.
Step 2: Create a GitHub Actions Workflow File
In your repository, create a new directory named .github/workflows. Inside this directory, add a file named rails-ci.yml.
This file will contain the configuration for your CI/CD pipeline.
Sample Workflow Configuration
Paste the following YAML configuration into rails-ci.yml:
name: Rails CI/CD
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
services:
db:
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
Step 3: Configure Database for CI/CD
Create a new database configuration file for GitHub Actions. In your project, add a file named config/database.yml.github-actions with the following content:
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: password
host: localhost
port: 5432
test:
<<: *default
database: myapp_test
Make sure to replace myapp_test with your actual test database name.
Step 4: Push the Workflow to GitHub
Commit your changes and push to the main branch:
git add .github/workflows/rails-ci.yml config/database.yml.github-actions
git commit -m "Add Rails CI/CD workflow"
git push origin main
Step 5: Monitor Your Workflow
Navigate to the "Actions" tab in your GitHub repository. You should see your workflow running whenever you push changes to main. Successful completion indicates your Rails app is correctly configured for CI/CD.
Additional Tips
- Secure your database credentials using GitHub Secrets.
- Extend workflows to include deployment steps, such as pushing to a server or cloud platform.
- Use Docker for consistent environment setup across development and CI/CD.
Implementing CI/CD with GitHub Actions streamlines your Rails development process, ensuring reliable testing and deployment. Customize the workflow to fit your project's specific needs and scale your automation as your project grows.