如何连接到GitHub Action的作业服务?

9

GitHub Actions允许您按每个作业运行后台服务。在遵循示例之后,我无法弄清如何连接到正在运行的PostgreSQL容器。

我在这个pull request中尝试了几种不同的方法,但都没有成功。

name: dinosql test suite
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432/tcp

    steps:
    - uses: actions/checkout@master

    - name: Test dinosql/ondeck
      run: go test -v ./...
      working-directory: internal/dinosql/testdata/ondeck
      env:
        PG_USER: postgres
        PG_DATABASE: postgres
        PG_PASSWORD: postgres
        PG_PORT: ${{ job.services.postgres.ports['5432'] }}

此设置会导致如下错误
Run go test -v ./...
=== RUN   TestQueries
=== PAUSE TestQueries
=== RUN   TestPrepared
=== PAUSE TestPrepared
=== CONT  TestQueries
=== CONT  TestPrepared
--- FAIL: TestPrepared (0.00s)
##[error]    db_test.go:212: db: postgres://postgres:postgres@127.0.0.1:32768/postgres?sslmode=disable
##[error]    db_test.go:212: dial tcp 127.0.0.1:32768: connect: connection refused
--- FAIL: TestQueries (0.00s)
##[error]    db_test.go:83: db: postgres://postgres:postgres@127.0.0.1:32768/postgres?sslmode=disable
##[error]    db_test.go:83: dial tcp 127.0.0.1:32768: connect: connection refused
FAIL
FAIL    example.com/ondeck  0.005s
?       example.com/ondeck/prepared [no test files]
##[error]Process completed with exit code 1.

如果能建立有效的数据库连接,测试应该通过。

2个回答

9

我也遇到了同样的问题,在经过很多尝试和错误之后,通过 GitHub 的 代码搜索,找到了这个示例

name: dinosql test suite
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432/tcp
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
    - uses: actions/checkout@master

    - name: Test dinosql/ondeck
      run: go test -v ./...
      working-directory: internal/dinosql/testdata/ondeck
      env:
        # use postgres for the host here because we have specified a contaienr for the job.
        # If we were running the job on the VM this would be localhost
        PG_HOST: postgres
        PG_USER: postgres
        PG_DATABASE: postgres
        PG_PASSWORD: postgres
        PG_PORT: ${{ job.services.postgres.ports['5432'] }}

添加健康检查选项并将数据库主机名从 127.0.0.1 更改为 postgres 应该可以解决问题。

似乎没有健康检查选项,postgres 容器将被关闭,无法用于测试。


1
感谢提供示例服务配置的链接。使用它,我成功解决了我的问题。您可以在此处查看提交记录here。不幸的是,您发布的YAML并没有解决这个问题。 - Kyle Conroy
将健康检查选项添加并将数据库主机名从127.0.0.1更改为postgres应该解决问题。我使用MySQL,并长时间遭受连接被拒绝的困扰。感谢您的答案,它通过将主机名从127.0.0.1更改为“mysql”来解决了我的问题。 - Veck Hsiao

0

如果您没有在容器中运行作业,例如在此示例中正在运行的VM ubuntu-latest上运行,则仍应使用localhost并映射端口。

services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_PASSWORD: postgres
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          # Maps tcp port 5432 on service container to the host
          - 5432:5432

https://docs.github.com/en/actions/using-containerized-services/creating-postgresql-service-containers#running-jobs-directly-on-the-runner-machine


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接