DEV Community

Md.Shariful Islam
Md.Shariful Islam

Posted on

Step by Step Guide Laravel CI/CD with GitHub Actions

What is GitHub Actions?

GitHub Actions is a free automation tool that is built into GitHub. It lets you run scripts and commands automatically when certain events happen in your repository, like pushing code or opening a pull request.

Workflow Overview
Our workflow will:

Run on every push or pull request to the main or dev branches.
Set up a MySQL database for testing.
Install PHP and all required extensions.
Cache Composer dependencies for faster builds.
Install your Laravel app’s dependencies.
Run static analysis and code quality checks.
Run your automated tests.
Let’s break down each step!

Workflow Triggers

on:
  push:
    branches: ["main", "dev"]
  pull_request:
    branches: ["main", "dev"]
This tells GitHub to run the workflow every time you push code or open a pull request to the main or dev branches.

Enter fullscreen mode Exit fullscreen mode

Define the Job and Environment

jobs:
  tests:
    runs-on: ubuntu-latest
We define a job called tests that runs on the latest Ubuntu Linux environment provided by GitHub.

Enter fullscreen mode Exit fullscreen mode

Set Up MySQL Database

services:
  mysql:
    image: mysql:8.0
    ports:
      - 3306:3306
    env:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
    options: --health-cmd="mysqladmin ping"
We spin up a MySQL 8.0 database in Docker, set the root password, and create a database called test_db. The health check ensures MySQL is ready before tests run.

Enter fullscreen mode Exit fullscreen mode

Checkout Your Code

- uses: actions/checkout@v4
This step pulls your repository’s code into the workflow environment so the next steps can work with it.

Enter fullscreen mode Exit fullscreen mode

Setup PHP with All Laravel Extensions

- name: Setup PHP with PECL extension
  uses: shivammathur/setup-php@v2
  with:
    php-version: '8.4'
    extensions: mbstring, bcmath, ctype, fileinfo, json, tokenizer, xml, pdo, pdo_mysql, openssl, curl, zip, imagick, swoole

We install PHP 8.4 and all the extensions Laravel and its ecosystem commonly need. This ensures your app runs just like it would in production.

Enter fullscreen mode Exit fullscreen mode

Cache Composer Dependencies

- name: Cache Composer dependencies
  uses: actions/cache@v4
  with:
    path: vendor
    key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
    restore-keys: |
      ${{ runner.os }}-composer-
This is “smart caching.” If your dependencies haven’t changed, Composer can skip downloading and installing them, making your workflow much faster.

Enter fullscreen mode Exit fullscreen mode

Copy the CI Environment File

- name: Copy .env file
  run: |
    cp .env.ci .env
We copy a special .env.ci file to .env. This file should contain settings for your test database and other CI-specific configs.

Enter fullscreen mode Exit fullscreen mode

Install Composer Dependencies

- name: Install dependencies
  run: |
    composer install -q --no-ansi --no-interaction --no-scripts --no-progress
We install all PHP dependencies your Laravel app needs, using flags to make the process faster and quieter.

Enter fullscreen mode Exit fullscreen mode

Generate Laravel Application Key

- name: Generate application key
  run: |
    php artisan key:generate
Laravel needs an application key for encryption and sessions. This command generates it.

Enter fullscreen mode Exit fullscreen mode

Set Directory Permissions

- name: Directory permissions
  run: |
    sudo chown -R $USER:$USER storage bootstrap/cache
    chmod -R 775 storage bootstrap/cache
Laravel needs write access to storage and bootstrap/cache for logs and cache files. This step ensures the permissions are correct.

Enter fullscreen mode Exit fullscreen mode

Run Static Analysis with PHPStan

- name: phpstan
  run: |
    ./vendor/bin/phpstan analyse --level=5 --memory-limit=1G
PHPStan checks your code for bugs and potential issues without running it. This helps you catch problems early.

Enter fullscreen mode Exit fullscreen mode

Run Code Quality Checks with PHP Insights

- name: phpinsights
  run: |
    php artisan insights --no-interaction \
    --min-quality=90 --min-complexity=90 \
    --min-architecture=90 --min-style=90 \
    --ansi --format=github-action
PHP Insights analyzes your code for quality, complexity, architecture, and style. The workflow enforces minimum scores for each metric.

Enter fullscreen mode Exit fullscreen mode

Show Database Configuration

- name: Show DB config
  run: |
    grep DB_ .env
This step prints out the database settings being used, so you can verify your tests are running against the correct database.

Enter fullscreen mode Exit fullscreen mode

Run Your Tests!

- name: Run tests
  run: |
    php artisan test
Finally, we run all your Laravel tests. These tests will use the MySQL test database you set up earlier.

Enter fullscreen mode Exit fullscreen mode

Conclusion
With this workflow, every push or pull request is automatically tested in a fresh environment, with a real database, and all the tools you need for code quality. This helps you catch bugs early, maintain high standards, and move faster as a team.

Tip:

Make sure your .env.ci file is committed and contains the correct database settings for CI!

Top comments (0)