Git lab preparing CI scripts to deploy automatically to server in yii2? - yii2

Here is my readme file to installation.I want to merge it on server using Ci in gitlab.
Installation
Clone project from Gitlab
git clone --single-branch --branch branchname https://gitlab.com/project/project.git
Update needed libraries
cd project
composer update
Go to project root directory cloned above and initialize all the configuration files which will be added to the application with a specific environment. Enter Yes to all the overwrite files ([Yes|No|All|Quit])
php init --env=Development
Create a new database, copy the file named common/config/main-local.example to common/config/main-local.php and adjust the components['db'] configuration in common/config/main-local.php accordingly.
cp common/config/main-local.example common/config/main-local.php
nano common/config/main-local.php
Change mail configuration inside common/config/main-local.php
Get Recaptcha keys (v2) from https://www.google.com/recaptcha/admin
Copy the param file named common/config/params.example to common/config/params.php and adjust the configuration in common/config/params.php accordingly.
cp common/config/params.example common/config/params.php
nano common/config/params.php
Apply migrations which will create tables needed for the application to work. Enter Yes to all prompts to dev environment but take extra care on the production environment (so it run the migrations and add the database tables to your db)
php yii migrate
Create assets folder under backend/web , allow group users to change the content and make the directory owned by apache user
mkdir -p backend/web/assets
chmod 775 backend/web/assets
sudo chown www-data:www-data backend/web/assets
What should be in the .gitlab-ci.yml file and ci/docker_install.sh
,ci/shell-scripts-dev.sh,ci/shell-scripts-prod.sh. Please also explain
how it work?

Here is you .gitlab-ci.yml file
#
# File is "indented" using multiple of 4 spaces
#
# Specify the docker image to use (only used if using docker runners)
# See: http://doc.gitlab.com/ee/ci/docker/using_docker_images.html
# From https://hub.docker.com/r/kaffineaddict/gitlabcakephp3/ - we could use image: kaffineaddict/gitlabcakephp3
image: php:7.2
# The docker services to configure
# See: http://doc.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-service
services:
#- mysql:5.7
# Define custom build variables
# For the default gitlab variables see: http://doc.gitlab.com/ce/ci/variables/README.html
# These can be used below, or they will also be ENV variables available within' any scripts
# you execute from the CI
variables:
#MYSQL_DATABASE: site_zoova
#MYSQL_ALLOW_EMPTY_PASSWORD: "1"
#MYSQL_ROOT_PASSWORD: ThisIsAStrongPassword#^2
# Define commands that run before each job's script
before_script:
- umask 022 # set permissions to default directory permissions of 755 and default file permissions are 644,
# Install ssh-agent if not already installed, it is required by Docker.
# (change apt-get to yum if you use a CentOS-based image)
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
#- docker-php-ext-install pdo pdo_mysql mysqli
# Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
# For Docker builds disable host key checking. Be aware that by adding that
# you are suspectible to man-in-the-middle attacks.
# WARNING: Use this only with the Docker executor, if you use it with shell
# you will overwrite your user's SSH config.
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
# Prepare the build environment. A way to overcome this is to create a script which installs all prerequisites prior the actual testing is done.
- bash ci/docker_install.sh > /dev/null
#- bash ci/docker_install.sh
# Prepare the build environment. A way to overcome this is to create a script which installs all prerequisites prior the actual testing is done.
- cd $CI_PROJECT_DIR && curl --silent --show-error https://getcomposer.org/installer | php
# Install composer dependencies
#- composer install --no-plugins --no-scripts
#- cd $CI_PROJECT_DIR && php composer.phar config cache-files-dir /cache/composer
- cd $CI_PROJECT_DIR && php composer.phar install --no-plugins --no-scripts --optimize-autoloader
- cd $CI_PROJECT_DIR
# Folder and file manipulation
- 'which rsync || ( apt-get install rsync -y )'
- '[ -d $CI_PROJECT_DIR/backend/web/assets ] || mkdir -p $CI_PROJECT_DIR/backend/web/assets'
## please change this line to the cache directories of yii mvc
# - rm -rf $CI_PROJECT_DIR/tmp/*
- rm -rf $CI_PROJECT_DIR/backend/web/assets/*
- rm -rf $CI_PROJECT_DIR/backend/runtime/debug/*
- rm -rf $CI_PROJECT_DIR/backend/runtime/logs/*
- rm -rf $CI_PROJECT_DIR/backend/runtime/mail/*
- rm -rf $CI_PROJECT_DIR/backend/runtime/URI/*
# Rest runtime files
- rm -rf $CI_PROJECT_DIR/rest/runtime/cache/*
- rm -rf $CI_PROJECT_DIR/backend/runtime/debug/*
- rm -rf $CI_PROJECT_DIR/backend/runtime/logs/*
- rm -rf $CI_PROJECT_DIR/backend/runtime/mail/*
- find $CI_PROJECT_DIR -type d -exec chmod 0755 {} \;
- find $CI_PROJECT_DIR -type f -exec chmod 0644 {} \;
- chmod -R 777 $CI_PROJECT_DIR/backend/web/assets
- chmod +x $CI_PROJECT_DIR/ci/*
- chown www-data:www-data $CI_PROJECT_DIR/ -R
# Make sure these dirs/files are not writable
# setup application
- chmod go-w $CI_PROJECT_DIR
#run code sniffer
- php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php backend --colors
- php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php common --colors
- php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php rest --colors
#- |
# following will done once on the server
# cp common/config/main-local.example common/config/main-local.php
# cp common/config/params.example common/config/params.php
# cp rest/web/index.example rest/web/index.php
# genetraing files and added to z_rsync_exclude_list
#php ./init --env=Development --overwrite=All
# replacing credentials in config file
#sed -i "s/{APP_DB_HOST}/${APP_DB_HOST}/g" ${CI_PROJECT_DIR}/common/config/main-local.php
#sed -i "s/{APP_DB_NAME}/${APP_DB_NAME}/g" ${CI_PROJECT_DIR}/common/config/main-local.php
#sed -i "s/{APP_DB_USERNAME}/${APP_DB_USERNAME}/g" ${CI_PROJECT_DIR}/common/config/main-local.php
#sed -i "s/{APP_DB_PASSWORD}/${APP_DB_PASSWORD}/g" ${CI_PROJECT_DIR}/common/config/main-local.php
# updating composer for dependencies
#php composer.phar update
# running yii migrations
#yes | php yii migrate
# resolve errors through phpcs
#php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php backend --colors
#php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php common --colors
#php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php rest --colors
# making required directories & assigning permissions
#mkdir -p backend/web/assets
#chmod 775 backend/web/assets
#chown www-data:www-data backend/web/assets
#- |
# Define commands that run before after all builds
after_script:
#- find . -type d -exec chmod 0755 {} \; # Set directory permissions #moved in each stage
#- find . -type f -exec chmod 0644 {} \; # Set file permissions #moved in each stage
# Define list of files that should be cached between subsequent runs -
# Composer stores all downloaded packages in the vendor/ directory.
# temporary commented out - builds failed
cache:
paths:
- vendor/
# stages is used to define build stages that can be used by jobs
# The specification of stages allows for having flexible multi stage pipelines
# The next stage only executes if all elements of the previous stage succeed
# Typically used for compiled languages testing and/or to automate deployments
stages:
- development
- production
#
# Run test on all branches but master
#
development:
stage: development
only:
- dev
script:
- echo Running dev
# DO NOT COPY THIS KEY TO PUBLIC PLACES
- ssh-add <(echo "$SSH_PRIVATE_KEY_DEV")
#- echo Running tests...
# Ex: - phpunit --configuration phpunit_myapp.xml
#- vendor/bin/phpunit # TODO: uncomment me - in docker file we have it as /usr/local/bin/phpunit
# Sync the files to the server
- echo Using rsync to push changes to dev server...
- rsync -ap --stats -e "ssh -p $SSH_PORT_DEV" --exclude-from 'ci/z_rsync_exclude_list.txt' $CI_PROJECT_DIR/ $SSH_USER_DEV#$SSH_IP_DEV:$PROJECT_PATH_DEV
- echo Running shell scripts on remote server
- ssh -t -p $SSH_PORT_DEV $SSH_USER_DEV#$SSH_IP_DEV 'cd '"'$PROJECT_PATH_DEV'"';ci/shell-scripts-dev.sh'
# Done
- echo Done pushing changes to dev...
#Environment is used to define that a job deploys to a specific environment. This allows easy tracking of all deployments to your environments straight from GitLab.
#If environment is specified and no environment under that name exists, a new one will be created automatically.
environment: development
#Make sure we don't push to live if build has failed
allow_failure: false #default behaviour
#
# Send to live server if branch is master
#
production:
stage: production
only:
- master
script:
- echo Running prod
# DO NOT COPY THIS KEY TO PUBLIC PLACES
- ssh-add <(echo "$SSH_PRIVATE_KEY_PROD")
#- echo Running tests...
# Ex: - phpunit --configuration phpunit_myapp.xml
#- vendor/bin/phpunit #TODO: uncomment me - in docker file we have it as /usr/local/bin/phpunit
# Push to live server now
- echo Using rsync to push changes to live server...
- rsync -ap --stats -e "ssh -p $SSH_PORT_PROD" --exclude-from 'ci/z_rsync_exclude_list.txt' $CI_PROJECT_DIR/ $SSH_USER_PROD#$SSH_IP_PROD:$PROJECT_PATH_PROD
- echo Running shell scripts on remote server
- ssh -t -p $SSH_PORT_PROD $SSH_USER_PROD#$SSH_IP_PROD 'cd '"'$PROJECT_PATH_PROD'"';ci/shell-scripts-prod.sh'
# Done
- echo Done pushing changes to live...
#Environment is used to define that a job deploys to a specific environment. This allows easy tracking of all deployments to your environments straight from GitLab.
#If environment is specified and no environment under that name exists, a new one will be created automatically.
environment: production
##make sure development built before moving to production
dependencies:
- development
Here you ci/docker_install.sh file
#!/bin/bash
# I had to specify DEBIAN_FRONTEND=noninteractive as we get after install apt-utils: debconf: unable to initialize frontend: Dialog
# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && [[ ! -e /.dockerinit ]] && exit 0
set -xe
# add the add-apt-repository command
DEBIAN_FRONTEND=noninteractive apt-get install software-properties-common -y
# Install git (the php image doesn't have it) which is required by composer
apt-get update -yqq
#debconf: delaying package configuration, since apt-utils is not installed
#apt-get install git -yqq
DEBIAN_FRONTEND=noninteractive apt-get install apt-utils git zip unzip -y
# Here you can install any other extension that you need
#docker-php-ext-install pdo_mysql intl mcrypt soap
docker-php-ext-install bcmath
Here your ci/shell-scripts-dev.sh file
#!/bin/bash
#do not enter current dir
#cd $(dirname $0)
BASEDIR=$(dirname "$0")
printf "\n"
printf "###############################################################################\n"
printf "# Running dev script from directory #\n"
printf "###############################################################################\n"
pwd
printf "\n"
printf "###############################################################################\n"
printf "# Create initial/ needed files #\n"
printf "###############################################################################\n"
# backend init files
if [ ! -f backend/config/main-local.php ]; then
printf "File backend/config/main-local.php does not exist, creating it, please edit configuration on server!\n"
cp backend/config/main-local.example backend/config/main-local.php
fi
if [ ! -f backend/config/params.php ]; then
printf "File backend/config/params.php does not exist, creating it, please edit configuration on server!\n"
cp backend/config/params.example backend/config/params.php
fi
if [ ! -f backend/web/index.php ]; then
printf "File backend/web/index.php does not exist, creating it, please edit configuration on server!\n"
cp backend/web/index.example backend/web/index.php
fi
if [ ! -f backend/web/index-test.php ]; then
printf "File backend/web/index-test.php does not exist, creating it, please edit configuration on server!\n"
cp backend/web/index.example backend/web/index-test.php
fi
# common init files
if [ ! -f common/config/main.php ]; then
printf "File common/config/main.php does not exist, creating it, please edit configuration on server!\n"
cp common/config/main.example common/config/main.php
fi
if [ ! -f common/config/main-local.php ]; then
printf "File common/config/main-local.php does not exist, creating it, please edit configuration on server!\n"
cp common/config/main-local.example common/config/main-local.php
fi
if [ ! -f common/config/params.php ]; then
printf "File common/config/params.php does not exist, creating it, please edit configuration on server!\n"
cp common/config/params.example common/config/params.php
fi
# rest init files
if [ ! -f rest/config/params.php ]; then
printf "File rest/config/params.php does not exist, creating it, please edit configuration on server!\n"
cp rest/config/params.example rest/config/params.php
fi
if [ ! -f rest/web/index.php ]; then
printf "File rest/web/index.php does not exist, creating it, please edit configuration on server!\n"
cp rest/web/index.example rest/web/index.php
fi
if [ ! -f rest/web/index-test.php ]; then
printf "File rest/web/index-test.php does not exist, creating it, please edit configuration on server!\n"
cp rest/web/index.example rest/web/index-test.php
fi
printf "\n"
printf "###############################################################################\n"
printf "# Clearing cache #\n"
printf "###############################################################################\n"
# backend runtime files
rm -rf backend/web/assets/*
rm -rf backend/runtime/debug/*
rm -rf backend/runtime/logs/*
rm -rf backend/runtime/mail/*
rm -rf backend/runtime/URI/*
rm -rf cache/*
# rest runtime files
rm -rf rest/runtime/cache/*
rm -rf backend/runtime/debug/*
rm -rf backend/runtime/logs/*
rm -rf backend/runtime/mail/*
###############################################################################
# Rebuild assets# - don't we need this???
###############################################################################
#chmod +x ../app/Console/cake
#cd ../app && Vendor/bin/cake asset_compress build --force
printf "\n"
printf "###############################################################################\n"
printf "# Composer update #\n"
printf "###############################################################################\n"
php composer.phar install
printf "\n"
printf "###############################################################################\n"
printf "# Running yii migrations #\n"
printf "###############################################################################\n"
yes | php yii migrate
printf "\n"
printf "###############################################################################\n"
printf "#Run PhpSniffer and output any errors #\n"
printf "###############################################################################\n"
php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php backend --colors
php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php common --colors
p
hp ./vendor/bin/phpcs --encoding=utf-8 --extensions=php rest --colors
Note: You can configure these configration file on server yourself or
replace with example files.Working like cham

1 / You need to access the server using ssh, then do the following:
a / gitlab-runner register
sudo gitlab-runner register
b / edit the .gitlab-ci.yml file :
stages:
- sit
deploy_linxsit:
stage: sit
variables:
APP_SIT_FOLDER: "/home/project_name"
- echo "Deploying code to SIT server"
- echo "rsync code from $CI_PROJECT_DIR to $APP_SIT_FOLDER"
# rsync only updated and new files, delete file if it's longer in repo need recursive flags
# also exluced file/dir included in excluded_list.txt
- rsync -ur --delete --exclude-from "$CI_PROJECT_DIR/excluded_list.txt" "$CI_PROJECT_DIR/" "$APP_SIT_FOLDER"
tags:
- "fill tag when register gitlab-runner"
environment:
name: "SIT"
url: http://domain/project_name
only:
# only deploy changes on branch sit, for example when merging branch branch to branch sit
- sit

Related

AWS EB .ebextensions skipping - Contains invalid key

I have been trying to deploy my php code but i get this error
Error processing file (Skipping): '.ebextensions/setup.confi`g' - Contains invalid key:
'chmod -R 777 /var/dubcut/uploads/uploads'. For information about valid keys, see
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions.html`
and this is my ebextension file
ebextensions config file
packages:
#ebextensions config file
packages:
yum:
incron: []
sox: []
container_commands:
#Create an upload directory and make it world-writable:
mkdir -p /var/dubcut/uploads
chmod -R 777 /var/dubcut/uploads/uploads
mkdir -p /var/dubcut/transcoded
#Copy the incrond script to /etc/incron.d and make it executable:
cp transcoder/dubcut.incrond /etc/incron.d
chmod -R 755 /etc/incron.d/dubcut.incrond
#Copy the transcoding script to /var/dubcut and make it executable:
cp transcoder/do_transcode.bash /var/dubcut
chmod -R 755 /var/dubcut/do_transcode.bash
#Copy the ffmpeg binary to /usr/local/bin and make it executable
cp ffmpeg /usr/local/bin
chmod -R 755 /usr/local/bin/ffmpeg
tried multiple commands: but still same error
Your container commands are not in the correct format. See the AWS docs for the syntax. You need to set them up like this:
container_commands:
00_create_upload_dir_1:
command: mkdir -p /var/dubcut/uploads
01_create_upload_dir_2:
command: chmod -R 777 /var/dubcut/uploads/uploads
02_create_upload_dir_3:
command: mkdir -p /var/dubcut/transcoded
03_copy_incrond_script_1:
command: cp transcoder/dubcut.incrond /etc/incron.d
04_copy_incrond_script_2:
command: chmod -R 755 /etc/incron.d/dubcut.incrond
etc. Note that the container commands run in alphabetical order by name, so you need to make sure that you appropriately name the commands.

Convert a simple .md file to a gitlab page

There are several options for setting up Gitlab pages. Most of them have a lot of awesome tools. But it is not really necessary for us. That's why we wondered if it was possible to convert a .MD (readme.md) file to a single Gitlab page. So we can write in .MD markup.
I have written a script which converts the md file to html and then uploads it on a host.
image: node:4.2.2
variables:
HOST: "myhost"
USERNAME: "myusername"
PASSWORD: "mypassword"
pages:
stage: deploy
script:
- npm install -g markdown-styles -y
- generate-md --input ./ --output ./output --layout mixu-bootstrap-2col
- cd output
- pwd
- ls
- apt-get update -qq && apt-get install -y -qq lftp
- lftp -c "set ftp:ssl-allow no; open -u $USERNAME,$PASSWORD $HOST; mirror -Rnev ./ ./www -e --delete-first --parallel=10 --exclude-glob .git* --exclude .sh --exclude .gitlab-ci.yml --exclude /node_modules/*"
only:
- master

centos7: Operation not permitted - mysql

I have installed mysql in centOS and now, want to start the mysql-server. However, I get that error:
# systemctl start mysqld
Failed to get D-Bus connection: Operation not permitted
To fix it, I created a Dockerfile as shown
FROM centos:7
MAINTAINER theodosiostziomakas <mymail#gmail.com>
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i
== systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
And then run it to create an image.
$ docker build --rm -t local/c7-systemd .
But I am still getting the same error.
I also looked at this proposed solution
Any ideas?
Thanks,
Theo.
I believe the issue with the Dockerfile or with the run command
It seems the issue in you Dockerfile is in this line
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
Here is MySQL centos Dockerfile
# Starting from base CentOS image
FROM centos:7
# Enabling SystemD
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
# Enabling EPEL & Remi repo
#RUN yum install -y epel-release && \
#yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# Mysql repo & installion
RUN yum install -y https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm && \
yum install -y mysql mysql-server
RUN chkconfig --level 345 mysqld on
RUN systemctl enable mysqld
VOLUME [ "/var/lib/mysql" ]
# Port Expose
EXPOSE 3306
CMD ["/usr/sbin/init"]
Now, Next step is to run
--privileged is not enough, you also need to mount cgroup
Here is the command
docker run --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro -it adilm7177/centos-mysql
You can build your own or you can pull the above image from docker registry that i build and push during testing.
docker push adilm7177/centos-mysql:latest
Update:
RUN systemctl enable mysqld
After adding this I am able to start-stop using systemctl
I am able to run mysql just fine with the docker-systemctl-replacement script which emulates "systemctl" commands without an active systemd daemon. You can look at that at the docker-systemctl-images examples.

Gitlab CI Runner | Custom mysql conf

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.
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"'

how can i create a database on mysql docker service

I'm trying to run moodle phpunit on my gitlab ci server. Using gitlab-ci.yml file i'm creating a container with php 5.6 and mysql service.
# Services
services:
- mysql:latest
before_script:
- mysql -e 'CREATE DATABASE gitlab_ci_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_bin;' ;
I'm getting ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) and not sure how to proceed.
Tomasz this is my gitlab-ci.yml file you will need something like this:
# Select image from https://hub.docker.com/r/_/php/
image: php:7.0.0
services:
- mysql:5.7
variables:
# Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
MYSQL_DATABASE: symfony
MYSQL_ROOT_PASSWORD: qwerty
# Composer stores all downloaded packages in the vendor/ directory.
# Do not use the following if the vendor/ directory is commited to
# your git repository.
cache:
paths:
- vendor/
before_script:
# Install dependencies
- bash ci/docker_install.sh > /dev/null
- cp ci/parameters.yml app/config/parameters.yml
- composer install
test:app:
script:
- phpunit
And this is my docker_install.sh inside ci folder
#!/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 phpunit, the tool that we will use for testing
curl -o /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar
chmod +x /usr/local/bin/phpunit
# Install mysql driver
# Here you can install any other extension that you need
docker-php-ext-install pdo pdo_mysql mbstring