Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute mysql command before test script on GitLab CI

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
like image 491
JiaChen ZENG Avatar asked Oct 22 '17 10:10

JiaChen ZENG


1 Answers

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
like image 62
Stefan van Gastel Avatar answered Nov 15 '22 08:11

Stefan van Gastel