Now, we are going to walk through the configuration.

The gists with the YML configuration file

Define events

You can define the GitHub events that trigger automatically the workflow. For example, if you want to trigger and execute the workflow when developers push the code on master (or features/) branch you can use the directive “on”.

name: Continuous Integration Laravel 6

on:

push:

branches:

- master

- features/*

We can define the name of the Workflow with the directive “name” and we can define the event “push” in the “on” section . In the “push” section, you can list (optionally) the branches. If you don’t specify branches section the workflow will be triggered on every push and not only on the push on the specific listed branches.

The directive “name” is also important because it will be used by GitHub in the workflow list, to identify the workflow (you could create more than one workflows).

The Workflow in the GitHub Actions UI

The jobs

With a workflow you can define one or more jobs. A job is a specific task that needs to be executed in the workflow. You can configure to run jobs in parallel or with some dependencies (for example: run the job “Test” only when the job “build assets” is completed)

A job can be executed on a specific operating system. For Laravel you could use a last version of Ubuntu system.

jobs:

laravel-tests:

runs-on: ubuntu-latest

In my Yaml file I used “laravel-tests” as job name and “ubuntu-latest” as operating system.

The steps

Each jobs has multiple steps. In each step you can “run” your commands or you can “use” some standard actions.

Following the Yaml syntax, each step starts with “minus sign”. Step that uses standard action is identified with “uses” directive, steps that use custom commands are identified by “name” and “run” directive. “name” is used in the execution log as a label in the GitHub Actions UI, “run” is used for launch the command. With “run” directive you could define command with arguments and parameters. With “run” you can also list a set of commands.

name: Continuous Integration Laravel 6

on:

push:

branches:

- master

- features/*

jobs:

laravel-tests:

runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v1

- name: Copy ENV Laravel Configuration for CI

run: php -r "file_exists('.env') || copy('.env.ci', '.env');"

- name: Install Dependencies (PHP vendors)

run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist

- name: Generate key

run: php artisan key:generate

- name: Create DB and schemas

run: |

mkdir -p database

touch database/database.sqlite

php artisan migrate

- name: Execute tests (Unit and Feature tests) via PHPUnit

run: vendor/bin/phpunit

Let me walk-through all steps in the job “laravel-tests”.

First step: git checkout

The first step is retrieve the sources. To do that, GitHub Actions has a standard action identified with “actions/checkout@v1”. To perform the action you need to set a step:

- uses: actions/checkout@v1

Second Step: copy the right .env file

In Laravel you can use .env to store your keys and parameters for the environment configuration. For example: database connection parameters, cache connection parameters, smtp configuration etc.

In order to create a database and the schema (tables) needed by your application, I suggest to create a .env.ci (for the continuous integration) file that use a sqlite database. I suggest to commit/push the .env.ci file on the repository. The workflow will copy .env.ci into .env file so your continuous integration environment will use a specific configuration.

I suggest you to set your database connection in your .env.ci file:

DB_CONNECTION=sqlite

DB_DATABASE=database/database.sqlite # DB_CONNECTION=mysql

# DB_HOST=127.0.0.1

# DB_PORT=3306

# DB_DATABASE=laravel

# DB_USERNAME=root

# DB_PASSWORD=

In order to create a right .env.ci file I suggest you to you to copy .env.example into .env.ci and the comment out the line about DB_ and add DB_CONNECTION and DB_DATABASE that uses sqlite database (as you can see in the example above).

Once you have your .env.ci file you can define in the Yaml file:

- name: Copy ENV Laravel Configuration for CI

run: php -r "file_exists('.env') || copy('.env.ci', '.env');"

Third step: install dependencies

Once you have your code checked out and you have your .env file, you need to install all dependencies in your composer.json:

- name: Install Dependencies (PHP vendors)

run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist

Fourth step: setup your Laravel key

- name: Generate key

run: php artisan key:generate

Fifth step: create your database

Every time you launch the workflow you start your environment from scratch so you need to create (every time) your database and then execute a migrate command.

- name: Create DB and schemas

run: |

mkdir -p database

touch database/database.sqlite

php artisan migrate

In a step you can define more command. To use more command in one step use pipe | character in run line.

Last step: execute the tests

Finally you can execute your test via php unit. I suggest you to use phpunit command in your vendor files:

- name: Execute tests (Unit and Feature tests) via PHPUnit

run: vendor/bin/phpunit

Is that all?

This is a small configuration that allow you to execute your firsts test in automate way.