如何在GithHub Actions中连接到Postgres [英] How to connect to Postgres in GithHub Actions

查看:59
本文介绍了如何在GithHub Actions中连接到Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ruby on Rails应用程序进行CI的GitHub Actions.

I am trying GitHub Actions for CI with a Ruby on Rails application.

我的设置是使用VM,而不是在容器中运行Ruby构建.

My setup is with VM, not running the Ruby build in a container.

这是我的工作流程yml.它一直运行直到出现"Setup Database"步骤为止.

This is my workflow yml. It runs all the way without errors until the step "Setup Database".

name: Rails CI

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master

jobs:
  build:

    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:10.10
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: db_test
        ports:
          - 5432/tcp
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
      redis:
        image: redis:latest
        ports:
          - 6379/tcp

    steps:
    - uses: actions/checkout@v1

    - name: Set up ruby 2.5
      uses: actions/setup-ruby@v1
      with:
        ruby-version: 2.5.5

    - name: Set up node 8.14
      uses: actions/setup-node@v1
      with:
        node-version: '8.14'

    - name: Setup system dependencies
      run: sudo apt-get install libpq-dev

    - name: Setup App Dependencies
      run: |
        gem install bundler -v 1.17.3 --no-document
        bundle install --jobs 4 --retry 3
        npm install
        npm install -g yarn

    - name: Run rubocop
      run: bundle exec rubocop

    - name: Run brakeman
      run: bundle exec brakeman

    - name: Setup Database
      env:
        RAILS_ENV: test
        POSTGRES_HOST: localhost
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
        POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
      run: |
        cp config/database.yml.ci config/database.yml
        bundle exec rails db:create
        bundle exec rails db:schema:load

    - name: Run rspec
      env:
        RAILS_ENV: test
        REDIS_HOST: redis
        REDIS_PORT: ${{ job.services.redis.ports[6379] }}
        POSTGRES_HOST: localhost
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
        POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
      run: bundle exec rspec --tag ~type:system

我能够安装ruby,node,图像,Postgres即服务等,并运行Rubocop和Brakeman.但是,当我尝试在运行Rspec之前设置数据库时,它说它无法连接到数据库.

I am able to install ruby, node, the images, Postgres as a service, etc, and run Rubocop and Brakeman. But when I try to set up the DB before running Rspec it says it cannot connect to the DB.

据我所知,运行VM配置而不是容器配置时,主机是localhost.

As far as I've been able to ascertain, the host is localhost when running the VM configuration as opposed to a container configuration.

这是设置数据库"步骤复制到Rails使用的database.yml的database.yml.ci.

This is the database.yml.ci that the "Setup Database" step copies to the database.yml to be used by Rails.

test:
  adapter: postgresql
  encoding: unicode
  database: db_test
  pool: 5
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>
  host: <%= ENV['POSTGRES_HOST'] %>

我希望Postgres可以正确设置,并且 bundle exec rails db:create 可以创建数据库.但是,它引发以下错误:

I expected Postgres to be correctly set up and bundle exec rails db:create to create the database. However, it throws the following error:

rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

我尝试了各种不同的配置,但不幸的是,动作"已经广为人知,而且在线上似乎没有很多资料.

I've tried all sorts of different configurations, but unfortunately, Actions is sort of knew and there doesn't seem to be a lot of material available online.

关于如何解决此问题的任何想法?

Any ideas on how to fix this?

==========================

===========================

因此,我能够通过反复试验来解决这个问题.我最终使用了带有ruby和node容器的docker镜像.这是工作配置:

So I was able to sort this out through trial and error. I ended up using a docker image with a ruby and node container. This is the working configuration:

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master
    - development
    - release

jobs:
  build:

    runs-on: ubuntu-latest

    container:
      image: timbru31/ruby-node:latest

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

    chrome:
        image: selenium/standalone-chrome:latest
        ports:
          - 4444:4444

    steps:
    - uses: actions/checkout@v1

    - name: Setup app dependencies
      run: |
        gem install bundler -v 1.17.3 --no-document
        bundle install --jobs 4 --retry 3
        npm install
        npm install -g yarn

    - name: Run rubocop
      run: bundle exec rubocop

    - name: Run brakeman
      run: bundle exec brakeman

    - name: Setup database
      env:
        RAILS_ENV: test
        POSTGRES_HOST: postgres
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
        POSTGRES_DB: ci_db_test
        POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
      run: |
        cp config/database.yml.ci config/database.yml
        bundle exec rails db:create
        bundle exec rails db:schema:load

    - name: Run rspec
      env:
        RAILS_ENV: test
        POSTGRES_HOST: postgres
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
        POSTGRES_DB: ci_db_test
        POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
        SELENIUM_URL: 'http://chrome:4444/wd/hub'
      run: bundle exec rspec

和CI DB配置数据库.yml.ci

And the CI DB configuration database.yml.ci

default: &default
  adapter: postgresql
  encoding: unicode
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>
  host: <%= ENV['POSTGRES_HOST'] %>
  pool: 5
  database: <%= ENV['POSTGRES_DB'] %>

test:
  <<: *default

推荐答案

我的设置略有不同,但是当我遇到相同的错误时,这是​​最相关的问题,因此希望在此处发布,以帮助您解决.对我来说至关重要的两件事是:1)设置 DB_HOST = localhost 2)使用Rails应用程序启动Docker容器时,设置-network ="host" 参数

I have a slightly different setup but this was the most relevant question when I encountered the same error so wanted to post here in case it can help. The two things that were critical for me were: 1) Set the DB_HOST=localhost 2) Set the --network="host" argument when you start the docker container with your rails app

name: Master Build

on: [push]

env:
  registry: my_registry_name
  # Not sure these are actually being passed down to rails, set them as the default in database.yml
  DB_HOST: localhost
  DB_USERNAME: postgres
  DB_PASSWORD: postgres

jobs:
  my_image_test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_DB: postgres        
          POSTGRES_PASSWORD: postgres
          POSTGRES_USER: postgres
        ports:
          - 5432:5432
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Check out repository
        uses: actions/checkout@v2
      - name: Build my_image docker image
        uses: whoan/docker-build-with-cache-action@v5
        with:
          username: "${{secrets.aws_ecr_access_key_id}}"
          password: "${{secrets.aws_ecr_secret_access_key}}"
          registry: "${{env.registry}}"
          image_name: my_image
          context: my_image
      - name: Lint rubocop
        working-directory: ./my_image
        run: docker run $registry/my_image bundle exec rubocop
      - name: Run rails tests
        working-directory: ./my_image
        run: docker run --network="host" $registry/my_image bash -c "RAILS_ENV=test rails db:create && RAILS_ENV=test rails db:migrate && rails test"

这篇关于如何在GithHub Actions中连接到Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆