i just have push my Symfony 5.4 application in my company server with Gitlab-ci and docker.
I have a Dokcerfile in the root avec my projet:
FROM php:7.4-apache
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
COPY . /var/www/html/
RUN apt-get update \
&& apt-get install -y --no-install-recommends locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev;
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
# INSTALL COMPOSER
RUN curl -sSk https://getcomposer.org/installer | php -- --disable-tls && \
mv composer.phar /usr/local/bin/composer
# INSTALL EXTENSION PHP
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo pdo_mysql gd opcache intl zip calendar dom mbstring zip gd xsl
RUN pecl install apcu && docker-php-ext-enable apcu
# INSTALL YARN
RUN apt-get update && apt-get install -y gnupg2
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn
RUN apt update
RUN chown -R www-data:www-data /var/www/html
RUN a2enmod rewrite
RUN service apache2 restart
And i have this job on my gitlab pipeline:
deploy_image:
stage: deploy
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- cd /srv/docker/stacks/r-panda
- docker network disconnect --force r-panda_default traefik_app
- /usr/local/bin/docker-compose down
- /usr/local/bin/docker-compose pull
- /usr/local/bin/docker-compose up -d
- docker network connect r-panda_default traefik_app
after_script:
- docker exec r-panda_app yarn install
- docker exec r-panda_app yarn run build
- ls -al
environment:
name: dev
url: $SERVER_URL
variables:
DATABASE_URL: $DATABASE_URL
tags:
- deploy
And there is my docker-compose.yaml on the server who run the contener with the application:
version: '3.9'
services:
r-panda_app:
container_name: r-panda_app
image: panda
environment:
- DATABASE_URL="mysql://db_user:db_pass#bbd_server:3306/panda_bdd"
restart: always
labels:
- traefik.enable=true
- traefik.http.routers.router-r-panda.rule=Host(`r-panda.fr`)
- traefik.http.routers.router-r-panda.tls.certresolver=myresolver
- traefik.http.routers.router-r-panda.tls=true
With this config i have the following error: An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory
But if i change my config/packages/doctrine.yaml
This
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: '%env(resolve:DATABASE_URL)%'
To this:
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: 'the real path for the DATABASE_URL'
Everything works
I don't know what to do, can someone help me
I'm trying to import database schema to mysql service through following statment
mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql
and it return mysql: not found. I have even tried the following command
docker exec -i mysql mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql
Even though received error + docker exec -i mysql mysql --user=$DB_USERNAME --password=$DB_PASSWORD 5i < DB_Schema.sql
Error: No such container: mysql
What would be the best way to use mysql so that I can import a stance of DB into it for testing purpose and how?
Please find the .yml file below.
# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# Specify a docker image from Docker Hub as your build environment.
# All of your pipeline scripts will be executed within this docker image.
image: php:8.0-fpm-alpine
# All of your Pipelines will be defined in the `pipelines` section.
# You can have any number of Pipelines, but they must all have unique
# names. The default Pipeline is simply named `default`.
pipelines:
default:
# Each Pipeline consists of one or more steps which each execute
# sequentially in separate docker containers.
# name: optional name for this step
# script: the commands you wish to execute in this step, in order
- parallel:
- step:
name: Installing Dependancies and Composer
caches:
- composer
script:
# Your Pipeline automatically contains a copy of your code in its working
# directory; however, the docker image may not be preconfigured with all
# of the PHP/Laravel extensions your project requires. You may need to install
# them yourself, as shown below.
- apt-get update && apt-get install -qy git curl libmcrypt-dev unzip libzip-dev libpng-dev zip git gnupg gnupg2 php-mysql
- docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp && \
- docker-php-ext-install gd && \
- docker-php-ext-install exif && \
- docker-php-ext-install zip && \
- docker-php-ext-install pdo pdo_mysql
- rm -rf ./vendor
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install --ignore-platform-reqs
- composer dump-autoload
# Here we create link between the .env.pipelines file and the .env file
# so that our database can retreieve all the variables inside .env.pipelines
- ln -f -s .env.pipelines .env
artifacts:
- vendor/**
- step:
name: Installing and Running npm
image: node:16
caches:
- node
script:
- npm install -g grunt-cli
- npm install
- npm run dev
artifacts:
- node_modules/**
- step:
name: Running Test
deployment: local
script:
# Start up the php server so that we can test against it
- php artisan serve &
# # Give the server some time to start
- sleep 5
# - php artisan migrate
- docker ps
- docker container ls
- mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql
# - docker exec -i mysql mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD -e "SHOW DATABASES"
- php artisan optimize
- php artisan test
services:
- mysql
- docker
# You might want to create and access a service (like a database) as part
# of your Pipeline workflow. You can do so by defining it as a service here.
definitions:
services:
mysql:
image: mysql:latest
environment:
MYSQL_DATABASE: $DB_DATABASE
MYSQL_USER: $DB_USERNAME
MYSQL_PASSWORD: $DB_PASSWORD
MYSQL_ROOT_PASSWORD: $DB_PASSWORD
SERVICE_TAGS: mysql
SERVICE_NAME: mysql
You cannot install/update/change you main image in the first step for them to be there in the last step. Make your custom Docker image with all those installations, which will make it faster to run the pipeline and will let you use other tools you need in your pipeline.
I prefer to use the "mysql" client outside Docker and have it reach into the Docker container based on the port mapping set up. Then, conceptually, it is like reading to a "mysqld" server on a separate "server".
LOAD DATA INFILE and INSERT, including use of mysql ... < dump.sql works fine.
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
The go app simply inserts a harcoded value in a mysql table and spits it back out. This is done using this database driver. It works fine on linux servers, but during gitlab's CI this returns:
dial tcp 127.0.0.1:3306: connect: connection refused
This is the .gitlab-ci.yml
image: mysql
services:
- mysql:latest
variables:
MYSQL_DATABASE: storage
MYSQL_ROOT_PASSWORD: root
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_TRANSAPORT: tcp
MYSQL_ADDRESS: "127.0.0.1:3306"
job:
script:
- apt-get update -qq && apt-get install -qq curl && apt-get install -qq git
- echo "SHOW GLOBAL VARIABLES LIKE 'PORT';" | mysql --user="$MYSQL_USER" --password="$MYSQL_ROOT_PASSWORD" --host="$MYSQL_HOST" "$MYSQL_DATABASE"
- curl -O https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz
- tar -C /usr/local -xzf go1.10.1.linux-amd64.tar.gz
- rm go1.10.1.linux-amd64.tar.gz
- echo "export PATH=\$PATH:/usr/local/go/bin" >> ~/.bashrc
- echo "export GOPATH=\$HOME/go" >> ~/.bashrc
- echo "export PATH=\$PATH:\$GOPATH/bin" >> ~/.bashrc
- source ~/.bashrc
- go get github.com/go-sql-driver/mysql
- go build main.go
- ./main
Is there a standard way to use mysql from golang during CI?
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