LARAVEL

Laravel Forge CI/CD Pipeline

April 28, 2024 13 min read

Introduction

Setting up CI/CD pipelines with Laravel Forge enables automated testing and deployment of your Laravel applications. This guide covers configuring GitHub Actions, GitLab CI, and deployment hooks.

GitHub Actions

name: Deploy

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'
      
      - name: Run Tests
        run: |
          composer install
          php artisan test
      
      - name: Deploy to Forge
        uses: laravel/forge-deploy-action@v1
        with:
          forge_server: ${{ secrets.FORGE_SERVER }}
          forge_token: ${{ secrets.FORGE_TOKEN }}

GitLab CI

stages:
  - test
  - deploy

test:
  stage: test
  image: php:8.3
  script:
    - composer install
    - php artisan test

deploy:
  stage: deploy
  script:
    - curl -X POST $FORGE_DEPLOY_HOOK
  only:
    - main

Deployment Hooks

Create a deployment hook in Forge dashboard:

// Forge API to create deployment hook
$forge->deploymentHooks()->create($serverId, [
    'name' => 'Production Deploy',
    'uuid' => 'deployment-uuid',
    'url' => 'https://api.github.com/repos/owner/repo/deployments'
]);

Automated Testing

// GitHub Actions test workflow
- name: Run PHPUnit
  run: ./vendor/bin/phpunit --coverage

- name: Run Dusk
  run: ./vendor/bin/php artisan dusk

- name: Run Lint
  run: ./vendor/bin/pint --test

Complete Workflow

  • Developer pushes code to repository
  • CI pipeline triggers on push
  • Tests run automatically
  • On success, deployment hook triggers
  • Forge pulls latest code and runs deployment script

Summary

Setting up CI/CD with Laravel Forge automates your deployment workflow, ensuring code is tested before production deployment. This leads to more reliable releases and faster iteration cycles.

For more information, check out our other tutorials on Laravel Forge and Laravel Dusk.