How to setup GitHub Actions CI in a Rails 7 app

Photo by Glen Carrie on Unsplash

How to setup GitHub Actions CI in a Rails 7 app

My learnings on how to go about it

What is GitHub Actions

For the uninitiated, GitHub Actions helps automate certain aspects of a software project's build, test and deployment aspects. If your project is already on Github, it makes sense to utilise what GitHub Actions has to offer.
The focus of this write-up is the CI aspect of GitHub Actions.

Why I did this

I set out to familiarise myself with how to use GitHub Actions for CI. I wanted to automate the running of the system tests in Rumblr.
I precisely wanted the CI to run every time I push to a new branch or the main branch and every time a new Pull Request is made.

How I achieved it

To get the CI to build, new changes needed to be made and new files needed to be added.

A new file: .env.test had to be created. Below are the contents of the file;

EMAIL_RECIPIENTS=test@test.com
FROM_EMAIL=sample@test.com
HONEYBADGER_API_KEY=honeybadger

A new file for the workflow needed to be added.
Below are the contents of the actual CI file which is located via: .github/workflows/config.yml

name: CI

 on: [push,pull_request]
 jobs:
   test:
     name: Tests
     runs-on: ubuntu-latest
     services:
       postgres:
         image: postgres:12.7
         env:
           POSTGRES_USER: rumblr
           POSTGRES_DB: rumblr_test
           POSTGRES_PASSWORD: password
         ports: ["5432:5432"]
         options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

     steps:
       - name: Checkout code
         uses: actions/checkout@v2

       - name: Setup Ruby and install gems
         uses: ruby/setup-ruby@v1
         with:
           bundler-cache: true

       - name: Install postgres client
         run: sudo apt-get install libpq-dev

       - name: Setup test database
         env:
           RAILS_ENV: test
           POSTGRES_HOST: localhost
           POSTGRES_USER: rumblr
           POSTGRES_PASSWORD: password
         run: |
           bin/rails db:setup

       - name: Run tests
         env:
           RAILS_ENV: test
           POSTGRES_HOST: localhost
           POSTGRES_USER: rumblr
           POSTGRES_PASSWORD: password
         run: |
           bundle exec rake assets:precompile
           bundle exec rspec

Some changes needed to be made to the database.yml file. The changes are below;

test:
   <<: *default
   database: rumblr_test
   host: <%= ENV.fetch('POSTGRES_HOST',  'localhost') %>
   username: <%= ENV.fetch('POSTGRES_USER', nil) %>
   password: <%= ENV.fetch('POSTGRES_PASSWORD', nil) %>
   port: <%= ENV.fetch('POSTGRES_PORT', 5432) %>

These changes were done to the test portion of the database configuration.

Challenges faced

Some challenges (as expected) were faced while attempting this task.
Consider this git change log for the challenges that were faced.

The .env.test file

This file had to be included and the suggestion came from a colleague.
The official Github guides were only partly helpful to assist in setting this up.
Otherwise, the build kept on failing and the error didn't make a lot of sense.

The changes to the test section of the DB config

Consider the config/database.yml file in the change log.
All that had to be added as well for the build to happen successfully.

References

The links below were helpful in achieving this task;

  1. Boring Rails Article

  2. Andy Croll Article

  3. This Stack Overflow answer

  4. Medium Article

  5. Github Tailwind CSS Issue

Conclusion

This is included in the Rumblr project which can be found here.