GitLab CI:我的testing作业不拾取MySQL容器

我有一个连接到mysql数据库的springboot应用程序。 我想使用.gitlab-ci.yml来处理pipe道。 我创build了以下configuration文件。

before_script: - echo "Execute scripts which are required to bootstrap the application. !" after_script: - echo "Clean up activity can be done here !." services: - mysql stages: - build - connect - test - package - deploy variables: MAVEN_CLI_OPTS: "--batch-mode" MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" MYSQL_DATABASE_NAME: gyyconsortiumdb MYSQL_DATABASE_SCHEMA: "$CI_PROJECT_DIR/src/main/resources/static/sql/gyyconsortiumdb.sql" MYSQL_ROOT_PASSWORD: mysql cache: paths: - .m2/repository/ - target/ build: stage: build image: maven:latest script: - mvn $MAVEN_CLI_OPTS clean compile connect: stage: connect image: mysql before_script: - mysql --version script: - echo "create database $MYSQL_DATABASE_NAME;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql $MYSQL_DATABASE_NAME < $MYSQL_DATABASE_SCHEMA - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql -e "show databases; use $MYSQL_DATABASE_NAME; show tables;" test: stage: test image: maven:latest script: - mvn $MAVEN_CLI_OPTS test package: stage: package image: maven:latest script: - mvn $MAVEN_CLI_OPTS package artifacts: paths: [target/basecamp-0.0.1.war] deploy_test: stage: deploy script: - echo "######## To be defined ########" environment: staging deploy_prod: stage: deploy script: - echo "######## To be defined ########" only: - master environment: production 

当第一个工作正在运行,它是从docker集线器拉动mysql映像,我不知道为什么? 我以为mysql应该只build立在连接作业上。

另外,连接作业成功后,testing作业再次拉取mysql映像。 而我的testing作业不能识别我的数据库,但是服务创build成功,我能够看到数据库也创build。

构build作业控制台输出:

 Running with gitlab-runner 10.3.0 (5cf5e19a) on docker-auto-scale (fa6cab46) Using Docker executor with image maven:latest ... Starting service mysql:latest ... Pulling docker image mysql:latest ... Using docker image mysql:latest ID=sha256:7d83a47ab2d2d0f803aa230fdac1c4e53d251bfafe9b7265a3777bcc95163755 for mysql service... Waiting for services to be up and running... Using docker image sha256:d6e999707ab00f954f4ab77c8ced4efce186099fb8318e1a0bc2f4fd8bb7bf6b for predefined container... Pulling docker image maven:latest ... Using docker image maven:latest ID=sha256:1f858e89a5843b0804a9e1498476a135f45e23a42fe673914f977e0882a6789e for build container... Running on runner-fa6cab46-project-4899225-concurrent-0 via runner-fa6cab46-srm-1514081729-3d654341... Cloning repository... Cloning into '/builds/user/xxx'... Checking out b9efb2da as 24-create-the-gitlab-ci-yml-file... Skipping Git submodules setup Checking cache for default... Downloading cache.zip from http://runners-cache-3-internal.gitlab.com:444/runner/project/4899225/default Successfully extracted cache $ echo "Execute scripts which are required to bootstrap the application. !" Execute scripts which are required to bootstrap the application. ! $ mvn $MAVEN_CLI_OPTS clean compile [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building xxx 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ xxx --- [INFO] Deleting /builds/user/xxx/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xxx --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 4 resources [INFO] Copying 108 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xxx --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 35 source files to /builds/user/xxx/target/classes [WARNING] /builds/user/xxx/src/main/java/com//www/xxx/domain/Project.java: Some input files use unchecked or unsafe operations. [WARNING] /builds/user/xxx/src/main/java/com/xxx/www/xxx/domain/Project.java: Recompile with -Xlint:unchecked for details. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.111 s [INFO] Finished at: 2017-12-24T02:18:07Z [INFO] Final Memory: 30M/71M [INFO] ------------------------------------------------------------------------ Running after script... $ echo "Clean up activity can be done here !." Clean up activity can be done here !. Creating cache default... .m2/repository/: found 1775 matching files target/: found 197 matching files Uploading cache.zip to http://runners-cache-3- internal.gitlab.com:444/runner/project/4899225/default Created cache Job succeeded 

连接作业控制台输出

 Running with gitlab-runner 10.3.0 (5cf5e19a) on docker-auto-scale (e11ae361) Using Docker executor with image mysql ... Starting service mysql:latest ... Pulling docker image mysql:latest ... Using docker image mysql:latest ID=sha256:7d83a47ab2d2d0f803aa230fdac1c4e53d251bfafe9b7265a3777bcc95163755 for mysql service... Waiting for services to be up and running... Using docker image sha256:295a9e80fe6ae475bfeef8e318eb11db267d317fa6bc1ed8d72185dfd2adb8b7 for predefined container... Pulling docker image mysql ... Using docker image mysql ID=sha256:7d83a47ab2d2d0f803aa230fdac1c4e53d251bfafe9b7265a3777bcc95163755 for build container... Running on runner-e11ae361-project-4899225-concurrent-0 via runner-e11ae361-srm-1514081811-5092ac06... Cloning repository... Cloning into '/builds/yimengael/basecamp'... Checking out b9efb2da as 24-create-the-gitlab-ci-yml-file... Skipping Git submodules setup Checking cache for default... Downloading cache.zip from http://runners-cache-5-internal.gitlab.com:444/runner/project/4899225/default Successfully extracted cache $ mysql --version mysql Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using EditLine wrapper $ echo "create database $MYSQL_DATABASE_NAME;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql mysql: [Warning] Using a password on the command line interface can be insecure. $ mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql $MYSQL_DATABASE_NAME < $MYSQL_DATABASE_SCHEMA mysql: [Warning] Using a password on the command line interface can be insecure. $ mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql -e "show databases; use $MYSQL_DATABASE_NAME; show tables;" mysql: [Warning] Using a password on the command line interface can be insecure. Database information_schema mydb mysql performance_schema sys Tables_in_mydb message persistent_logins project project_description role user user_role Running after script... $ echo "Clean up activity can be done here !." Clean up activity can be done here !. Creating cache default... .m2/repository/: found 1775 matching files target/: found 197 matching files Archive is up to date! Created cache Job succeeded 

testing作业失败:

 2017-12-24 02:21:43.827 INFO 62 --- [ main] cgwbxxxWebApplicationTests : Starting xxxWebApplicationTests on runner-4e4528ca-project-4899225-concurrent-0 with PID 62 (started by root in /builds/yimengael/xxx) 2017-12-24 02:21:43.829 DEBUG 62 --- [ main] cgwbxxxWebApplicationTests : Running with Spring Boot v1.5.8.RELEASE, Spring v4.3.12.RELEASE 2017-12-24 02:21:43.829 INFO 62 --- [ main] cgwbxxxWebApplicationTests : The following profiles are active: dev 2017-12-24 02:21:45.101 INFO 62 --- [ main] oswcsGenericWebApplicationContext : Refreshing org.springframework.web.context.support.GenericWebApplicationContext@62e20a76: startup date [Sun Dec 24 02:21:45 UTC 2017]; root of context hierarchy 2017-12-24 02:21:49.515 INFO 62 --- [ main] faAutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 2017-12-24 02:21:51.533 WARN 62 --- [ main] oatomcat.jdbc.pool.ConnectionPool : maxIdle is larger than maxActive, setting maxIdle to: 50 2017-12-24 02:21:52.448 ERROR 62 --- [ main] oatomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'mydb' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ..... 2017-12-24 02:21:52.477 WARN 62 --- [ main] osbaorm.jpa.DatabaseLookup : Unable to determine jdbc url from datasource org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'mydb' at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:339) at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:366) at org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup.getDatabase(DatabaseLookup.java:72) at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties.java:139) 

你能告诉我为什么testing工作没有正确的方式拿起mysql服务吗?

最好的G

当第一个工作正在运行,它是从docker集线器拉动mysql映像,我不知道为什么?

因为您将Mysql定义为服务。 GitLab CI使用services关键字来定义哪些docker容器应该与你的基础镜像链接。 所以,它会在每个阶段运行。

你能告诉我为什么testing工作没有正确的方式拿起mysql服务吗?

您的gitlabconfiguration文件中的数据库名称是'gyyconsortiumdb',它与您在springconfiguration文件中定义的不同,因为日志显示您正试图连接到'mydb'

jdbc4.MySQLSyntaxErrorException:未知数据库'mydb'

请点击链接获取更多信息https://docs.gitlab.com/ce/ci/services/mysql.html