Laravel CI/CD Tutorial (Step-by-Step) – Using GitHub Actions
This tutorial will show you how to set up a complete CI/CD pipeline for your Laravel 11 application using GitHub Actions.
We’ll cover:
-
CI (Testing + Code Quality)
-
Build Process
-
Automatic Deployment to VPS
-
Production Optimization
Step 1: Prepare Your Laravel Project
Make sure your project runs locally:
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate
php artisan test
Push your project to GitHub.
Step 2: Create GitHub Actions Workflow
Inside your Laravel project:
mkdir -p .github/workflows
touch .github/workflows/laravel.yml
Open laravel.yml and paste:
name: Laravel CI/CD
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
laravel-tests:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel_test
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
- name: Install Dependencies
run: composer install --no-progress --prefer-dist
- name: Copy .env
run: cp .env.example .env
- name: Generate Key
run: php artisan key:generate
- name: Run Migrations
run: php artisan migrate
- name: Run Tests
run: php artisan test
Commit and push.
Now every push will automatically:
-
Install dependencies
-
Setup database
-
Run migrations
-
Run tests
Step 3: Add Code Quality Check (Optional but Recommended)
Install Laravel Pint:
composer require laravel/pint --dev
Add this step to your workflow:
- name: Run Pint
run: ./vendor/bin/pint --test
This ensures clean code formatting.
Step 4: Add Frontend Build (If Using Vite / Vue / React)
Add this to workflow:
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: 18
- name: Install NPM Dependencies
run: npm ci
- name: Build Assets
run: npm run build
Step 5: Setup Automatic Deployment (VPS via SSH)
On your server:
cd /var/www/your-project
git clone https://github.com/username/project.git .
Then add SSH key to GitHub:
GitHub → Settings → Secrets → Actions → New Secret
Add:
HOST
USERNAME
SSH_PRIVATE_KEY
Now add deploy job:
deploy:
needs: laravel-tests
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to VPS
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/your-project
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan queue:restart
Step 6: Production Optimization Commands
After deployment always run:
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize
Final CI/CD Flow
Push Code →
Run Tests →
Run Pint →
Build Assets →
Deploy to Server →
Run Migrations →
Cache Optimization →
Restart Queue
Professional Setup Includes
✔ Automated Testing
✔ Code Style Check
✔ Asset Build
✔ Auto Deployment
✔ Zero Downtime Strategy
✔ Database Backups
✔ Queue Monitoring