我目前正在通过GitHub Actions为我的Laravel项目配置CI管道。
这是我的build.yml文件:
# 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/
问题在于,操作总是在“迁移测试数据库”步骤处失败,并出现以下错误。
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.
不幸的是,对我来说这似乎是正确的行为,因为我从未创建过名为homestead的用户,但我仍然不知道如何创建一个可用的mysql用户,因为每当尝试通过工作流使用mysql时,总是会收到“SQLSTATE [HY000] [1045]拒绝访问用户”错误。
有人知道如何解决这个问题吗?