That’s my .yaml file
name: Laravel
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
laravel-tests:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.1'
- uses: actions/checkout@v3
- 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 --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
And I’m also using GenreServiceProvide
$allGenres = Genre::where('isActive', 1)->orderBy('name')->get();
View::share([
'allGenres' => $allGenres,
]);
Run php artisan key:generate
That’s error
SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from `genres` where `isActive` = 1 and `genres`.`deleted_at` is null order by `name` asc)
How can I fix that ?
There is no problem if I comment all the codes in this file in my project, but I want it to work in github action as well
2
Answers
In your workflow file, you’ve specified DB_CONNECTION: sqlite for the testing environment, but it seems like the application is still trying to use a MySQL connection.
To fix this issue make sure that your Laravel application is configured to use the SQLite connection when running in the GitHub Actions environment. You can set this in your .env or via the GitHub Actions workflow.
That’s the modified version of your
.yaml
file:I had this error before, and if you think about it you can already guess that you don’t need
$allGenres
variable because you are in CI/CD and therefor you don’t have any view to share it with.So i always tend to disable these kind of queries on providers for CI/CD, mostly by checking if i’m in console or not. But sometimes if i need this in console commands the trick i use is i add an env variable just for CI/CD (in github action) and then in provider i check if i’m in CI/CD then i don’t query. The code will be something like this:
Or if i have set the env variable then i use something like this
For action: