DEV Community

Cover image for Quick Bit:  Github Action to Test a Rails App

Quick Bit: Github Action to Test a Rails App

Corey Schaf
Software Engineer and AI explorer building stuff with ruby, python, go, c# and c++. Game dev in Unreal Engine and Unity3d.
・3 min read

Hello there!

In this quick little post I am going to quickly show how I built a Github Action that will run my Rails application's test suite. Now there are many posts on this topic, but I legit could not get anything I found on google to work. So after "days" of banging my head against the wall I was able to piece together a working Github Action. So if there is anyone in a similar place I hope this helps. As always, you can find me on twitter, cheers.

Alt Text

For my Ruby on Rails application I am using the following:

  1. Rails v6.1.3.1
  2. Postgres v12
  3. Standard minitest rails integration.


On my many pushes I would like to have a Github Action run that verifies my code and tests. This should also test any branch that is set to be PR'd into the main branch. Also for note, this is a personal project so I commit a lot and my commit msg's are quite sparse.

In the Rails Application.

In config/database.yml update the settings to use the env vars that we will establish later.

  <<: *default
  database: <%= ENV.fetch('PG_DATABASE', 'myapp_test') %>
  host:     <%= ENV.fetch('PG_HOST',  'localhost') %>
  username: <%= ENV.fetch('PG_USER', nil) %>
  password: <%= ENV.fetch('PG_PASSWORD', nil) %>
  port:     <%= ENV.fetch('PG_PORT', 5432) %>

Enter fullscreen mode Exit fullscreen mode

And the Github Action

name: Rails
      - main
      - main

  registry: myapp_registry
  DB_HOST: localhost
  DB_USERNAME: myapp
  DB_PASSWORD: password

    name: Tests
    runs-on: ubuntu-latest

        image: postgres:12
        ports: ["5432:5432"]
          POSTGRES_PASSWORD: password
          POSTGRES_DB: myapp_test
          POSTGRES_USER: myapp
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        #  ruby-version: 2.7.2 not needed with .ruby-version file

      - name: Setup Node
        uses: actions/setup-node@v1
          node-version: 10.18.0

      - name: Find yarn cache location
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: JS package cache
        uses: actions/cache@v1
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install packages
        run: |
          yarn install --pure-lockfile
      - name: Install PostgresSQL 12 Client
        run: |
          sudo apt-get -yqq install libpq-dev
      - name: Cache Ruby Gems
        uses: actions/cache@v2
          path: vendor/bundle
          key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-gems-
      - name: Bundle Install
        run: |
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3
      - name: Setup test database and run tests
          RAILS_ENV: test
          PG_HOST: localhost
          PG_DATABASE: myapp_test
          PG_USER: myapp
          PG_PASSWORD: password
          WITH_COVERAGE: true
          DISABLE_SPRING: 1
        run: |
          bin/rails db:setup
          bin/rails test

      - name: Upload Code Coverage
        uses: actions/upload-artifact@v2
          name: code-coverage
          path: coverage/
Enter fullscreen mode Exit fullscreen mode

Where is this file?

With Github Actions, you can set them up directly in the Github repo.

Alt Text

This will create a file in your code base under .github/workflows/{filename}.yml.

With this in place you should now see the build steps execute 1 by 1 and complete successfully, if your project is indeed passing all tests.

Alt Text

Cover credit:

Discussion (0)

Forem Open with the Forem app