簡體   English   中英

GitHub Actions:如何在 GithHub Actions 中連接到 Postgres

[英]GitHub Actions: How to connect to Postgres in GithHub Actions

我正在使用 Ruby on Rails 應用程序嘗試 GitHub Actions for CI。

我的設置是使用 VM,而不是在容器中運行 Ruby 構建。

這是我的工作流程 yml。 它一直運行而沒有錯誤,直到步驟“設置數據庫”。

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​​、節點、圖像、Postgres 作為服務等,並運行 Rubocop 和 Brakeman。 但是當我在運行 Rspec 之前嘗試設置數據庫時,它說它無法連接到數據庫。

據我所知,在運行 VM 配置而不是容器配置時,主機是 localhost。

這是 database.yml.ci,“Setup Database”步驟復制到 database.yml 以供 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來創建數據庫。 但是,它會引發以下錯誤:

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?

我嘗試了各種不同的配置,但不幸的是,Actions 有點了解,而且網上似乎沒有很多可用的材料。

有想法該怎么解決這個嗎?

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

編輯:

所以我能夠通過反復試驗來解決這個問題。 我最終使用了帶有 ruby​​ 和 node 容器的 docker 鏡像。 這是工作配置:

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 配置 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 應用程序啟動--network="host"容器時設置--network="host"參數

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"

您的問題似乎是 Postgres 未在端口${{ job.services.postgres.ports[5432] }}上公開。嘗試將端口號替換為${{ job.services.postgres.ports[5432] }}

這里有例子: https : //github.com/actions/example-services/blob/master/.github/workflows/postgres-service.yml

我在嘗試為 Rails 應用程序設置 GitHub 操作時遇到了這個挑戰者。

這是對我有用的

name: Ruby

on:
  push:
    branches:
    - main
  pull_request:
    branches:
    - main

jobs:
  test:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        ruby-version:
        - '2.7.2'
        node-version:
        - '12.22'
        database-name:
        - my-app
        database-password:
        - postgres
        database-user:
        - postgres
        database-host:
        - 127.0.0.1
        database-port:
        - 5432

    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_DB: ${{ matrix.database-name }}
          POSTGRES_USER: ${{ matrix.database-user }}
          POSTGRES_PASSWORD: ${{ matrix.database-password }}
        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 Git Repository
      uses: actions/checkout@v2

    - name: Set up Ruby, Bundler and Rails
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: ${{ matrix.ruby-version }}
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically

    - name: Set up Node
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}

    - name: Install packages
      run: |
        yarn install --check-files

    - name: Setup test database
      env:
        RAILS_ENV: test
        DATABASE_NAME_TEST: ${{ matrix.database-name }}
        DATABASE_USER: ${{ matrix.database-user }}
        DATABASE_PASSWORD: ${{ matrix.database-password }}
        DATABASE_HOST: ${{ matrix.database-host }}
        DATABASE_PORT: ${{ matrix.database-port }}
        POSTGRES_DB: ${{ matrix.database-name }}
      run: |
        bundle exec rails db:migrate
        bundle exec rails db:seed

注意

  1. my-app替換為您的應用程序名稱。
  2. 您可以將database-passworddatabase-user保留為postgres

就這樣。

我希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM