Github操作工作流程PG :: ConnectionBad:无法连接到服务器:运行bundle exec rake时没有此类文件或目录

时间:2020-07-07 20:26:50

标签: ruby-on-rails ruby github github-actions

这是我的第一个问题。

我正在尝试将github actions集成到我的rails项目中。

我的ruby.yml是这样的:

name: Ruby

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

jobs:
  test:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Ruby
      uses: ruby/setup-ruby@ec106b438a1ff6ff112340de34ddc62c540232e0
      with:
        ruby-version: 2.6.5
    - name: Install dependencies
      run: bundle install
    - name: Run tests
      run: bundle exec rake

bundle步骤要花费很多时间,我不知道是否可以只加载Gemfile.lock而不是每次都运行bundle。但运行正常。

问题在于测试步骤。

这是错误:

rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
/home/runner/work/project/project/test/test_helper.rb:10:in `<main>'
/home/runner/work/project/project/test/application_system_test_case.rb:1:in `<main>'
/home/runner/work/project/project/test/system/create_practice_test.rb:1:in `<main>'
/home/runner/work/GPLM/GPLM/bin/rails:9:in `<top (required)>'
/home/runner/work/GPLM/GPLM/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => test:system
(See full trace by running task with --trace)
Coverage report generated for Minitest to /home/runner/work/project/project/coverage. 31 / 3352 LOC (0.92%) covered.
SimpleCov failed with exit 1
##[error]Process completed with exit code 1.

谢谢大家。

1 个答案:

答案 0 :(得分:1)

您需要将Postgres服务添加到GitHub Action配置中。例如:

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

bundle install之前,您需要安装依赖库:

- name: Install library for postgres
  run: sudo apt-get install libpq-dev

别忘了设置数据库:

- name: Setup Database
  run: |
    cp config/YOUR_GITHUB_ACTIONS_DATABASE.yml config/database.yml
    bundle exec rails db:setup
  env:
    POSTGRES_PASSWORD: postgres
    POSTGRES_USER: postgres
    RAILS_ENV: test

当然,在这里,您必须根据具体情况定制命令。 最简单的方法是为GitHub Actions提供一个单独的数据库配置文件。但是您也可以为CI提供特殊的环境。