Add CI to Rails repo on Github with Github Actions

Integrate test with rspec and Jasmine

Posted on January 30, 2025

Having Continuous Integrations and automated test on any software development on any project is a primary requirement for a successful product


Create the ci yaml file

The first step is to enabled the Github Actions on your current repository, following the documentation we must create a github/workflow folder and add a file named ci.yaml or using the Actions button on your current repository.

The structure of the file must include branches, jobs, services, and steps for each job.


name: CI
on:
push:
  branches:
    - main
jobs:
  test:
    runs-on: ubuntu-latest
    services:
    steps:

Setup your local db version

Previously to setup your ruby version we must setup the db engine and version, for this example we will use Postgres as the db engine. So we must setup the Postgres version and credentials on services tag.



services:
  db:
    image: postgres:12
    ports: ['5432:5432']
    env:
      POSTGRES_PASSWORD: thisisthepasswordofdb
    options: >-
      --health-cmd pg_isready
      --health-interval 10s
      --health-timeout 5s
      --health-retries 5



Setup the ruby version and services

If you are using one of the supported version of ruby by setup-ruby you just can add the strategy step and define the ruby version to use



strategy:
  matrix:
    ruby-version: '3.1'

But if you are using one of the non-supported versions you must customize your current file, so this is our case, so we must install a custom ruby version using rvm or another tool, in this case using rvm and setup ruby 2.6.9.


 - name: Set up RVM
        run: |
          curl -sSL https://get.rvm.io | bash
 - name: Set up Ruby from .ruby-version
        run: |
          source $HOME/.rvm/scripts/rvm
          wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
          tar zxvf openssl-1.1.1g.tar.gz
          cd "openssl-1.1.1g"
          ./config --prefix=$HOME/.openssl/openssl-1.1.1g --openssldir=$HOME/.openssl/openssl-1.1.1g
          make
          make install
          rm -rf ~/.openssl/openssl-1.1.1g/certs
          ln -s /etc/ssl/certs ~/.openssl/openssl-1.1.1g/certs
          rvm install ruby-3.2.2 -C --with-openssl-dir=$HOME/.openssl/openssl-1.1.1g #--binary
          rvm --default use ruby-3.2.2

Install the gems and run tests

Once we setup the ruby instalation, proceed with the gem instalation and run the tests using the previously created service for the database, so for accessing this created a environment variable using the env tag.


 - name: Build and run tests
     env:
       DATABASE_URL: postgres://postgres:thisisthepasswordofdb@localhost:5432
       RAILS_ENV: test
     run:
       source $HOME/.rvm/scripts/rvm
       sudo apt-get -yqq install libpq-dev
       gem install bundler
       bundle install
       bundle exec rake db:create RAILS_ENV=test
       bundle exec rake db:migrate RAILS_ENV=test
       bundle exec rspec

Validate test running on github

The final step is validate our test on github so on action section we must found the test results, once the yaml is properly setup the action will run automatically.