skip to Main Content

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


  1. 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:

    name: Laravel
    
    on:
      push:
          branches: [ "main" ]
      pull_request:
          branches: [ "main" ]
    
    jobs:
      laravel-tests:
    
        runs-on: ubuntu-latest
    
        services:
          mysql:
            image: mysql:5.7
            env:
              MYSQL_ROOT_PASSWORD: password
              MYSQL_DATABASE: laravel
            ports:
              - 3306:3306
            options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    
        steps:
        - uses: actions/checkout@v3
        
        - name: Set up PHP
          uses: shivammathur/setup-php@v2
          with:
            php-version: '8.1'
        
        - name: Copy .env
          run: php -r "file_exists('.env') || copy('.env.example', '.env');"
        
        - name: Install Dependencies
          run: composer install --prefer-dist --no-progress --no-suggest
    
        - name: Set up application for SQLite
          run: |
            touch database/database.sqlite
            echo "DB_CONNECTION=sqlite" >> .env
            echo "DB_DATABASE=${{ github.workspace }}/database/database.sqlite" >> .env
    
        - name: Generate key
          run: php artisan key:generate
        
        - name: Directory Permissions
          run: chmod -R 777 storage bootstrap/cache
    
        - name: Execute tests (Unit and Feature tests) via PHPUnit
          env:
            DB_CONNECTION: sqlite
            DB_DATABASE: database/database.sqlite
          run: vendor/bin/phpunit
    
    Login or Signup to reply.
  2. 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:

    if (! app()->runningInConsole()) {
        $allGenres = Genre::where('isActive', 1)->orderBy('name')->get();
        View::share([
            'allGenres' => $allGenres,
        ]);
    }
    

    Or if i have set the env variable then i use something like this

    if (! env('IS_CI_ENV', false)) {
        $allGenres = Genre::where('isActive', 1)->orderBy('name')->get();
        View::share([
            'allGenres' => $allGenres,
        ]);
    }
    

    For action:

    ...
    jobs:
      laravel-tests:
    
      runs-on: ubuntu-latest
      env:
        IS_CI_ENV: true
    
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search