我正在使用spring boot应用程序并创建一个映像并在docker中运行,当该映像从本地运行时,它运行良好。但是当在gitlab CI / CD中运行时,它会失败
这是我的控制器类:
server.port=8084
spring.datasource.url=${MYSQL_URL}
spring.datasource.username=${MYSQL_USER}
spring.datasource.password=${MYSQL_PASSSWORD}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
application.properties
export MYSQL_DATABASE=UserDb
export MYSQL_USER=root
export MYSQL_PASSSWORD=root123
export MYSQL_URL=jdbc:mysql://localhost:3306/UserDb
env-variable.sh
FROM java:8-jre
WORKDIR usr/src
ENV MYSQL_DATABASE=UserDb
ENV MYSQL_USER=approot
ENV MYSQL_PASSSWORD=root123
ENV MYSQL_URL=jdbc:mysql://localhost:3306/UserDb
ADD ./target/authenticationservice-0.0.1-SNAPSHOT.jar /usr/src/authenticationservice-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","authenticationservice-0.0.1-SNAPSHOT.jar"]
Dockerfile
version: '3'
services:
authService:
image: tshyamvamsi/authenticationservice:v1
restart: always
network_mode: host
container_name: authServiceCon
depends_on:
- mysql
ports:
- 8084:8084
mysql:
image: mysql:5.5
ports:
- 3306:3306
container_name: mysql_container
network_mode: host
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_DATABASE: UserDb
MYSQL_USER: approot
MYSQL_PASSWORD: root123
docker-compose.yml
image: 'maven:3-jdk-8'
stages:
- test
- build
services:
- 'mysql:5.5'
- 'mongo:3.4-jessie'
variables:
MYSQL_DATABASE: "userDb"
MYSQL_URL: "jdbc:mysql://mysql/userDb"
MYSQL_USER: "app_root"
MYSQL_PASSWORD: "root"
MYSQL_ROOT_PASSWORD: "root"
MONGO_DATABASENAME: "muzixdb"
MONGO_URL: "mongodb://mongo/muzixdb"
cache:
paths:
- .m2/repository
- target
maven-test:
stage: test
script: "mvn test"
maven-build:
stage: build
script: 'mvn package'
artifacts:
paths:
- target/*.jar
这是 gitlab-ci.yml .... CI / CD引擎正在从中读取凭据并失败
package com.sample.authenticationservice;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AuthenticationServiceApplicationTests {
@Test
public void contextLoads() {
}
}
这些是测试类:
AuthenticationServiceApplicationTests.java
package com.sample.authenticationservice.repository;
import com.sample.authenticationservice.domain.User;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
private User user;
@Before
public void setUp(){
user = new User();
user.setUsername("John123");
user.setPassword("John123");
}
@After
public void tearDown(){
userRepository.deleteAll();
user = null;
}
//userRepository.save(user);
@Test
public void testUserLoginSuccess(){
userRepository.save(user);
User userObj = userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());
Assert.assertEquals(user.getUsername(), userObj.getUsername());
userRepository.delete(user);
}
@Test
public void testSaveUserSuccess(){
userRepository.save(user);
User userObj = userRepository.findById(user.getUserId()).get();
Assert.assertEquals(user.getUsername(), userObj.getUsername());
userRepository.delete(user);
}
}
UserRepositoryTest.java
java.sql.SQLException: Access denied for user 'app_root'@'172.17.0.4' (using password: YES)
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta-data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'app_root'@'172.17.0.4' (using password: YES)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed;
[ERROR] testUserLoginSuccess(com.stackroute.authenticationservice.testSaveUserSuccess » IllegalState Failed to load Application Context
[ERROR] UserRepositoryTest.testUserLoginSuccess » IllegalState Failed to load Application Context
[INFO]
[ERROR] Tests run: 7, Failures: 0, Errors: 3, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for MuzixApplication 0.0.1-SNAPSHOT:
[INFO]
[INFO] MuzixApplication ................................... SUCCESS [ 0.011 s]
[INFO] UserTrackService ................................... SUCCESS [ 12.978 s]
[INFO] AuthenticationService .............................. FAILURE [ 20.222 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 33.743 s
[INFO] Finished at: 2019-06-16T21:36:46Z
[INFO] ------------------------------------------------------------------------
以下是我在gitlab CI / CD中看到的错误:
n