April 10, 2024

Ruby on Rails GitHub Actions con MySQL

Guía básica para crear tu CI con GitHub Actions y MySQL

Esta guía provee los elementos básicos para crear un GitHub Action que te permita correr las pruebas de un proyecto de Ruby on Rails 6.

Realmente verás que es muy sencillo, solo tienes que agregar los siguientes dos archivos en las rutas correspondientes.
// .github/workflows/ruby.yml

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: Ruby

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

jobs:
  test:

    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: app_db_test
        ports:
          - 3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

    steps:
      - uses: actions/checkout@v2

      - name: Set up Ruby 2.6.6
        uses: actions/setup-ruby@v1
        with:
          ruby-version: 2.6.6

      - name: Gem cache
        uses: actions/cache@v1
        with:
          path: vendor/bundle
          key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-gems-

      - name: Verify MySQL connection from host
        run: |
          sudo apt-get install -y mysql-client libmysqlclient-dev
          sudo /etc/init.d/mysql start
          mysql -h 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -u root -proot -e "CREATE DATABASE IF NOT EXISTS app_db_test;"

      - name: Yarn install
        uses: borales/actions-yarn@v2.0.0
        with:
          cmd: install

      - name: Bundle install, setup DB and run tests
        env:
          RAILS_ENV: test
          DB_PASSWORD: root
          DB_PORT: ${{ job.services.mysql.ports[3306] }}
        run: |
          cp config/database.yml.ci config/database.yml
          gem install bundler --version 2.1.4
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3
          bundle exec rails db:setup
          bundle exec rails test

    
        

// ./config/database.yml.ci

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: root

test:
  <<: *default
      database: app_db_test

Agregamos el archivo database.yml.ci ya que seguramente tus configuraciones locales o de tu servidor son diferentes al ambiente que crea la acción de GitHub. Solo asegúrate de tener las configuraciones adecuadas de acuerdo a tu proyecto. Por ejemplo tu el nombre de tu base de datos será muy diferente a app_db_test.