Skip to content

CI Workflow for Ruby on Rails

Published: at 12:25 PM

Discover how to implement an efficient ci workflow for your ruby on rails repository using github actions

In this tutorial, we’ll guide you through the steps to automate testing and linting, ensuring the reliability and quality of your codebase. Catch issues early and deliver high-quality code with confidence. Streamline your development process and focus on building great features. Let’s get started with GitHub Actions! 🚀

Continuous Integration (CI) workflows play a crucial role in ensuring the reliability and quality of software projects. In this tutorial, we’ll guide you through setting up a robust CI workflow for your Ruby on Rails repository using GitHub Actions. By automating the process of testing and linting your code, you can catch issues early and deliver high-quality code with confidence.

Prerequisites

Before proceeding, ensure that you have a Ruby on Rails repository hosted on GitHub.

Step 1: Organizing Your Workflow

In your GitHub repository, create a new directory named .github/workflows/ to store your workflow files.

Step 2: Create the CI Workflow File

Within the newly created .github/workflows/ directory, add a new file called rubyonrails-ci.yml. This file will contain the configuration for your CI workflow.

Step 3: Setting Up the Workflow

Paste the following code into rubyonrails-ci.yml. This workflow will install a prebuilt Ruby version, set up a PostgreSQL database, install project dependencies, and run tests and linters.

name: "Ruby on Rails CI"
on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:11-alpine
        ports:
          - "5432:5432"
        env:
          POSTGRES_DB: rails_test
          POSTGRES_USER: rails
          POSTGRES_PASSWORD: password
    env:
      RAILS_ENV: test
      DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test"
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Setup Chrome
        run: |
          wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
          sudo apt install ./google-chrome-stable_current_amd64.deb
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.1.2 # Change the ruby version
      - name: Set up dependencies
        run: bundle install
      - name: Set up database schema
        run: bin/rails db:schema:load
      - name: Precompile assets
        run: bundle exec rake assets:precompile
      - name: Run tests
        run: bundle exec rspec

Explanation of the Workflow

Conclusion

Congratulations! You have successfully set up a CI workflow using GitHub Actions for your Ruby on Rails project. With this workflow, you can automate testing, ensure code quality, and streamline the development process. By leveraging GitHub Actions, you can focus on building great features while maintaining a robust codebase.

For more details on GitHub Actions, refer to the official documentation