Skip to main content

GitLab CI

In GitLab CI, set php-ci as the job image and your scripts run with the full PHP toolchain already present. Configuration lives in .gitlab-ci.yml.

Minimal pipeline

stages:
- test

phpunit:
stage: test
image: himanshuramavat/php-ci:8.3
script:
- composer install --no-interaction --prefer-dist
- ./vendor/bin/phpunit

Cache Composer dependencies

phpunit:
stage: test
image: himanshuramavat/php-ci:8.3
variables:
COMPOSER_CACHE_DIR: "$CI_PROJECT_DIR/.composer-cache"
cache:
key:
files:
- composer.lock
paths:
- .composer-cache/
- vendor/
script:
- composer install --no-interaction --prefer-dist
- ./vendor/bin/phpunit

PHP version matrix

GitLab's parallel:matrix runs one job per PHP version, templating the image tag:

phpunit:
stage: test
image: himanshuramavat/php-ci:${PHP_VERSION}
parallel:
matrix:
- PHP_VERSION: ['8.1', '8.2', '8.3', '8.4']
script:
- composer update --no-interaction --prefer-dist
- ./vendor/bin/phpunit

With a database service

Use GitLab's services keyword. Inside the job, the service is reachable by its hostname alias:

phpunit:
stage: test
image: himanshuramavat/php-ci:8.3
services:
- name: mysql:8.0
alias: mysql
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_test
DB_HOST: mysql
DB_DATABASE: app_test
DB_USERNAME: root
DB_PASSWORD: root
script:
- composer install --no-interaction --prefer-dist
- ./vendor/bin/phpunit

Full quality pipeline

stages:
- quality
- test

default:
image: himanshuramavat/php-ci:8.3
before_script:
- composer install --no-interaction --prefer-dist

coding-standard:
stage: quality
script:
- ./vendor/bin/phpcs --standard=PSR12 src

static-analysis:
stage: quality
script:
- ./vendor/bin/phpstan analyse src --level=6

phpunit:
stage: test
script:
- ./vendor/bin/phpunit --coverage-text --colors=never
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
Coverage badge

The coverage regex above lets GitLab parse the line-coverage percentage from PHPUnit's text report and display it as a pipeline badge.

GitHub Container Registry mirror

If you prefer GHCR over Docker Hub:

image: ghcr.io/himanshuramavat/php-ci:8.3