CI/CD for PHP Projects: Setting Up GitHub Actions from Scratch

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CI/CD for PHP Projects: Setting Up GitHub Actions



Modern PHP development is unthinkable without automation of testing, building, and deployment processes. CI/CD (Continuous Integration / Continuous Deployment) allows teams to release updates faster, reduce the number of errors, and maintain high code quality. In this article, we will thoroughly examine how to set up CI/CD pipelines for PHP projects using GitHub Actions — GitHub's built-in automation tool.



We will look at real-world examples for popular frameworks (Laravel, Symfony), learn how to run tests, check code style, build artifacts, and automatically deploy to a server. The article will be useful for both beginners and experienced developers looking to optimize their workflow.



1. GitHub Actions Basics for PHP



GitHub Actions is a platform for automating workflows directly in the repository. Workflows are described in YAML files stored in the .github/workflows/ directory. Each workflow consists of one or more jobs that run on virtual machines (runners) when specific events occur (push, pull request, release, etc.).



For PHP projects, the key steps usually include:



  • Installing dependencies via Composer
  • Setting up the environment (PHP version, extensions, database)
  • Running static analysis (PHPStan, Psalm)
  • Executing unit tests (PHPUnit, Pest)
  • Checking code style (PHP CS Fixer, Laravel Pint)
  • Building and publishing a Docker image
  • Deploying to a production/staging server


A minimal workflow for a PHP project looks like this:



name: PHP CI

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

jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' extensions: mbstring, intl, pdo_mysql coverage: xdebug - name: Install dependencies run: composer install --prefer-dist --no-progress - name: Run tests run: vendor/bin/phpunit --testdox


This workflow runs on every push or pull request creation to the main branch. It checks the code against standards and runs tests.



2. Advanced Configuration: Testing, Linting, and Static Analysis



A real project requires a more thorough code quality check. Let's look at an example for the Laravel framework with a full set of tools.



Let's create the file .github/workflows/laravel-ci.yml:



name: Laravel CI/CD

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

jobs: quality: runs-on: ubuntu-latest services: mysql: image: mysql:8.0 env: MYSQL_DATABASE: testing MYSQL_USER: user MYSQL_PASSWORD: secret MYSQL_ROOT_PASSWORD: root ports: - 3306:3306 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps: - uses: actions/checkout@v4

- name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' extensions: mbstring, intl, pdo_mysql, bcmath, redis coverage: pcov tools: composer:v2

- name: Copy .env run: | cp .env.example .env php artisan key:generate

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

- name: Run Laravel Pint (Code Style) run: vendor/bin/pint --test

- name: Run PHPStan (Static Analysis) run: vendor/bin/phpstan analyse --memory-limit=2G

Blogs

Book Recommendations