How to set up Github Actions for Rails with PostgreSQL

794 views Asked by At

I have an empty Rails 6 with PostgreSQL project on GitHub and I'm trying to configure GitHub Actions to run the Rails test suite whenever a pull request gets created.

I tried to follow this guide but somehow it doesn't work for me.

The pipeline fails on rails db:setup and complains that:

could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Which to me looks like for some reason the Rails app can't properly locate the PostgreSQL service.

This is my current GitHub actions config:

name: CI

on:
  pull_request:
    branches:
      - '*'
  push:
    branches:
      - master
jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:11
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: link_shortener
          POSTGRES_PASSWORD: link_shortener
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - uses: actions/checkout@master

      - name: Setup Ruby 2.7
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.7

      - name: Setup Node 12
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Install dependencies
        run: |
          sudo apt-get -yqq install libpq-dev
          bundle install
          yarn
          rails db:setup
      # Clean up git repo so the new rails template doesn't conflict
      - name: Remove git repo
        run: |
          rm -rf .git
      - name: Run tests
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        run: |
          rails t

And this is my Rails database config:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: link_shortener_development

test:
  <<: *default
  host: localhost
  database: link_shortener_test
  username: link_shortener
  password: link_shortener

production:
  <<: *default
  database: link_shortener_production
  username: link_shortener
  password: <%= ENV['LINK_SHORTENER_DATABASE_PASSWORD'] %>

If needed, the full GitHub repository is located at: https://github.com/dusan-rychnovsky/link-shortener

0

There are 0 answers