GopenSource

Everything You Need to Know About Open Source

Follow publication

GitHub actions with Laravel

Basic usage of Github action on your Laravel application

Emmanuel Paul Mnzava.
GopenSource
Published in
4 min readApr 7, 2021

--

Are you a Laravel developer?

Do you use GitHub as your source control ( VCS )?

If yes this article is for you. In the previous article , I had begun by introducing what is GitHub's actions as a continuous integration and continuous deployment tool in general. If you did not go through it please visit it here to get a bit of a glimpse of what is GitHub's actions.

In this article, however, we will be looking at a basic way to start using GitHub actions to run basic tests that will make sure our code is okay when two events happen on our GitHub repository on push event and on pull request event.

We will be using this repository https://github.com/dbrax/laravel-admindashboard to go over how one can create simple workflows that would be triggered whenever one pushes or gets a PR ( pull request).

Step 1

Go to your Laravel repository look for the action tab as seen below.

Once you have located it click on the tab action to see various Github actions.

Step 2

Once in the actions GitHub tab, you will see the following content.

Scroll down to look for continuous integration workflows as seen below

Here you will find a collection of various workflow starts for various programming languages

We need to look for Laravel then click on the button that says set up this workflow.

Step 3

Once you have clicked on set up this workflow on the Laravel workflow you will be taken to this window.

Please observe on the top left the flowing directories will be created

.github > workflows

then a file names laravel.yml will be inside the workflows directory, This file will contain all the workflows to be taken by GitHub action.

Let's go over the default workflow created for you.

name: Laravelon:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
laravel-tests:
runs-on: ubuntu-lateststeps:
- uses: shivammathur/setup-php@b7d1d9c9a92d8d8463ce36d7f60da34d461724f8
with:
php-version: '8.0'
- 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 --prefer-dist
- 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: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit

Let's start from the beginning

  1. name can be anything you wish it mainly names the workflow
  2. on an event shows when the workflow should be triggered basically what events will trigger the workflow in this case on push and pull requests on the main branch
  3. runs-on represents operating systems runners on Github action platform on this case it will run on ubuntu latest version I suggest living this alone and not bother changing it, especially on beginner level.
  4. Steps section describes various steps that will be taken on the workflows
- uses: shivammathur/setup-php@b7d1d9c9a92d8d8463ce36d7f60da34d461724f8
with: php-version: '8.0'

5. We will be using this repository https://github.com/shivammathur/setup-php as a GitHub action. Hence the following line shivammathur/setup-php@b7d1d9c9a92d8d8463ce36d7f60da34d461724f8. The work of this line of flow is to set up PHP on Github actions.

- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"

The above step uses this repository https://github.com/actions/checkout with the main function of checking-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- 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: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit

The part above involves running various common commands

  • Installing dependencies > generating Laravel keys > changing permissions of Laravel bootstrap folder also creating a database
  • Then at the end running PHPUnit to check if all your tests have passed

Please note the above workflow is mainly for running tests hence if your Laravel applications is built considering TDD ( Test-driven development ) the above workflow will be very useful.

The next article will be looking into creating a continuous deployment kind workflow to help you have automatic deployments whenever you push your code to your Github repository.

Thanks for reading the article! If you enjoyed it, please don’t forget to show your appreciation by clicking 👏 below!

Any questions or comments hit me up on

Mail: epmnzava@gmail.com

Twitter: https://twitter.com/epmnzava

Github: https://github.com/dbrax

Do you have an idea of an application either mobile / web or desktop please visit primeware.co.tz we have the best team ready to listen to your idea.

Until next time cheers

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Written by Emmanuel Paul Mnzava.

Software Engineer and techprenuer with passion of helping entreprenuers and small businesses using Technology.

No responses yet

Write a response