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'] }}
此设置将导致以下error:
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.
如果可以建立有效的数据库连接,则测试应通过。
答案 0 :(得分:2)
我遇到了同样的问题,在经过反复试验 后,通过GitHub的example找到了这个code search。
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容器将被关闭,并且无法用于测试。