I have been battling GitHub actions for a while now. I had to switch to GitHub Action after using Travis for a while and cannot set it up to run tests for Rails application. The problem is that no database is started for Rails to connect to and create the database from migrations.
This is my workflow .yml file:
name: Ruby on Rails CI
on:
push:
branches: [ master, develop ]
pull_request:
branches: [ master, develop ]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6', '2.7', '3.0']
services:
postgres:
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
image: postgres:13
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7.3
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Install dependencies
run: |
yarn install
- name: Build and Test
run: |
sudo apt-get -yqq install libpq-dev
bundle exec rake db:migrate
bundle exec rake test
The error I am getting from the Actions tab:
rake aborted!
ActiveRecord::ConnectionNotEstablished: 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"?
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:83:in `rescue in new_client'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:77:in `new_client'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `public_send'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `new_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:926:in `checkout_new_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:905:in `try_to_checkout_new_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:866:in `acquire_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:588:in `checkout'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:428:in `connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1128:in `retrieve_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:327:in `retrieve_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:283:in `connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/tasks/database_tasks.rb:237:in `migrate'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:92:in `block (3 levels) in <main>'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `each'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `block (2 levels) in <main>'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
/opt/hostedtoolcache/Ruby/2.7.3/x64/bin/bundle:23:in `load'
/opt/hostedtoolcache/Ruby/2.7.3/x64/bin/bundle:23:in `<main>'
Caused by:
PG::ConnectionBad: 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"?
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/pg-1.2.3/lib/pg.rb:58:in `new'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/pg-1.2.3/lib/pg.rb:58:in `connect'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:78:in `new_client'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `public_send'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `new_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:926:in `checkout_new_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:905:in `try_to_checkout_new_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:866:in `acquire_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:588:in `checkout'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:428:in `connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1128:in `retrieve_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:327:in `retrieve_connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:283:in `connection'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/tasks/database_tasks.rb:237:in `migrate'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:92:in `block (3 levels) in <main>'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `each'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `block (2 levels) in <main>'
/home/runner/work/Escape-Room-Planner/Escape-Room-Planner/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
/opt/hostedtoolcache/Ruby/2.7.3/x64/bin/bundle:23:in `load'
/opt/hostedtoolcache/Ruby/2.7.3/x64/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Error: Process completed with exit code 1.
I have looked at multiple videos and Stack Overflow discussions regarding this issue, and nothing worked so far. Any help is appreciated.
Try to pass correct test db config ENV. Check and set your variables in database.yml
for test db.
test:
<<: *default
database: <%= ENV.fetch('PG_DATABASE', 'postgres') %>
host: <%= ENV.fetch('PG_HOST', 'localhost') %>
username: <%= ENV.fetch('PG_USER', nil) %>
password: <%= ENV.fetch('PG_PASSWORD', nil) %>
port: <%= ENV.fetch('PG_PORT', 5432) %>
Or add test db config ENV to your workflow.yml
:
- name: Build and Test
env:
PG_DATABASE: postgres
PG_HOST: localhost
PG_PORT: 5432
PG_USER: postgres
PG_PASSWORD: postgres
RAILS_ENV: test
run: |
sudo apt-get -yqq install libpq-dev
bundle exec rake db:migrate
bundle exec rake test
Check working example.