Ruby on Rails GitHub Action Workflow

The goal is to start running tests that is part of my code base with each push to main and also any pull requests to main branch. This blog post will not cover any linting or deployment of the ruby on rails web application. The final yaml is tested w…


This content originally appeared on DEV Community and was authored by Harish Babu

Ruby on Rails GitHub Action Workflow

The goal is to start running tests that is part of my code base with each push to main and also any pull requests to main branch. This blog post will not cover any linting or deployment of the ruby on rails web application. The final yaml is tested with ruby version 3.0.0 and ruby on rails version 6.1.3.1 at the time of writing. Skip to end of the post to view the finished yaml.

We will start with recommended ruby starter workflow yaml file from GitHub actions page. The starter file provides you with a matrix with various ruby versions. Because, the workflow I am setting up is for a web application that will be deployed to an environment with a specific ruby version install, I will delete the strategy section completely and also specify my ruby version to 3.0.

runs-on: ubuntu-latest
steps:
    - uses: actions/checkout@v2
    - name: Set up Ruby
      uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
      with:
        ruby-version: 3.0
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically

I am planning on deploying the application in a docker container running on linux. Therefore, I've set my build environment to be an ubuntu base image. If you are developing on something other than linux (which is my case), you will also need to add support for linux with your bundle.

bundle lock --add-platform x86_64-linux

After the ruby setup step, the next step thats already provided is the test step with bundle rake command. I have my project setup to run with mysql database. Therefore, we need to define a service for mysql for the tests to pass. Read more about service containers at GitHub documentation site.

  test:
    ...
    services:
      # Label used to access the service container
      mysql:
        # Docker Hub image and tag
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: my-secret-password
          MYSQL_DATABASE: <db_name> # replace with actual name
          MYSQL_USER: test_user
          MYSQL_PASSWORD: password
        # Set health checks to wait until mysql has started
        options: >-
          --health-cmd "mysqladmin ping -h localhost -u root -p$$MYSQL_ROOT_PASSWORD"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      ...

Now a container will be spun up my GitHub action and made available on the port you specified. The options section specifies that the service is only ready when mysqladmin ping command succeeds. The next step is to setup the database required for your tests. To do that, we will make use of the DATABASE_URL environment variable and the bin/rails db:setup command. Make sure to use the prefix bin. You can read more about configuration and order of preference at ruby on rails documentation site.

  - name: Setup test database
      run: bin/rails db:setup
      env:
        DATABASE_URL: mysql2://test_user:password@127.0.0.1:3306/<db_name>

Make sure that DATABASE_URL environment variable starts with mysql2. For some reason, I can't seem to use localhost for the hostname part. If someone could point me to the reason, that will be much appreciated ?.

After the database is setup, you might also have to make sure that webpacker is installed. Refer to the documentation for up to date information.

  - name: Install Webpacker
    run: bin/rails webpacker:install

The final yaml file...

name: Ruby

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:

    runs-on: ubuntu-latest

    services:
      # Label used to access the service container
      mysql:
        # Docker Hub image and tag
        image: mysql:8.0
        ports:
          - 3306:3306
        env:
          MYSQL_ROOT_PASSWORD: my-secret-password
          MYSQL_DATABASE: <db_name> #replace with name
          MYSQL_USER: test_user
          MYSQL_PASSWORD: password
        # Set health checks to wait until mysql has started
        options: >-
          --health-cmd "mysqladmin ping -h localhost -u root -p$$MYSQL_ROOT_PASSWORD"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - uses: actions/checkout@v2
    - name: Set up Ruby
      uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
      with:
        ruby-version: 3.0
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically

    - name: Setup test database
      run: bin/rails db:setup
      env:
        DATABASE_URL: mysql2://test_user:password@127.0.0.1:3306/<db_name>

    - name: Install Webpacker
      run: bin/rails webpacker:install

    - name: Run tests
      run: bundle exec rake


This content originally appeared on DEV Community and was authored by Harish Babu


Print Share Comment Cite Upload Translate Updates
APA

Harish Babu | Sciencx (2021-04-23T22:36:43+00:00) Ruby on Rails GitHub Action Workflow. Retrieved from https://www.scien.cx/2021/04/23/ruby-on-rails-github-action-workflow/

MLA
" » Ruby on Rails GitHub Action Workflow." Harish Babu | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/ruby-on-rails-github-action-workflow/
HARVARD
Harish Babu | Sciencx Friday April 23, 2021 » Ruby on Rails GitHub Action Workflow., viewed ,<https://www.scien.cx/2021/04/23/ruby-on-rails-github-action-workflow/>
VANCOUVER
Harish Babu | Sciencx - » Ruby on Rails GitHub Action Workflow. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/ruby-on-rails-github-action-workflow/
CHICAGO
" » Ruby on Rails GitHub Action Workflow." Harish Babu | Sciencx - Accessed . https://www.scien.cx/2021/04/23/ruby-on-rails-github-action-workflow/
IEEE
" » Ruby on Rails GitHub Action Workflow." Harish Babu | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/ruby-on-rails-github-action-workflow/. [Accessed: ]
rf:citation
» Ruby on Rails GitHub Action Workflow | Harish Babu | Sciencx | https://www.scien.cx/2021/04/23/ruby-on-rails-github-action-workflow/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.