简体   繁体   中英

PG::ConnectionBad on Github Actions for Rails

I'm in a problem right now and don't know how to solve this in Github Actions context.

Side context: I'm following a tutorial on setting up Rails-Postgres with GH actions for CI.

Normally when I run into this problem in dev environment, I'd delete a postmaster.pid file. But here, because I'm in a test environment in GH actions, I'm not sure how to solve it. I tried already doing how I would do it in dev env, it unfortunately did not work.

The solutions I've tried:

  • rm /usr/local/var/postgres/postmaster.pid

  • Changing the ports within postgres entry in the workflow

My GH Actions Workflow:

name: Test

on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:12.1
        ports:
          - 6543:6543
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Install Ruby version specified in `.ruby-version`
        uses: eregon/use-ruby-action@master

      - name: Install required apt packages
        run: |
          sudo apt-get -y install libpq-dev

      - name: Setup cache key and directory for gems cache
        uses: actions/cache@v1
        with:
          path: vendor/bundle
          key: ${{ runner.os }}-gem-use-ruby-${{ hashFiles('**/Gemfile.lock') }}

      - name: Read Node.js version to install from `.nvmrc`
        run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
        id: nvm

      - name: Install required Node.js version
        uses: actions/setup-node@v1
        with:
          node-version: "${{ steps.nvm.outputs.NVMRC }}"

      - name: Get Yarn cache directory path
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Setup cache key and directory for node_modules cache
        uses: actions/cache@v1
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}

      - name: Bundle install
        run: |
          gem install bundler -v2.1.4
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3

      - name: Yarn install
        run: yarn --frozen-lockfile

      - name: Run RSpec // ERRORS HERE
        run: |
          RUBYOPT='-W:no-deprecated -W:no-experimental' bundle exec rails db:prepare
          RUBYOPT='-W:no-deprecated -W:no-experimental' bundle exec rspec

It breaks on the Run RSpec task.

Error message:

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"?

Thanks for your help everyone😁

Found a fix!

I needed to add the host , username , and password entry in config/database.yml for the test DB

Because you specified the port for 6543, so PostgreSQL cannot find its default port 5432.

To fix this, specify the port under

services:
  postgres:
    ports:
      - 5432/tcp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM