DevOps

laravel-cicd

Configures CI/CD pipelines for Laravel applications

Overview

The laravel-cicd agent sets up continuous integration and deployment pipelines for Laravel applications. It supports GitHub Actions, GitLab CI, and Bitbucket Pipelines with Laravel-optimized workflows including testing, code quality checks, and automated deployments.

Responsibilities

  • GitHub Actions - Create Laravel-optimized workflows
  • GitLab CI - Configure .gitlab-ci.yml pipelines
  • Testing Pipeline - PHPUnit/Pest with coverage reports
  • Code Quality - PHPStan, Pint, security checks
  • Auto-Deploy - Deploy on merge to main/production
  • Environment Setup - Database, cache, queue services

Supported Platforms

Platform Config File Features
GitHub Actions .github/workflows/ Matrix testing, caching, environments
GitLab CI .gitlab-ci.yml Stages, artifacts, environments
Bitbucket bitbucket-pipelines.yml Parallel steps, deployments

Generated GitHub Actions Workflow

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  tests:
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: testing
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

      redis:
        image: redis:7
        ports:
          - 6379:6379

    strategy:
      matrix:
        php: [8.2, 8.3]

    steps:
      - uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: $
          extensions: mbstring, pdo_mysql, redis
          coverage: xdebug

      - name: Cache Composer
        uses: actions/cache@v4
        with:
          path: vendor
          key: $-composer-$

      - name: Install Dependencies
        run: composer install --no-interaction --prefer-dist

      - name: Copy Environment
        run: cp .env.ci .env

      - name: Generate Key
        run: php artisan key:generate

      - name: Run Migrations
        run: php artisan migrate --force

      - name: Run Tests
        run: php artisan test --coverage-clover=coverage.xml

      - name: Upload Coverage
        uses: codecov/codecov-action@v4
        with:
          files: coverage.xml

  code-quality:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.3

      - name: Install Dependencies
        run: composer install --no-interaction

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

      - name: Run PHPStan
        run: ./vendor/bin/phpstan analyse

      - name: Security Check
        run: composer audit

  deploy:
    needs: [tests, code-quality]
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production

    steps:
      - uses: actions/checkout@v4

      - name: Deploy to Production
        uses: appleboy/ssh-action@v1
        with:
          host: $
          username: $
          key: $
          script: |
            cd /var/www/app
            php artisan down
            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
            php artisan up

GitLab CI Configuration

# .gitlab-ci.yml
stages:
  - test
  - quality
  - deploy

variables:
  MYSQL_ROOT_PASSWORD: password
  MYSQL_DATABASE: testing
  DB_HOST: mysql
  DB_DATABASE: testing
  DB_USERNAME: root
  DB_PASSWORD: password

.php-base:
  image: php:8.3-cli
  before_script:
    - apt-get update && apt-get install -y git unzip
    - curl -sS https://getcomposer.org/installer | php
    - php composer.phar install

test:
  extends: .php-base
  stage: test
  services:
    - mysql:8.0
    - redis:7
  script:
    - cp .env.ci .env
    - php artisan key:generate
    - php artisan migrate --force
    - php artisan test --coverage-text
  coverage: '/Lines:\s*(\d+\.\d+)%/'
  artifacts:
    reports:
      junit: report.xml

phpstan:
  extends: .php-base
  stage: quality
  script:
    - ./vendor/bin/phpstan analyse --error-format=gitlab > report.json
  artifacts:
    reports:
      codequality: report.json

pint:
  extends: .php-base
  stage: quality
  script:
    - ./vendor/bin/pint --test

deploy-production:
  stage: deploy
  only:
    - main
  environment:
    name: production
    url: https://app.example.com
  script:
    - 'which ssh-agent || apt-get install openssh-client'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | ssh-add -
    - ssh $DEPLOY_USER@$DEPLOY_HOST "cd /var/www/app && ./deploy.sh"

Environment Configuration

# .env.ci - CI environment settings
APP_ENV=testing
APP_DEBUG=true
APP_KEY=

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=password

CACHE_DRIVER=redis
QUEUE_CONNECTION=sync
SESSION_DRIVER=array

REDIS_HOST=127.0.0.1
REDIS_PORT=6379

CI Pipeline Flow

┌────────────────┐     ┌────────────────┐     ┌────────────────┐
│   Push/PR      │────▶│     Tests      │────▶│  Code Quality  │
│   Triggered    │     │   (parallel)   │     │   (parallel)   │
└────────────────┘     └────────────────┘     └────────────────┘
                              │                       │
                              ▼                       ▼
                       ┌────────────────────────────────────┐
                       │         All Checks Pass?           │
                       └────────────────────────────────────┘
                                       │
                    ┌──────────────────┴──────────────────┐
                    │                                      │
                    ▼                                      ▼
             ┌────────────┐                        ┌────────────┐
             │  main only │                        │  PR/branch │
             │   Deploy   │                        │   Done     │
             └────────────┘                        └────────────┘
            

Invoked By Commands

Guardrails

The CI/CD agent follows strict rules:

  • ALWAYS run tests before deployment
  • ALWAYS use secrets for credentials, never hardcode
  • ALWAYS cache dependencies for faster builds
  • NEVER skip PHPStan or security checks
  • NEVER deploy without successful test run

See Also