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

lux*_*lux 2 ruby ruby-on-rails 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
Run Code Online (Sandbox Code Playgroud)

捆绑步骤需要很多时间,我不知道是否可以只加载 Gemfile.lock 而不是每次都运行捆绑。但运行起来没问题。

问题是测试步骤。

所以这是错误:

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.
Run Code Online (Sandbox Code Playgroud)

谢谢大家。

小智 5

您需要将 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
Run Code Online (Sandbox Code Playgroud)

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

- name: Install library for postgres
  run: sudo apt-get install libpq-dev
Run Code Online (Sandbox Code Playgroud)

不要忘记设置数据库:

- 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
Run Code Online (Sandbox Code Playgroud)

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