GitLab CI:我的测试作业没有拾取mysql容器

时间:2017-12-24 02:54:46

标签: mysql docker spring-boot continuous-integration gitlab

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

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 hub中提取mysql图像,我不知道为什么?我认为mysql应该仅在连接作业上构建。

此外,在连接作业成功后,测试作业再次拉出mysql图像。我的测试工作无法识别我的数据库,但服务创建成功,我能够看到数据库也已创建。

构建作业控制台输出:

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

测试作业失败:

2017-12-24 02:21:43.827  INFO 62 --- [           main] c.g.w.b.xxxWebApplicationTests      : 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] c.g.w.b.xxxWebApplicationTests      : Running with Spring Boot v1.5.8.RELEASE, Spring v4.3.12.RELEASE
2017-12-24 02:21:43.829  INFO 62 --- [           main] c.g.w.b.xxxWebApplicationTests      : The following profiles are active: dev
2017-12-24 02:21:45.101  INFO 62 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : 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] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-12-24 02:21:51.533  WARN 62 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : maxIdle is larger than maxActive, setting maxIdle to: 50
2017-12-24 02:21:52.448 ERROR 62 --- [           main] o.a.tomcat.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] o.s.b.a.orm.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)

您能否告诉我为什么测试工作没有以正确的方式获取mysql服务?

最佳, ģ

1 个答案:

答案 0 :(得分:0)

  

当第一个作业运行时,它正在从中提取mysql图像   码头工人中心,我不知道为什么?

因为您将Mysql定义为服务。 GitLab CI使用services关键字来定义哪些docker容器应与基本映像链接。因此,它将在每个阶段运行。

  

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

您的gitlab配置文件中的数据库名称是&#39; gyyconsortiumdb&#39;这与您在spring配置文件中定义的不同,因为日志显示您正在尝试连接到&#39; mydb&#39;

jdbc4.MySQLSyntaxErrorException:未知数据库&#39; mydb&#39;

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