Rails 可以通过 GitHub 个操作连接到 PostgreSQL

Rails can connect to PostgreSQL with GitHub Actions

我已经与 GitHub 行为斗争了一段时间了。在使用 Travis 一段时间后,我不得不切换到 GitHub Action,并且无法将其设置为 Rails 应用程序的 运行 测试。问题是 Rails 没有启动数据库以连接到迁移并从迁移创建数据库。

这是我的工作流 .yml 文件:

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

我从“操作”选项卡中得到的错误:

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.

我看过多个关于这个问题的视频和 Stack Overflow 讨论,但到目前为止没有任何效果。感谢任何帮助。

尝试通过正确的测试数据库配置环境。在 database.yml 中检查并设置测试数据库的变量。

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) %>

或将测试数据库配置 ENV 添加到您的 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

检查工作 example.