ENGINEERING JOURNAL

GitHub Actions CI Pipeline for a PHP Project: A Practical Setup

Set up a GitHub Actions CI pipeline for a PHP project: workflow file, tests with a real database, secrets, and caching.

THE ARTICLE 01 — 02

Every PHP project I work on eventually reaches the same point: someone pushes a change, it works on their machine, and it breaks in production. The fix is not more careful developers. It is a CI pipeline that runs the same checks on every push, so broken code never reaches the server. GitHub Actions makes this cheap to set up, and a basic pipeline takes less than an hour to write.

What the pipeline should check

A CI pipeline has one job: answer the question "is this change safe to merge?" as fast as possible. For a typical PHP project, that means four checks. First, syntax: every PHP file must parse. Second, dependencies: composer install must succeed from a clean state. Third, tests: the test suite must pass. Fourth, style: the code must follow the project's coding standard, so reviews are about logic, not formatting. Keep the list short at the start. A pipeline that runs ten slow checks will be ignored or worked around. A pipeline that runs four fast checks on every push gets trusted.

The workflow file

The whole pipeline lives in one file, .github/workflows/ci.yml. Here is a starting point that works for a Laravel or plain PHP project running PHP 8.2:

name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          extensions: mbstring, pdo, pdo_pgsql
          coverage: none

      - name: Validate composer files
        run: composer validate --strict

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

      - name: Check syntax
        run: find . -name '*.php' -not -path './vendor/*' -exec php -l {} \;

      - name: Run tests
        run: vendor/bin/phpunit --testsuite=Unit

Two details matter here. Pin the PHP version explicitly, because the default on the runner changes over time. And commit composer.lock to the repo, or composer install will resolve fresh versions on every run and your builds will stop being reproducible.

Running tests with a real database

Unit tests that never touch a database are easy. The moment your tests need PostgreSQL or MySQL, the workflow needs a service container:

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

Your test configuration then points at 127.0.0.1:5432 with those credentials. The health check is the part people skip, and skipping it is how you get flaky builds that fail because the tests started before Postgres finished booting. Use it every time.

One more rule: never point CI at a real database. Test databases in CI should be disposable and recreated on every run. If a test can only pass against production data, the test is the problem, not the pipeline.

Keep secrets out of the workflow file

Anything a workflow needs that is not safe to print in a log goes in GitHub Secrets, never in the YAML. Database passwords for the test container above are test-only values, so they are fine inline. API keys, deployment credentials, and tokens are not. Reference them as ${{ secrets.NAME }} and set them under the repository's Settings, then Secrets and variables, then Actions.

Also be careful with pull requests from forks. By default, a workflow triggered by a forked pull request does not get access to your repository secrets. That is a safety feature, not a bug.

Cache dependencies so the pipeline stays fast

The slowest step in most PHP pipelines is composer install. Cache the vendor directory between runs and the pipeline drops from minutes to seconds on most pushes:

      - name: Cache Composer dependencies
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}

The cache key includes a hash of composer.lock, so any dependency change invalidates it automatically. A cache is only useful when you can trust it to refresh when the underlying data changes. I wrote about the same principle on the application side in preventing Redis cache stampedes with locks in Laravel.

What to do when the pipeline goes red

A red pipeline is only useful if someone looks at it. Three habits make the difference. First, make the main branch protected so a failing check blocks the merge, otherwise the pipeline is decoration. Second, keep the run time short, ideally under five minutes, because a slow pipeline trains people to merge without waiting for it. Third, treat a flaky test as a bug in the test, not as noise. A test that fails one time in ten will be ignored the other nine times, and then it will hide a real failure. When a build fails, read the log from the first failing step, not the last. Later steps fail because earlier steps did, and the root cause is almost always at the top of the failure chain.

Takeaway

A GitHub Actions CI pipeline for a PHP project does not need to be elaborate. Check out the code, set up a pinned PHP version, validate and install dependencies, lint the files, run the tests, and cache the vendor directory. Add a database service container when your tests need one, keep secrets in GitHub Secrets, and protect the main branch so a red build blocks a merge. That is enough to catch the broken push before it becomes a production incident, which is the entire point.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗