[Rails] Setup GitHub Actions Workflow

December 17, 2020

Introduction

GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

Details from GitHub website.

With Boring Generator

Boring Generator installation guide.

> rails generate boring:ci:github_action:install [options]
OPTIONS:
  ruby-version:    Tell us the ruby version which you use for the application.
                   Default to Ruby .ruby-version, which will cause the action to use the version specified in the .ruby-version file.
  node-version:    Tell us the node version which you use for the application.
                   Default to Node 10.13.0
  repository-name: Tell us the repository name to be used as database name on GitHub Actions.
                   Defaults to boring_generators

Without Boring Generator

1. Create GitHub actions workflow directory

In your repository, create the .github/workflows/ directory to store your workflow files.

> mkdir .github/workflows/

2. Create workflow to run test-suite

In the .github/workflows/ directory, create a new file called ci.yml and add the following code. The following workflow would be triggered on push event. Read more on other GitHub events here.

name: CI
on: [push]
jobs:
  test:
    name: Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

3. Setup Ruby and install gems

Add following steps after Checkout code step to configure Ruby version on the workflow. You can specify the ruby version here used in the application.

...
- name: Set up Ruby <ruby_version>
  uses: ruby/setup-ruby@v1
  with:
    ruby-version: <ruby_version>
    bundler-cache: true

4. Setup Node and install packages

Add following steps after Install gems step to configure Node version on the workflow. Specify the ruby version here used in the application.

- name: Setup Node
  uses: actions/setup-node@v1
  with:
    node-version: 10.13.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
  with:
    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

5. Setup Database

  • Add PG services with expected POSTGRES_USER name and POSTGRES_DB name.
  • Add the step to run bin/rails db:setup with proper environment variables after Install packages step.
name: CI
on: [push]
jobs:
  test:
    name: Tests
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:11
        env:
          POSTGRES_USER: boring_generators
          POSTGRES_DB: boring_generators_test
          POSTGRES_PASSWORD: ""
        ports: ["5432:5432"]
    steps:
      ...
      ...
      - name: Setup test database
        env:
          RAILS_ENV: test
          PGHOST: localhost
          PGUSER: boring_generators
        run: |
          bin/rails db:setup

6. Run the test-suite

Add following step after database setup. Make sure to update the test runner command as per your requirement.

- name: Run tests
        run: bundle exec rails test

Happy Coding!!!