How to set up github actions Laravel

Viewed 165

I am trying to set up CICD in github actions.

Currently my configuration is below

name: Laravel

on:
  push:
    branches: [ develop ]
  pull_request:
    branches: [ develop ]

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@master

      - name: Copy ENV Laravel Configuration for CI
        run: php -r "file_exists('.env') || copy('.env.ci', '.env');"

      - name: Create database
        run: |
          mkdir -p database
          touch database/database.sqlite
          sudo apt-get install php-sqlite3
        env:
          DB_CONNECTION: sqlite
          DB_DATABASE: database/database.sqlite
          DB_FOREIGN_KEYS: true
      - name: Install Dependencies
        run: |
          composer self-update
          composer install --prefer-source --no-interaction --dev
          composer require --dev php-coveralls/php-coveralls
      - name: Generate key
        run: php artisan key:generate
      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache

      - name: Run tests
        run: vendor/bin/phpunit

Everything is running except the test with a connection error

PDOException: SQLSTATE[HY000] [2002] Connection refused

What could I be missing? why is my connection failing?

Below is the .env.ci file

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:H9HHky2+uG+1SMfzg2tzOj1zembVCsmcxqbBjDTIeew=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

1 Answers

In Laravel, when running phpunit tests, the phpunit.xml file env vars take precedence over everything (despite what the PHPUnit docs say - this behavior is specific to Laravel). (Read more about environment variable precedence in Laravel here: https://docs.chipperci.com/builds/env/#env-precedence)

So, my guess is your phpunit.xml file has DB_CONNECTION set to mysql, OR your application configuration is customized so DB_CONNECTION is not being used.

Side note, for your first command in the action, you may find it simpler to run:

cmd: cp .env.ci .env

Related