Laravel Github Actions | Laravel News


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

1on: push

2name: CI

3jobs:

4 phpunit:

5 runs-on: ubuntu-latest

6 container:

7 image: kirschbaumdevelopment/laravel-test-runner:7.3

8 

9 services:

10 mysql:

11 image: mysql:5.7

12 env:

13 MYSQL_ROOT_PASSWORD: password

14 MYSQL_DATABASE: test

15 ports:

16 - 33306:3306

17 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

18 

19 steps:

20 - uses: actions/[email protected]

21 with:

22 fetch-depth: 1

23 

24 - name: Install composer dependencies

25 run: |

26 composer install --no-scripts

27 

28 - name: Prepare Laravel Application

29 run: |

30 cp .env.ci .env

31 php artisan key:generate

32 

33 - name: Run Testsuite

34 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:

1# database

2DB_CONNECTION=mysql

3DB_HOST=mysql

4DB_PORT=3306

5DB_DATABASE=test

6DB_USERNAME=root

7DB_PASSWORD=password

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

Github Actions Build

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:

1services:

2 postgres:

3 image: postgres:10.8

4 env:

5 POSTGRES_USER: postgres

6 POSTGRES_PASSWORD: postgres

7 POSTGRES_DB: test

8 ports:

9 - 5432:5432

10 options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

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

1DB_CONNECTION=pgsql

2DB_HOST=postgres

3DB_PORT=5432

4DB_DATABASE=test

5DB_USERNAME=postgres

6DB_PASSWORD=postgres

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

1DB_CONNECTION=sqlite

2DB_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:

1- name: Install front-end dependencies

2 run: |

3 npm install

4 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.

1- name: Deploy to Laravel Forge

2 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:

1- name: Deploy to Laravel Forge

2 run: |

3 export VAPOR_API_TOKEN="${{ secrets.VAPOR_API_TOKEN }}"

4 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:

1[![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:

Actions Status

The code for this badge looks like this:

1[![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:

1- name: Configure composer for Laravel Nova

2 run:|

3 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.



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here