[SOLVED] GitHub Actions migration and seeding issue with Laravel

Issue

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/[email protected]
      with:
        php-version: '8.0'
        extensions: mysql   
    - uses: actions/[email protected]
    - 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?

Solution

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/[email protected]
      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 

Answered By – Unseptium

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published.