我正在尝试为我的Spring启动应用程序进行自动构建和测试。
这是application.yml
server:
port: 8080
use-forward-headers: true
display-name: targcontrol-cloud-employee-management
spring:
application:
name: employee-management
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/targcontrol_cloud_employee_management
username: postgres
password: postgres
jpa:
show-sql: true
hibernate:
ddl-auto: create
database-platform: org.hibernate.dialect.PostgreSQLDialect
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
我有多个测试需要Postgres(带数据库)和Rabbit才能在测试之前启动。
如果我使用docker命令在本地计算机上启动所有内容:
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=targcontrol_cloud_employee_management postgres
docker run -d -p 5672:5672 -p 15672:15672 rabbitmq
一切正常,弹簧靴可以看到Postgres。
这是我的管道jenkins文件
node {
stage 'Checkout'
checkout scm
stage 'Build'
sh 'mvn -B -DskipTests clean package'
stage 'Test'
docker.image('postgres').withRun('-p 5432:5432 -e "POSTGRES_PASSWORD=postgres" -e "POSTGRES_DB=targcontrol_cloud_employee_management"') { c ->
sleep 60;
sh 'mvn test'
}
post {
always {
junit 'target/surefire-reports/*.xml'
}
}
}
如果我在Jenkins中运行该配置,我可以看到错误:
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。 詹金斯本身也是在码头推出的。
sudo docker run -d -p 8080:8080 -p 50000:50000 -v /home/jenkins:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -u root jenkins