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.
如果能建立有效的数据库连接,测试应该通过。