Paulund

Test Laravel With Github Actions

In this tutorial we're going to setup github actions to run tests on a merge of a Laravel Project

What Are GitHub Actions?

GitHub actions allow you to automate and configure development workflows on your repository based on multiple types of events that happen on your repository.

You can use this to create a automated CI/CD workflow for your project.

Run Laravel Tests

We're going to create a new GitHub action that will install the Laravel project, install dependencies and run phpunit tests on the project.

To create a new workflow you will need to add new file inside .github/workflows/laravel.yml and add the below yaml to this file and push to your repository.

First we need to give the workflow a new by using name: Laravel.

Next we define what events we want to listen on to trigger this workflow, for this workflow we want to run tests when pull requests are merged into master or when they're pushes into master.

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

When this is triggered it will run through the list of jobs in the workflow. For a Laravel project we need to setup the project and then run the phpunit tests.

Full Workflow

name: Laravel

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  laravel-tests:

    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v2
    - 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
    - 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: Compile assets
      run: |
        npm install
        npm run production
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: sqlite
        DB_DATABASE: database/database.sqlite
		CACHE_DRIVER: array
        SESSION_DRIVER: array
        QUEUE_DRIVER: sync
      run: vendor/bin/phpunit

The next time you push into the repository GitHub will build your project and run the phpunit tests on your application.