Implementing Continuous Integration and Deployment in Rails Projects
Automate testing, integration, and deployment in your Ruby on Rails applications using CI/CD pipelines.
Introduction
How do you ensure code quality, automate testing, and deploy Rails applications effortlessly? π
CI/CD (Continuous Integration & Continuous Deployment) automates the entire build, test, and deployment process, reducing errors and ensuring fast, reliable releases.
In this guide, we will cover:
β
Setting up Continuous Integration (CI) with GitHub Actions
β
Running automated tests on every push
β
Building Docker containers for deployment
β
Using Capistrano for zero-downtime releases
By the end, youβll have a fully automated pipeline to ship high-quality Rails code faster. π
1. Understanding CI/CD in Rails
What is Continuous Integration (CI)?
CI ensures each code commit is tested automatically before merging.
π‘ Example Workflow:
- Developer pushes code β GitHub Actions triggers
- Run RSpec & RuboCop tests
- If tests pass, merge to main branch
What is Continuous Deployment (CD)?
CD automates deploying tested code to production.
π‘ Example Workflow:
- Code is merged β Build a Docker image
- Deploy to Heroku, AWS, or DigitalOcean
- Capistrano ensures zero-downtime deployment
Letβs set this up step by step.
2. Setting Up Continuous Integration with GitHub Actions
GitHub Actions automates CI for Rails projects.
π Create a .github/workflows/ci.yml
File
name: CI
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:13
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- 5432:5432
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Set up database
run: |
cp config/database.yml.ci config/database.yml
bin/rails db:create db:migrate
- name: Run tests
run: bundle exec rspec
β
Triggers on every commit & PR
β
Runs tests inside GitHubβs cloud environment
β
Uses PostgreSQL service for DB testing
3. Automating Deployment with Docker & Capistrano
Once CI tests pass, we deploy using Docker and Capistrano for zero downtime.
π Dockerize Your Rails App
Create a Dockerfile
:
FROM ruby:3.1
WORKDIR /app
COPY . .
RUN bundle install
CMD ["rails", "server", "-b", "0.0.0.0"]
Build & push the image:
docker build -t myrailsapp .
docker tag myrailsapp myrepo/myrailsapp:latest
docker push myrepo/myrailsapp:latest
Now, letβs automate deployment with Capistrano.
π Install Capistrano
bundle add capistrano capistrano-rails capistrano-passenger
Run:
bundle exec cap install
Modify config/deploy.rb
:
set :application, "myrailsapp"
set :repo_url, "git@github.com:yourusername/myrailsapp.git"
set :deploy_to, "/var/www/myrailsapp"
Now, deploy with:
cap production deploy
π Your Rails app is now deployed automatically!
4. Automating Deployment with GitHub Actions
Instead of manual deployment, trigger Docker builds & Capistrano deploys automatically.
π Create a .github/workflows/deploy.yml
File
name: Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Log in to DockerHub
run: echo "$" | docker login -u "$" --password-stdin
- name: Build and push Docker image
run: |
docker build -t myrepo/myrailsapp:latest .
docker push myrepo/myrailsapp:latest
- name: Deploy with Capistrano
run: |
ssh deploy@myserver "cd /var/www/myrailsapp && cap production deploy"
πΉ Automatically deploys on git push
πΉ Ensures fresh Docker images are deployed
πΉ Uses GitHub Secrets for security
5. Best Practices for CI/CD in Rails
β
Run tests on every commit β Prevents broken code from reaching production
β
Use Docker for consistency β Eliminates βworks on my machineβ issues
β
Automate deployment β Avoids manual errors and downtime
β
Secure secrets β Use environment variables instead of hardcoding API keys
β
Monitor deployments β Use tools like New Relic or Datadog to track performance
Conclusion
With this setup, your Rails app now has:
β
Continuous Integration (CI) with GitHub Actions
β
Automated Testing (RSpec, RuboCop, Postgres)
β
Containerized Deployment with Docker
β
Zero-Downtime Deployments with Capistrano
This streamlines development, increases reliability, and makes scaling easier. π
πΉ What CI/CD tools do you use for Rails? Drop a comment below!