2

I'm trying to set my own mysql conf for Gitlab CI Runner. I found in the documentation how to set my own php.ini :

before_script:
- cp ci/php.ini /usr/local/etc/php/conf.d/test.ini

I didn't find informations about how setting my.cnf, I tried :

before_script:
- cp ci/my.cnf /usr/local/etc/mysql/conf.d/my.cnf

But /usr/local/etc/mysql/ doesn't exist in the generated environment.

This is all my gitlab.ci :

services:
  - mysql:latest

variables:
  # Configure mysql environment variables
  WITH_XDEBUG: "1"
  MYSQL_ROOT_PASSWORD: password
  MYSQL_DATABASE: symfony

cache:
  paths:
  - vendor/

before_script:
# Install dependencies
- bash ci/docker_install.sh > /dev/null
- cp ci/php.ini /usr/local/etc/php/conf.d/test.ini
- cp ci/my.cnf /usr/local/etc/mysql/conf.d/my.cnf
- mv app/config/parameters.gitlab.yml app/config/parameters.yml
- mv app/config/config_test.gitlab.yml app/config/config_test.yml
- composer clear-cache
- composer install
- php bin/console doctrine:schema:update --force
- php bin/console doctrine:fixtures:load

test:
  image: php:7.0
  stage: test
  script:
  - echo "Running PHPUnit Tests"
  - vendor/bin/phpunit --configuration phpunit.xml.dist --colors --debug --coverage-text

And my docker_install.sh :

  #!/bin/bash

  # We need to install dependencies only for Docker
  [[ ! -e /.dockerenv ]] && [[ ! -e /.dockerinit ]] && exit 0

  set -xe

  # Install git (the php image doesn't have it) which is required by composer
  apt-get update -yqq
  apt-get install git -yqq
  apt-get install wget -yqq
  apt-get install zip unzip -yqq

  # Install composer
  curl -sS https://getcomposer.org/installer | php
  mv composer.phar /usr/local/bin/composer

  # Install mysql driver
  # Here you can install any other extension that you need
  docker-php-ext-install pdo_mysql mbstring

Thanks.

Aximem
  • 714
  • 8
  • 27
  • I'm not seeing where you're actually installing MySQL. – Jonathon Reinhart Dec 07 '16 at 12:06
  • @JonathonReinhart Do I really need to install mysql here ? I mean, I'm using mysql docker image and I just need to override the my.cnf, what should I do ? – Aximem Dec 07 '16 at 12:14
  • i think you dont need to install MySQL itself, but MySQL client. – Gabbax0r Dec 07 '16 at 13:23
  • I tried to install a mysql client : `apt-get install -y mysql-client libmysqlclient-dev` I still do not know how to override my.cnf and `/usr/local/etc/mysql/` still not exist. – Aximem Dec 07 '16 at 15:44
  • Is this really the new normal for dockerized CI? Apt-get every time? It seems to me that there could be 1+ million new questions on StackOverflow on how to Apt-Get All the Things. Or you could build a custom docker image that contains what you need it to contain. Is "what should I apt-get so I can connect to MySQL" really on topic for Stackoverflow? – Warren P Dec 07 '16 at 23:44

1 Answers1

0

I don't think it is possible to easily mount the config inside of the service container using the Gitlab CI config. It is probably easier to just create a Dockerfile that extends the image that you use for testing and set the the configs you need there.

You can find an example of such a small Dockerfile here, where the author copies a config file to set the encoding to utf8:

https://hub.docker.com/r/dnhsoft/mysql-utf8/dockerfile

Or you can simply append your configs for example like this if you don't want to write a Dockerfile that copies files:

FROM mysql:8.0

RUN echo '[mysqld]' >> /etc/mysql/conf.d/mysql.cnf
RUN echo 'default-authentication-plugin = mysql_native_password' >> /etc/mysql/conf.d/mysql.cnf
RUN echo 'collation-server = utf8mb4_general_ci' >> /etc/mysql/conf.d/mysql.cnf
RUN echo 'character-set-server = utf8mb4' >> /etc/mysql/conf.d/mysql.cnf

Once your Dockerfile has the configs you need you can publish it to Dockerhub under your username. Then in your gitlab-ci.yml you can use it like this:

services:
    - {name: 'mydockerusername/mysql:1', alias: 'mysql'}

In your scripts in gitlab-ci.yml you can then connect to your custom database container like this:

script:
    - 'mysql -u gitlabci -h mysql --password="gitlabci" -e "SHOW DATABASES"'
tobias47n9e
  • 2,233
  • 3
  • 28
  • 54