我在 gitlab yml 文件中有以下阶段。
integration-tests:
image: mydocker-hub-id/mvn-intergration-tests-image:latest
stage: test
services:
- postgres
script:
- export PGPASSWORD=$POSTGRES_PASSWORD
- psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
- cd source_code
- pg_isready
- mvn verify -P test-ci -DskipUTs=true
当为命令 pg_isready 执行作业时,我有响应 5432 没有响应。当从每个集成测试执行测试时,我有错误:
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
此阶段使用的图像基于 postgres 图像构建,该图像另外安装了 maven 和 java 11。spring 应用程序无法与 postgres 服务器连接可能是什么问题。命令
postgres --version
显示正确的版本。还命令"psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
返回 OK 结果。app.properties 中的 Spring db 连接如下:
spring.datasource.url=jdbc:postgresql://localhost:5432/test
最佳答案
我在 gitlab 文档中找到了有关 postgres 服务配置的详细信息
https://docs.gitlab.com/ee/ci/services/postgres.html?fbclid=IwAR1eDlOQ4ACn6zqw1auIetB07JRcCGi1Pjl-hpfGBM45ujvkImm6fHSSbgg
db url 应该包含主机 postgres 的名称而不是 localhost。
关于spring - Gitlab CI 无法将 spring 应用程序连接到 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466025/