Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitlab-ci docker executor | how to setup mysql service?

I'm trying to get a gitlab-ci build going, but I'm having trouble with the mysql setup portion. When I commit, the mysql script seems to run OK, but then the application build fails on connecting to the db. What am I missing here?

.gitlab-ci.yml

stages:
  - prepare
  - test

services:
  - mariadb

variables:
  MYSQL_ROOT_PASSWORD: "password"

connect:
  stage: prepare
  image: mysql
  script:
  - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mariadb < "data/db/scripts/create-db.sql"
  - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mariadb < "data/db/scripts/init-db-tables.sql"

dev:
  stage: test
  image: java:opendjdk-8
  script:
    - ./gradlew assemble
    - ./gradlew check

data source config

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setUrl("jdbc:mysql://mariadb:3306/rvep");
    dataSource.setUsername("root");
    dataSource.setPassword("password");
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");

    return dataSource;
}

build errors result of hibernate config not hitting db

io.abnd.rvep.RVEPTests > contextLoads FAILED
    java.lang.IllegalStateException
        Caused by: org.springframework.beans.factory.BeanCreationException
            Caused by: org.springframework.beans.factory.BeanCreationException
                Caused by: org.springframework.beans.BeanInstantiationException
                    Caused by: org.hibernate.HibernateException

io.abnd.rvep.security.rest.TestControllerTest > findAllRoleCategories FAILED
    java.lang.IllegalStateException
        Caused by: org.springframework.beans.factory.BeanCreationException
            Caused by: org.springframework.beans.factory.BeanCreationException
                Caused by: org.springframework.beans.BeanInstantiationException
                    Caused by: org.hibernate.HibernateException

io.abnd.rvep.security.rest.TestControllerTest > findAllRoles FAILED
    java.lang.IllegalStateException
        Caused by: org.springframework.beans.factory.BeanCreationException
            Caused by: org.springframework.beans.factory.BeanCreationException
                Caused by: org.springframework.beans.BeanInstantiationException
                    Caused by: org.hibernate.HibernateException
like image 829
oxenfree Avatar asked Nov 23 '25 10:11

oxenfree


2 Answers

First make sure to read carefully the "What is service" and "How is service linked to the build" sections in documentation: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-service

In your configuration, you are starting the mysql service in these two lines:

services:
  - mysql

Thanks to this in your build containers you can use a mysql service which is available on standard port by the hostame mysql.

Here:

connect:
  image: mysql
  script:
  - service mysql start
  - echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('password');" | mysql -u root
  - echo "SELECT 'OK';" | mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -h mysql "$MYSQL_DATABASE"
  - mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -h localhost < "data/db/scripts/create-db.sql"
  - mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -h localhost < "data/db/scripts/init-db-tables.sql"

You are starting a new build inside of a container started from mysql image and prepare a database state locally! None of those commands even touch your mysql service.

Instead of this you should run the connect job inside of a container that will have a mysql client on board (it can be also this mysql one), but run all commands on a host mysql and not on a host localhost.

Second thing is that you are running those two jobs - connect and dev in one stage, so they will be executed in parallel (but with a limitation set in the runner's configuration).

You should add first - as a top-level entry:

stages:
  - prepare
  - test

and then modify those two jobs to:

connect:
  stage: prepare
  (...)

dev:
  stage: test
  (...)

After this jobs from the first stage (prepare) will be executed in parallel, then jobs from the second stage (test) will be executed in parallel etc.

like image 58
Tomasz Maczukin Avatar answered Nov 26 '25 00:11

Tomasz Maczukin


It seems installing mysql-client through apt-get, and combining scripts helps. Here is my .gitlab-ci.yml for now. build passing.

variables:
  MYSQL_ROOT_PASSWORD: "password"

dev:
  image: java:openjdk-8
  services:
    - mariadb
  script:
    - apt-get update && apt-get --assume-yes install mysql-client
    - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mariadb < "data/db/scripts/create-db.sql"
    - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mariadb < "data/db/scripts/init-db-tables.sql"
    - ./gradlew assemble check --info
like image 38
oxenfree Avatar answered Nov 26 '25 00:11

oxenfree