Laravel Github Actions

Published on by

Laravel Github Actions image

This tutorial will show you how to configure Github actions to run your phpunit tests, how to deploy after your tests pass and a few other things. We'll also show you how to connect to a database (MySQL, Postgres or SQLite) to run your test suite.

The first thing we'll need is a docker container with PHP installed that is able to run our Laravel test suite. We, at KDG, put together a docker container specifically for this purpose. The docker container can be found at:

  • PHP 7.4: kirschbaumdevelopment/laravel-test-runner:7.4
  • PHP 7.3: kirschbaumdevelopment/laravel-test-runner:7.3
  • PHP 7.2: kirschbaumdevelopment/laravel-test-runner:7.2

The Github repository can be found at Laravel Test Runner Container. Please open issues or send pull requests if you find any libraries missing for your needs.

We also created this example repository which is using the setup mentioned below to run the test suite.

Alright, let's get into it!

Setting up the Github Action

You may need to tweak a few things, but basically you should be able to just copy and paste the following configuration to your Github actions.

.github/workflows/ci.yml

on: push
name: CI
jobs:
phpunit:
runs-on: ubuntu-latest
container:
image: kirschbaumdevelopment/laravel-test-runner:7.3
 
services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
 
steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
 
- name: Install composer dependencies
run: |
composer install --no-scripts
 
- name: Prepare Laravel Application
run: |
cp .env.ci .env
php artisan key:generate
 
- name: Run Testsuite
run: vendor/bin/phpunit tests/

Don’t forget to configure your env!

On this example, I created a .env.ci file with some of the configurations. Here is what is important for you to configure in this file:

# database
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=password

You may need a few tweaks for your own configuration but afterwards you should be able to see the build passing.

Using PostgreSQL or SQLite instead of MySQL

To use PostgreSQL instead of MySQL, you can easily change the services section in your CI config with the following:

services:
postgres:
image: postgres:10.8
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

And also change your .env.ci DB configuration to:

DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=test
DB_USERNAME=postgres
DB_PASSWORD=postgres

And to use SQLite, you should be able to just remove the services section entirely, and change your environment configuration to:

DB_CONNECTION=sqlite
DB_DATABASE=:memory:

Compiling assets is easy too

If you use the kirschbaumdevelopment/laravel-test-runner docker container to run your suite then it already has Node/NPM/Yarn installed. You can install dependencies/compile assets by simply adding a new step into your pipeline:

- name: Install front-end dependencies
run: |
npm install
npm run dev

And that should be enough!

Deploying your code after your test suite passes

You can easily automatically deploy your code ONLY if all of your tests are passing. I’m going to assume you already have an automated way to deploy your code here and will not go into how to do this or all the different options available.

Let’s say you want to deploy to Laravel Forge after your build passes.

- name: Deploy to Laravel Forge
run: curl ${{ secrets.FORGE_DEPLOYMENT_WEBHOOK }}

In this case, you need to register FORGE_DEPLOYMENT_WEBHOOK in the repository secrets.

Or, if you want to deploy to Vapor:

- name: Deploy to Laravel Forge
run: |
export VAPOR_API_TOKEN="${{ secrets.VAPOR_API_TOKEN }}"
vapor deploy staging

And of course, register VAPOR_API_TOKEN in your repository secrets.

Badges

Github recently implemented the ability to include badges with the last status of your actions. You probably saw some of these around open source projects in the past. If you want to include in your project, you can find the documentation here. But in short, the only thing you need is the following markdown:

[![Actions Status](https://github.com/{owner}/{repo}/workflows/{workflow_name}/badge.svg)](https://github.com/{owner}/{repo}/actions)

Owner is the owner of the repo, repo is obviously the repo name, and workflow_name is the name property in your workflow file (usually line 2).

Below you can see the rendered badge from the example repo I created:

The code for this badge looks like this:

[![Actions Status](https://github.com/luisdalmolin/laravel-ci-test/workflows/CI/badge.svg)](https://github.com/luisdalmolin/laravel-ci-test/actions)

Extra: Configuring Laravel Nova on your pipeline

If your Laravel project uses Laravel Nova, you will need to authenticate composer before installing dependencies. You can configure Nova authentication by adding the following step:

- name: Configure composer for Laravel Nova
run:|
composer config "http-basic.nova.laravel.com" "${{ secrets.NOVA_USERNAME }}" "${{ secrets.NOVA_PASSWORD }}"

Also, don’t forget to add NOVA_USERNAME and NOVA_PASSWORD to your Github Actions secrets. This configuration can be found the repository settings > Actions.

Luis Dalmolin photo

Luis is CTO at Kirschbaum with 15+ years building complex applications. A Laravel expert since v4, he also specializes in React, Vue, JavaScript, and AWS/DevOps, and actively contributes to open source.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell

The latest

View all →
Vocalizer: Local Text-to-Speech for PHP image

Vocalizer: Local Text-to-Speech for PHP

Read article
Build a Laravel Scout Search Endpoint With the HTTP QUERY Method image

Build a Laravel Scout Search Endpoint With the HTTP QUERY Method

Read article
Enforce Per-Action Waiting Periods in Laravel with Cooldown image

Enforce Per-Action Waiting Periods in Laravel with Cooldown

Read article
First-Party Image Processing in Laravel 13.20 image

First-Party Image Processing in Laravel 13.20

Read article
Laravel Legacy Bridge: Carry Authenticated Sessions from a Legacy App into Laravel image

Laravel Legacy Bridge: Carry Authenticated Sessions from a Legacy App into Laravel

Read article
Laravel Quota: Usage Budgets for Calendar Periods image

Laravel Quota: Usage Budgets for Calendar Periods

Read article