DEV Community

Allan
Allan

Posted on • Updated on

How To Set Up A Ruby On Rails CI Workflow Using Github Actions.

Continuous Integration(CI) is a development practice that enables developers to integrate code regularly into a central repository, where builds and tests run. The rationale behind this is that it helps development teams detect bugs quickly, improves code quality and security.

There are several tools we can use to automate tests. Examples of such tools are GitHub Actions, CircleCI, and many more.

This guide covers how you can set up a CI workflow for a Ruby on Rails app using GitHub Actions.

PREREQUISITES

  • Ruby On Rails app
  • Git
  • GitHub Account

WHAT IS GITHUB ACTIONS?

GitHub Actions is an automation platform that helps developers to automate workflow. Since GitHub Actions come included in your GitHub projects, it is pretty easy to set up a CI workflow without relying on any third-party integrations.

COMPONENTS OF GITHUB ACTIONS

WORKFLOW

Workflow is an automated steps that is made up of one or more jobs that can be scheduled or triggered by an event. They are defined by a YAML file in the .github/workflows directory and can be used to build, test, package, release or deploy a project on GitHub.

EVENTS

Events are specific tasks that trigger the execution of a workflow. An example is when you push a commit to a repository or create a pull request.

JOBS

A job is a series of steps that executes on the same runner.

STEPS

Steps consist of individual tasks to run a job. They group actions that are carried out on the runner.

ACTIONS

Actions are the smallest building blocks of a workflow that are combined into steps to create a job.

RUNNERS

A runner is a server on which our jobs will be executed.

CREATING A RUBY ON RAILS WORKFLOW

I. Create a repository on GitHub.
II. Create a new rails application, navigate to the directory, and push it to GitHub.
III. Go to the repository, and in the Actions tab, click on setup a workflow yourself. By default, this will create folder .github, a sub-folder workflow, and a main.yml file.
IV. Commit this and run git pull in the terminal to pull this.

Now let's follow these steps to set up a CI workflow using GitHub Actions.

1. Name the workflow.

name: CI
Enter fullscreen mode Exit fullscreen mode

2. Set the events.

on:

  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
Enter fullscreen mode Exit fullscreen mode

In this code snippet, the events will trigger the action to run whenever we push a code to the master branch or we perform a pull request. Note: The branch is set to the master branch by default, but you can configure it for different branches as well.

3. Define The Job.

 jobs:
  test:
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

We define a job, give it a name and tell it to run on the latest version of ubuntu.

4. Define The Services.

  services: 
      postgres:
        image: postgres:12
        env:
          POSTGRES_PASSWORD: postgres
        ports: ['5432:5432']
Enter fullscreen mode Exit fullscreen mode

Here, we specify the services we need to run the job and the database we need to run the tests.

5. Define the steps and Setup Dependencies

steps:
      - uses: actions/checkout@v2
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1.80.0
        with:
          ruby-version: 2.6.6

      - uses: Borales/actions-yarn@v2.3.0
        with:
          cmd: install

      - name: Install Dependencies
        run: |
          sudo apt install -yqq libpq-dev
          gem install bundler

      - name: Install Gems
        run: |
          bundle install
Enter fullscreen mode Exit fullscreen mode

The code above checkouts the source code, sets up ruby with version 2.6.6, install gems, and Postgres dependencies.

6. Set up the database

  - name: Setup database
        env:
          PG_DATABASE: postgres
          PG_HOST: localhost
          PG_USER: postgres
          PG_PASSWORD: password
          RAILS_ENV: test
          WITH_COVERAGE: true
          DISABLE_SPRING: 1
        run: |
          bundle exec rails db:prepare
          bundle exec rake test
Enter fullscreen mode Exit fullscreen mode

Here we specify the database required for rails to create, run and connect to Postgres.

7. Add Rspec Test.

  - name: Build and test with RSpec
        env:
          PG_DATABASE: postgres
          PG_HOST: localhost
          PG_USER: postgres
          PG_PASSWORD: password
          RAILS_ENV: test
        run: |
          bundle exec rspec spec
Enter fullscreen mode Exit fullscreen mode

Since we don't have RSpec included in the gemfile when we push the changes to GitHub we will have a failing workflow. Now, let's add gem 'rspec-rails' to the gemfile like so:

 group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  gem 'rspec-rails'
end
Enter fullscreen mode Exit fullscreen mode

Add some models and controllers to your app and write tests against them.

8. Add Code Coverage.

  - name: Create Coverage Artifact
        uses: actions/upload-artifact@v2
        with:
          name: code-coverage
          path: coverage/
Enter fullscreen mode Exit fullscreen mode

and in the gemfile add gem 'simplecov', require: false

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver'
  gem 'webdrivers'
  gem 'simplecov', require: false
end

Enter fullscreen mode Exit fullscreen mode

This will generate a coverage report from the testing environment.

Here is the complete workflow file.

name: CI
on:

  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  test:
    runs-on: ubuntu-latest

    services: 
      postgres:
        image: postgres:12
        env:
          POSTGRES_PASSWORD: postgres
        ports: ['5432:5432']

    steps:
      - uses: actions/checkout@v2
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1.80.0
        with:
          ruby-version: 2.6.6

      - uses: Borales/actions-yarn@v2.3.0
        with:
          cmd: install

      - name: Install Dependencies
        run: |
          sudo apt install -yqq libpq-dev
          gem install bundler

      - name: Install Gems
        run: |
          bundle install

      - name: Setup database
        env:
          PG_DATABASE: postgres
          PG_HOST: localhost
          PG_USER: postgres
          PG_PASSWORD: password
          RAILS_ENV: test
          WITH_COVERAGE: true
          DISABLE_SPRING: 1
        run: |
          bundle exec rails db:prepare
          bundle exec rake test

      - name: Build and test with rspec
        env:
          PG_DATABASE: postgres
          PG_HOST: localhost
          PG_USER: postgres
          PG_PASSWORD: password
          RAILS_ENV: test
        run: |
          bundle exec rspec spec

      - name: Create Coverage Artifact
        uses: actions/upload-artifact@v2
        with:
          name: code-coverage
          path: coverage/



Enter fullscreen mode Exit fullscreen mode

Now, commit this to the repository and push the file to GitHub. Go to the Actions tab, and in the Actions logs click on the latest build that outputs the name of each step in the workflow file. Be sure that the test runs successfully when you push a code to GitHub.

CONCLUSION

In this guide, we demonstrated how to set up a CI workflow using GitHub Actions. I hope you learned a lot, and thanks for following along.

REFERENCES

Top comments (1)

Collapse
 
pitosalas profile image
Pito Salas

I went looking for your article because my github action was failing with rails (activerecord) not succeeding to connect to postgres. Now that I am using your approach I still get this error. Have you ever seen it? Any idea?

Image description