'Using mysql in github workflow always leads to SQLSTATE[HY000] [1045] Access denied for user error
Im currently configuring a CI pipe for my laravel project via github actions.
This is my build.yml file
# GitHub Action for Laravel with MySQL and Redis
name: API
on: [push, pull_request]
jobs:
laravel:
name: Laravel (PHP ${{ matrix.php-versions }})
runs-on: ubuntu-latest
services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: 'secret'
MYSQL_DATABASE: 'content_information_test'
MYSQL_USER: 'homestead'
MYSQL_PASSWORD: 'secret'
ports:
- 33306:3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
redis:
image: redis
ports:
- 6379/tcp
options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3
strategy:
fail-fast: false
matrix:
php-versions: ['7.4']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, dom, fileinfo, mysql
coverage: xdebug #optional
- name: Start mysql service
run: sudo /etc/init.d/mysql start
- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Copy Env File
run: cp .env.testing .env
- name: Setup database user
run: mysql -u runner -e 'CREATE USER 'worker'@'localhost' IDENTIFIED BY 'secret';'
- name: Flush privileges
run: mysql -u worker --password=secret -e 'FLUSH PRIVILEGES;'
- name: Create testing database
run: mysql -u worker --password=secret -e 'CREATE DATABASE IF NOT EXISTS content_information_test;'
- name: Migrate Test Database
run: php artisan migrate --env=testing --seed --force
env:
DB_PORT: 33306:3306/tcp
REDIS_PORT: ${{ job.services.redis.ports['6379'] }}
- name: Change Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Static Analysis via PHPStan
run: ./vendor/bin/phpstan analyse app/BusinessDomain app/DataDomain app/Infrastructure tests/Unit -c phpstan.neon
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
env:
DB_PORT: 33306:3306/tcp
REDIS_PORT: ${{ job.services.redis.ports['6379'] }}
- name: Run code style fixer on app/
run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix app/
- name: Run code style fixer on tests/
run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix tests/
- name: Run code style fixer on database/
run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix database/
- name: Run code style fixer on routes/
run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix routes/
The problem is that the action always fails at the "Migrate Test Database" step with the following error
Run php artisan migrate --env=testing --seed --force
php artisan migrate --env=testing --seed --force
shell: /usr/bin/bash -e {0}
env:
DB_PORT: 33306:3306/tcp
REDIS_PORT: 49153
In Connection.php line 678:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (usin
g password: NO) (SQL: select * from information_schema.tables where table_s
chema = content_information_test and table_name = migrations and table_type
= 'BASE TABLE')
In Connector.php line 70:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (usin
g password: NO)
Error: Process completed with exit code 1.
Unfortunately this seems like the correct behaviour for me since I have never created a user named homestead but yet I still don't know how to create a mysql user that I can use since I'm always getting the ' SQLSTATE[HY000] [1045] Access denied for user ' error when trying to use mysql via the workflow.
Does anyone know how to make this work?
Solution 1:[1]
I also ran into this issue trying to set up a CI/CD pipeline with GitHub Actions and Laravel Dusk, using the default GitHub Action workflow in the Dusk documentation, which - as I've come to except for anything related to Dusk - is lacking.
I fixed it by adding my database environment variables from my .env.dusk.testing
file to the GitHub Actions workflow, to make sure that the GitHub Actions runner had access to them. I also optionally moved them from the "Run Dusk" step to the job-level, making them available to any other steps in the job:
name: CI
on: [push]
jobs:
dusk-php:
env:
APP_URL: "http://127.0.0.1:8000"
DB_DATABASE: CICD_test_db
DB_USERNAME: root
DB_PASSWORD: root
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Prepare The Environment
run: cp .env.example .env
- name: Create Database
run: |
sudo systemctl start mysql
mysql --user="root" --password="root" -e "CREATE DATABASE CICD_test_db character set UTF8mb4 collate utf8mb4_bin;"
- name: Install Composer Dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Generate Application Key
run: php artisan key:generate
- name: Upgrade Chrome Driver
run: php artisan dusk:chrome-driver `/opt/google/chrome/chrome --version | cut -d " " -f3 | cut -d "." -f1`
- name: Start Chrome Driver
run: ./vendor/laravel/dusk/bin/chromedriver-linux &
- name: Run Laravel Server
run: php artisan serve --no-reload &
- name: Run Dusk Tests
run: php artisan dusk --env=testing -vvv
- name: Upload Screenshots
if: failure()
uses: actions/upload-artifact@v2
with:
name: screenshots
path: tests/Browser/screenshots
- name: Upload Console Logs
if: failure()
uses: actions/upload-artifact@v2
with:
name: console
path: tests/Browser/console
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hashim Aziz |