DEV Community

Luis Ortega
Luis Ortega

Posted on

Setting up GitHub Actions with Rails 6.1 and MySQL 5.7

I'm actually new to this technology, but I've been working with it for a week and I have to say it's really handy. When you integrate GitHub Actions into your workflow, you can stop worrying about setting up your own custom CI server, at least for testing.

I've been working it with a Rails 6.1 and a MySQL 5.6 database engine.

It takes little time and effort and in return you have a full testing environment ready to work on the same place you have your code. So no need for API keys, no integrations with other systems, keeping everything simple.

I've done this setup with Rails & Postgres and had no issue, but had a hard time looking how others have done it with Rails & MySQL. Because of this I thought of sharing how I did it.

 My working example

.github/workflow/ci.yml file:


name: github-actions-rails-mysql

on:
  pull_request:
    branches: ['*']
  push:
    branches: [master]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_HOST: 127.0.0.1
          MYSQL_DB: github_actions_test
          MYSQL_USER: root
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_PASSWORD:
        ports:
          - "3306:3306"
    steps:
      - uses: actions/checkout@v2
      - name: Setup Ruby 2.7
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.7
          bundler: default
          bundler-cache: true
      - name: Run tests
        env:
          RAILS_ENV: test
          DATABASE_PORT: 3306
        run: |
          bin/rails db:create
          bin/rails db:schema:load
          bin/rails test

Enter fullscreen mode Exit fullscreen mode

My config/database.yml file:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: github_actions_development

test:
  <<: *default
  database: github_actions_test
  host: 127.0.0.1

production:
  <<: *default
  database: github_actions_production
Enter fullscreen mode Exit fullscreen mode

Top comments (0)