Execute mysql command before test script on GitLab CI

天大地大妈咪最大 提交于 2020-04-13 04:07:07

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/46873159/execute-mysql-command-before-test-script-on-gitlab-ci

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!