Build a CI Pipeline for Laravel Using Jenkins

Updated

laravel php

Put this Jenkinsfile in the repository root. It never touches a real database: the test suite runs against SQLite in memory, which the default Laravel phpunit.xml already configures, and the migrate step is deliberately absent.

pipeline {
  agent any
  environment {
    APP_ENV                = 'testing'
    DB_CONNECTION          = 'sqlite'
    DB_DATABASE            = ':memory:'
    COMPOSER_NO_INTERACTION = '1'
  }
  stages {
    stage('Checkout') { steps { checkout scm } }
    stage('Install') {
      steps {
        sh 'composer install --no-interaction --prefer-dist --no-progress'
        sh 'cp -n .env.example .env'
        sh 'php artisan key:generate'
      }
    }
    stage('Test') {
      steps { sh 'php artisan test --log-junit=build/junit.xml' }
      post { always { junit allowEmptyResults: false, testResults: 'build/junit.xml' } }
    }
  }
}

Why php artisan migrate is not in there

Laravel only guards migrate when APP_ENV=production. Both halves, executed here against a scratch SQLite file:

$ APP_ENV=production php artisan migrate < /dev/null ; echo "EXIT=$?"
                           APPLICATION IN PRODUCTION.
   WARN  Command cancelled.
EXIT=1

$ DB_DATABASE=/tmp/scratch.sqlite php artisan migrate < /dev/null ; echo "EXIT=$?"
   INFO  Preparing database.
  Creating migration table ....................................... 3.08ms DONE
   INFO  Running migrations.
  0001_01_01_000000_create_users_table ........................... 2.91ms DONE
  0001_01_01_000001_create_cache_table ........................... 1.54ms DONE
  0001_01_01_000002_create_jobs_table ............................ 2.72ms DONE
EXIT=0

The first form breaks the build (exit 1) because there is no TTY to answer the prompt — which is the good outcome. The second is the dangerous one: no prompt, exit 0, nine tables created in whatever database the environment pointed at. A build agent with production credentials in its environment will migrate production and report a green build. If you must migrate from CI, do it in a separate deploy job, against an explicit host, with --force so the intent is written down and the production guard is consciously overridden. That deploy job is also the event deployment frequency counts — one dated release rather than one green tick per build — and if you would rather not build and maintain it yourself, Forge is the managed version: you point it at a server you already pay for and it handles the configuration and the zero-downtime deploy.

The .env step the pipeline needs

.env is gitignored, so a fresh checkout has none. On Laravel 13.24.0 that produces two distinct failures:

$ php artisan test ; echo "EXIT=$?"
  FAILED  Tests\Feature\ExampleTest > the applicati…  MissingAppKeyException
  No application encryption key has been specified.
  at vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:83
EXIT=2

$ php artisan key:generate ; echo "EXIT=$?"
   ErrorException
  file_get_contents(/path/.env): Failed to open stream: No such file or directory
  at vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php:105
EXIT=1

key:generate edits .env in place; it does not create it. That is why the pipeline copies .env.example first. After cp -n .env.example .env && php artisan key:generate, the suite passes: Tests: 2 passed (2 assertions) / Duration: 0.13s, exit 0. In a fresh Laravel project the two failures without a .env, the passing test run and the production-only migrate guard all come out as shown above; laravel-jenkins-cicd-pipeline.sh re-checks each of them against whatever composer create-project laravel/laravel installs when you run it.

You do not need .env.testing

A generated Laravel 13 project’s phpunit.xml already contains the test environment, which overrides .env for the test run:

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="DB_CONNECTION" value="sqlite"/>
    <env name="DB_DATABASE" value=":memory:"/>
    <env name="CACHE_STORE" value="array"/>
    <env name="QUEUE_CONNECTION" value="sync"/>
    <env name="MAIL_MAILER" value="array"/>
    <env name="BCRYPT_ROUNDS" value="4"/>
</php>

Add a .env.testing only if you need something that block does not cover, such as a real MySQL service container. And the test runner is PHPUnit, not Pest: a fresh Laravel 13.24.0 requires phpunit/phpunit ^12.5.12 in require-dev and nothing from Pest. php artisan test wraps PHPUnit and forwards its flags, including --log-junit, which is what feeds the junit step.

Version reality check

composer create-project laravel/laravel on 2026-08-10 produced Laravel Framework 13.24.0, released 2026-08-04, whose composer.json requires "php": "^8.3". Any older guide’s “PHP 7.4 / 8.0” assumption is dead — a PHP 8.2 agent will fail at composer install, not at test time. On a warm Composer cache the install stage took real 4.88 seconds for 110 packages here; a cold agent will be much slower, so cache ~/.composer/cache on the build node.

What I could not test, and one surprise

Jenkins is not installed on this machine, so the Jenkinsfile above has not been run by a controller. What I did check: it parses as valid Groovy (Apache Groovy 4.0.28), and every plugin the junit and checkout scm steps depend on exists in the current update centre — Pipeline (workflow-aggregator) 608.v67378e9d3db_1, JUnit 1418.v67a_81935603c, Git 5.10.1, against core 2.576.

The surprise came from Laravel itself. Laravel 13’s skeleton ships laravel/pao (“Agent-optimized output for PHP testing tools”). When it detects a coding-agent environment variable — its detector looks for CLAUDECODE, CURSOR_AGENT, GEMINI_CLI, COPILOT_CLI and about fifteen others — it replaces PHPUnit’s human output with a single JSON line. None of those are set by Jenkins, so your pipeline is unaffected; but if you run php artisan test from inside an AI coding tool and wonder where your test output went, that is why. PAO_DISABLE=1 restores the normal output. All the Laravel output quoted above was captured with PAO_DISABLE=1 set, because this machine sets one of those variables.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.