docker-compose MySQL container for automated testing with GithubActions - mysql

I have GitHub repo and I want to use GithubActions to automatically execute unit Tests with every pull request.
I already set up a workflow file:
name: CI
on:
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
- name: Install
run: npm ci
- name: Linter
run: npm run lint
- name: Build
run: npm run build
- name: Docker
run: docker-compose up -d
- name: Wait / Sleep
uses: jakejarvis/wait-action#v0.1.0
with:
time: '10s'
- run: |
docker ps
cat ./dumps/backup.sql | docker exec -i mysql-development_1 /usr/bin/mysql -u root --password=password
- name: Test
run: npm test
As I need to insert the tables first, I want to insert a dump which does work on my machine with the exact same command used here.
However, the Action fails with this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
read unix #->/var/run/docker.sock: read: connection reset by peer
cat: write error: Broken pipe
##[error]Process completed with exit code 1.
How can I access the database within GithubActions?
docker-compose.yml:
version: '3'
services:
mysql-development:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test_db
ports:
- "3308:3306"

To anyone who might run into the same problem:
https://github.blog/changelog/2020-02-21-github-actions-breaking-change-ubuntu-virtual-environments-will-no-longer-start-the-mysql-service-automatically/
You just need to start the mysql-service manually and perhaps wait for a couple of seconds

Related

Github action yml file and mysql

I'm having issues using mysql as the database for my tests that are running in a Github action. I'm using this as a guide.
I'm getting the following error:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'172.18.0.2' (using password: NO) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
here is my yaml file:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-latest
container:
image: kirschbaumdevelopment/laravel-test-runner:8.1
services:
testdb:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test
MYSQL_ALLOW_EMPTY_PASSWORD: 1
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-latest
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET
UPDATE
I removed this line from my phpunit.xml file:
<env name="DB_HOST" value="testdb"/>
and now I'm getting a different error:
SQLSTATE[HY000] [2002] Connection refused (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
After doing some more googling, I found this post which made me go back and pull out the docker container and try to do it using the mysql service that's already available on ubuntu.
Once I was able to successfully start the service and create a database, I then realized that the steps in my action were copying the .env file, and if it wasn't there, it was copying the .env.example file, which I overlooked, and was getting odd results. Once I realized that it was looking in that file for database connection values, I was able to override them with the env option in the yaml file. So, I finally got it working, and this is my working yaml file for anyone that may run into this at some point:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-20.04
env:
DB_CONNECTION: mysql
DB_HOST: localhost
DB_PORT: 3306
DB_DATABASE: testdb
DB_USERNAME: root
DB_PASSWORD: root
steps:
- name: Set up MySQL
run: |
sudo systemctl start mysql
mysql -e 'CREATE DATABASE testdb;' -uroot -proot
mysql -e 'SHOW DATABASES;' -uroot -proot
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-20.04
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET

GitHub Actions migration and seeding issue with Laravel

I am trying to implement a CI/CD structure with GitHub Actions to automatically run tests on my Laravel folder. To successfully execute my tests, there has to be a functional MySQL database that has to be seeded.
So far, the seeding isn't successful. I get the following error:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations and table_type = 'BASE TABLE')
Here is my laravel.yml:
name: Laravel CI
on:
push:
branches: [ test ]
jobs:
laravel-tests:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./laravel
services:
mysql:
image: mysql:latest
ports:
- 3306
env:
MYSQL_USER: laravel_user
MYSQL_PASSWORD: laravel_pass
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testingdatabase
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: shivammathur/setup-php#15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.0'
extensions: mysql
- uses: actions/checkout#v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install dependencies
run: composer update --no-interaction --no-progress --prefer-dist --optimize-autoloader --ignore-platform-reqs
- name: Generate key
run: php artisan key:generate
- name: Configuration caching for speed
run: php artisan config:cache
- name: Route caching for speed
run: php artisan route:cache
- name: Installing npm dependencies
run: npm install
- name: Running npm prod
run: npm run prod
- name: Migrate & seed database
run: php artisan migrate --seed
- name: Setting up file storage
run: php artisan storage:link
- name: Serve application
run: php artisan serve &
- name: Run Feature tests
run: vender/bin/phpunit
How can I successfully run my migrations and seeding?
The issue was that I only declared environment variables at the mysql service, whereas I also should have declared them when attempting to migrate and seed my database:
on:
push:
paths:
- './laravel'
name: Laravel CI
jobs:
phpunit:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./laravel
services:
mysql:
image: mysql:5.7
env:
MYSQL_USER: user
MYSQL_PASSWORD: secret
MYSQL_DATABASE: testdatabase
MYSQL_ROOT_PASSWORD: root
DB_PORT: ${{ job.services.mysql.ports[3306] }}
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout#v1
with:
fetch-depth: 1
- name: Verify MySQL connection
run: |
mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uuser -psecret -e "SHOW DATABASES"
- name: Install dependencies
run: |
php --version
composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
chmod -R 777 storage bootstrap/cache
- name: Boot Laravel application
run: |
php -r "file_exists('.env') || copy('.env.example', '.env');"
php artisan key:generate
php artisan --version
- name: Execute PHPUnit tests
env:
DB_CONNECTION: mysql
DB_DATABASE: testdatabase
DB_PORT: 33306
DB_USER: root
DB_PASSWORD: secret
run: |
php artisan migrate:fresh --seed
./vendor/bin/phpunit

Jobs not connecting to MySQL service for self hosted GitHub Actions on ECS

I have a self hosted GitHub Actions running in ECS and it has been working wonders, until I have to deal with service discovery.
The below is the workflow file
name: Checks before merging for core-service server
on:
push:
paths:
- 'tons/of/paths/**'
jobs:
build:
runs-on: [self-hosted, linux, x64, nomad]
#set a timeout just in case the tests hang
timeout-minutes: 5
services:
mysql:
image: mysql:5.7
env:
MYSQL_DATABASE: test-db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: rootpassword
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout#v2
- name: Use Node.js 14.16
uses: actions/setup-node#v1
with:
node-version: 14.16
- name: Install dependencies
run: npm ci
working-directory: some-dependency/location
- name: Check if build is passing
run: npm run build
working-directory: more-dependency/more-places
- name: Run tests
run: npm run test
env:
DATABASE_URL: mysql://user:password#127.0.0.1:${{ job.services.mysql.ports[3306] }}/test-db
working-directory: core-service/server
The below is the error I'm getting
Using existing mysql database from: DATABASE_URL
Error: connect ECONNREFUSED 127.0.0.1:32777
Any comments will be appreciated.
Have a nice day~

How can I see what went wrong inside a Github CI run

I am new to Github CI and trying to setup Mysql with a specific username and password to test my Django project as per the below .github/workflow file but when systemctl starts mysql an error occurs:
Run sudo systemctl start mysql.service
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.
##[error]Process completed with exit code 1.
is visible in the github actions log. The question is: how can I know what went wrong? Is there a way of viewing the output of journalctl -xe for example... Other ideas welcome.
This is my workflow file:
name: Django CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.6, 3.7, 3.8]
steps:
- uses: actions/checkout#v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python#v2
with:
python-version: ${{ matrix.python-version }}
- name: Set up MySQL 5.7
uses: mirromutth/mysql-action#master
with:
mysql version: 5.7
mysql database: apis
mysql root password: mypassword
mysql user: myuser
mysql password: myotherpassword
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
sudo systemctl start mysql.service
python manage.py test

run mysql:5.5 docker image from ansible

How to run mysql: 5.5 from ansible? If I run it directly:
docker run -e MYSQL_ROOT_PASSWORD=pass mysql:5.5
it's work ok. But if I run from ansible:
- name: run database
local_action:
module: docker
image: mysql:5.5
state: running
it's start and immediately stop. Also post in case 1 is 3306/tcp, but in case 2 there is no port.
You need to specify mysql root password as environment variable for container. For example:
- hosts: ansible_host
gather_facts: False
sudo: yes
pre_tasks:
- name: install pip pkg.
yum:
name: python-pip
state: present
- name: install boto pkg.
pip:
name: docker-py
state: present
- name: docker
docker:
image: "mysql:5.5"
state: running
env: "MYSQL_ROOT_PASSWORD=my-secret-pw"