2

I'm currently writing a .gitlab-ci.yml file to automate the build, test, package, and deployment of my application, but the build job is passing, but the other jobs (test, deployment) are failing.

My app is a Springboot web app, with a Mysql Database to store the data and I'm using maven to build the project.

Below is my .gitlab-ci.yml file

before_script:
 - echo "Execute scripts which are required to bootstrap the application. !"

after_script:
 - echo "Clean up activity can be done here !."

stages:
 - build
 - test
 - package
 - deploy

variables:
 MAVEN_CLI_OPTS: "--batch-mode"
 MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
 paths:
  - .m2/repository/
  - target/

build:
 stage: build
 image: maven:latest
 script:
  - mvn $MAVEN_CLI_OPTS clean compile

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

The error I'm receiving is the following:

2017-12-23 10:00:33.854  WARN 65 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : maxIdle is larger than maxActive, setting maxIdle to: 50
2017-12-23 10:00:34.567 ERROR 65 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. 
The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2222)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2017)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

Could you please know what the issue is ?

Thanks, G.

yimson
  • 49
  • 2
  • 14
  • can you locally run `mvn test` ? Maybe the problem is with the auto generated test from SpringBoot which tries to load the whole Spring context and therefor the app wants to connect to your database. As there is no database in your CI environment, your test fails. – rieckpil Dec 23 '17 at 11:15
  • @rieckpil Yes, locally everything is running fine. Test is running well locally. The problem is not the autogenerated test. I have a bunch of unit tests that I implemented. All of them are running fine locally. – yimson Dec 23 '17 at 11:19
  • try to stop your local mysql and run the test again – rieckpil Dec 23 '17 at 11:19
  • Just tried to stop and the tests are still running. – yimson Dec 23 '17 at 11:31
  • you "tried" to stop? Did your database shut down or not? x) – rieckpil Dec 23 '17 at 11:31
  • @rieckpil Ooooohhhh, I shut down the database, and the mvn test failed... – yimson Dec 23 '17 at 11:33

2 Answers2

0

Find your Spring context test and disable it (e.g. comment it out or use JUnit's @Ignore on the test classfor a new try for your pipeline. The text may look like:

@RunWith(SpringRunner.class)
@SpringBootTest
public class YourApplicationTest {

    @Test
    public void contextLoads() {
    }

}

Your exception is because thrown because your database can't be accessed during your tests. A detailed explanation and some trouble shooting can be found here: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • I updated that test by ignoring it as you requested me to do. I gave another try to the pipeline and I'm still having the same issue, the same error message I posted before. I understand that the problem is because the database can't be accessed, is there a way to specify another database instance in the .gitlab-ci.yml file. – yimson Dec 23 '17 at 11:51
  • do you have other tests where you load the Spring context? If you want you can publish your project to GitHub and I'll have look at it. – rieckpil Dec 23 '17 at 12:20
  • The problem is that I want to create a docker container with a mysql instance on it and then the test will run fine. I don't think that the problem is related to the context. I have a bunch of tests. Could you show me how I can run/specify a docker image with maven and mysql in the test job so that the test job can run properly ? – yimson Dec 23 '17 at 12:33
  • @charli, can you please assist on this issue ? – yimson Dec 24 '17 at 00:00
0

Well ok, it's a bit lately... but i assume you have no MySQL database for your build process (integration tests). Thats why you get a CommunicationsException. I've got the same problem. I try to setup a gitlab-pipeline, which can execute integration tests (not workin' yet)

That's what i've found

  1. https://docs.gitlab.com/ce/ci/services/mysql.html
  2. https://gitlab.com/gitlab-examples/mysql
SleepyX667
  • 670
  • 9
  • 21