我正在为我的 Spring Boot Java 应用程序设置集成测试环境。docker-maven-plugin
所以我正在使用我的应用程序应该调用的 PostgreSQL 数据库。
此设置在我的本地系统上运行时运行正常mvn verify
,但在 GitLab CI 运行器上失败,因为 PostgreSQL 主机名不再匹配。
我的问题是,如何在 CI 管道中获取数据库容器的主机名?
我的插件配置如下所示:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.maven.plugin.version}</version>
<executions>
<execution>
<id>prepare-it-database</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<images>
<image>
<name>postgres:9.6.14</name>
<alias>it-database</alias>
<run>
<ports>
<port>it-database.port:5432</port>
</ports>
<wait>
<log>(?s)database system is ready to accept connections.*database system is ready to accept connections</log>
<time>20000</time>
</wait>
<log>
<prefix>POSTGRESQL-TEST</prefix>
<date>ISO8601</date>
<color>blue</color>
</log>
</run>
</image>
</images>
</configuration>
</execution>
<execution>
<id>remove-it-database</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
最终,我将不得不在我的application yaml
:
spring:
datasource:
platform: postgresql
url: jdbc:postgresql://<db_host_name_here_once_i_get_it>:${it-database.port}/postgres
username: postgres
password: postgres
schema: classpath:schema-postgresql.sql
data: classpath:data-postgresql.sql
driver-class-name: org.postgresql.Driver
initialization-mode: always