6

I want to create the test database before my test script executes. I have included the mysql service, but I can't find a way to run mysql command.

I run mysql ... in before-script, but it keep complaining

/bin/bash: line 57: mysql: command not found

Here is my .gitlab-ci.yml

image: maven:3.5-jdk-8

services:
  - mysql

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  MYSQL_ROOT_PASSWORD: example

cache:
  paths:
    - .m2/repository

compile:
  stage: build
  script:
    - 'mvn $MAVEN_CLI_OPTS test-compile'

verify:
  stage: test
  before_script: 
    - mysql --user=root --password=\"$MYSQL_ROOT_PASSWORD\" --host=mysql < src/main/sql/database.sql
  script:
    - 'mvn $MAVEN_CLI_OPTS verify'
  artifacts:
    paths:
    - target/*.jar
JiaChen ZENG
  • 711
  • 8
  • 16

1 Answers1

11

You are running MySQL in a different container as a service to connect to. The maven:3.5-jdk-8 image doesn't contain the mysql-client package you invoke using mysql.

So to solve it; install the mysql-client in your before command:

before_script: 
    - apt-get update -q && apt-get install -qqy --no-install-recommends mysql-client
    - mysql --user=root --password=\"$MYSQL_ROOT_PASSWORD\" --host=mysql < src/main/sql/database.sql
Stefan van Gastel
  • 4,330
  • 23
  • 25