Laravel CI Testing with GitLab

Published 1 year ago by Refringe

I'd love to set Laravel up on GitLab with CI, however I can not find any tutorials on how to configure the .gitlab-ci.yml file for a basic Laravel 5.2 installation. Does anyone have any experience with this? Currently I'm just on gitlab.com using their shared runners.

Best Answer (As Selected By Refringe)
Refringe

Alright, I wanted to work with the official PHP docker builds, so here's what I ended up doing. It tests using PHP v5.5, v5.6, and v7 with MySQL v5.6 and v5.7; six tests in total. A test takes about 6-7 minutes on average to compile before running. Thanks for the help!

.gitlab-ci.yml:

before_script:
  - bash .gitlab-ci.sh

variables:
  MYSQL_DATABASE: project_name
  MYSQL_ROOT_PASSWORD: secret

phpunit:php5.5:mysql5.6:
  image: php:5.5
  services:
    - mysql:5.6
  script:
    - php vendor/bin/phpunit --colors

phpunit:php5.6:mysql5.6:
  image: php:5.6
  services:
    - mysql:5.6
  script:
    - php vendor/bin/phpunit --colors

phpunit:php7.0:mysql5.6:
  image: php:7.0
  services:
    - mysql:5.6
  script:
    - php vendor/bin/phpunit --colors

phpunit:php5.5:mysql5.7:
  image: php:5.5
  services:
    - mysql:5.7
  script:
    - php vendor/bin/phpunit --colors

phpunit:php5.6:mysql5.7:
  image: php:5.6
  services:
    - mysql:5.7
  script:
    - php vendor/bin/phpunit --colors

phpunit:php7.0:mysql5.7:
  image: php:7.0
  services:
    - mysql:5.7
  script:
    - php vendor/bin/phpunit --colors

.gitlab-ci.sh:

#!/bin/bash

# Install dependencies only for Docker.
[[ ! -e /.dockerinit ]] && exit 0
set -xe

# Update packages and install composer and PHP dependencies.
apt-get update -yqq
apt-get install git libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq

# Compile PHP, include these extensions.
docker-php-ext-install mbstring mcrypt pdo_mysql curl json intl gd xml zip bz2 opcache

# Install Composer and project dependencies.
curl -sS https://getcomposer.org/installer | php
php composer.phar install

# Copy over testing configuration.
cp .env.testing .env

# Generate an application key. Re-cache.
php artisan key:generate
php artisan config:cache

# Run database migrations.
php artisan migrate

.env.testing:

APP_ENV=testing
APP_DEBUG=true
APP_KEY=key

DB_HOST=mysql
DB_DATABASE=project_name
DB_USERNAME=root
DB_PASSWORD=secret

CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log
ohffs
ohffs
1 year ago (234,960 XP)

Oddly enough - this is on my todo list (although that's basically infinite...). I think maybe @bashy has poked at this more? :: flutters eyelashes :: ;-)

noeldiaz

I forget where I found this sample script that someone placed as a Gist, but I have used a variant on this on my steps to start using the CI component of Gitlab (just got manual install migrated to Omnibus last week). So far just doing phpunit tests and it is working wonderfully. But here is the full script I got online:

before_script:
    - composer install --prefer-dist > /dev/null
    - export APP_ENV=testing

unitTesting:
    script:
        - echo "Running PHP Unit tet"
        - php vendor/bin/phpunit --colors --debug --coverage-text
codeSniffer:
    script:
        - php vendor/bin/phpcs --config-set ignore_warnings_on_exit 1
        - php vendor/bin/phpcs --standard=PSR2 --ignore=app/views,app/storage,app/tests,app/filters.php,app/routes.php,packages/,app/Providers/,app/Console/,app/services/,http/Middleware/,app/Exceptions/,app/Events/ -w --colors app/
    allow_failure: true

phpMess:
    script:
        - php vendor/bin/phpmd app/ text cleancode,controversial,codesize,design,naming,unusedcode --exclude=app/views,app/storage,app/tests,app/filters.php,app/routes.php,packages/,app/Providers/,app/Console/,app/services/,http/Middleware/,app/Exceptions/,app/Events/
    allow_failure: true
Refringe

Hey @noeldiaz, thanks for the reply. Are you sure that's all that's needed for basic unit testing? It's failing for me on the composer install line.

$ composer install --prefer-dist > /dev/null
bash: line 23: composer: command not found

ERROR: Build failed with: exit code 1

Also, will a simple set-up like that get functional testing to work? eg:

$this->visit('/login')->see('Login');
noeldiaz

Ah, don't forget, if you want your gitlab server to do the CI testing you need to install all the tools for it. I made that mistake at first too.

I installed php, phpunit, composer, phpcs, and phpmd globally.

Now, at minimum all you need is composer because the other tools you can add as dev dependencies on your project composer.json. In fact, laravel already pull down phpunit so you will have that one in:

vendor/bin/phpunit

So just do the global composer install (so it is in somewhere like /usr/local/bin/) and the CI runner should find it and be able to execute for you.

bashy
bashy
1 year ago (1,064,770 XP)

This is the script I use for a lot of projects.

before_script:
  - git submodule update --init
  - php -v
  - composer --version
  - git --version
  - ls -lah
  - composer self-update
  - composer config --global github-oauth.github.com $GITHUB_TOKEN
  - composer install --no-interaction
  - php artisan env
  - touch database/testing.sqlite
  - php artisan migrate --database=testing --env=testing

stages:
  - test

test:
  script:
  - vendor/bin/phpunit
  - vendor/bin/phpmd app/ text phpmd.xml
  - vendor/bin/phpcs --standard=psr2 app

I also use this locally to test the build before commiting.

#!/bin/sh

# Run some general commands so we're up-to-date
composer self-update
composer install --no-interaction --optimize-autoloader

# Setup db
touch database/testing.sqlite
echo "" > database/testing.sqlite
php artisan migrate --database=testing --env=testing

# Start tests
vendor/bin/phpmd app/ text phpmd.xml
vendor/bin/phpcbf --standard=psr2 app/
vendor/bin/phpcs --standard=psr2 --colors app/
vendor/bin/phpunit

Got a docker build for Laravel (php7) if you want to use it for the runner: https://hub.docker.com/r/bashy/docker-build/

Source: https://github.com/WithSocial/docker-build

Refringe

Alright, I wanted to work with the official PHP docker builds, so here's what I ended up doing. It tests using PHP v5.5, v5.6, and v7 with MySQL v5.6 and v5.7; six tests in total. A test takes about 6-7 minutes on average to compile before running. Thanks for the help!

.gitlab-ci.yml:

before_script:
  - bash .gitlab-ci.sh

variables:
  MYSQL_DATABASE: project_name
  MYSQL_ROOT_PASSWORD: secret

phpunit:php5.5:mysql5.6:
  image: php:5.5
  services:
    - mysql:5.6
  script:
    - php vendor/bin/phpunit --colors

phpunit:php5.6:mysql5.6:
  image: php:5.6
  services:
    - mysql:5.6
  script:
    - php vendor/bin/phpunit --colors

phpunit:php7.0:mysql5.6:
  image: php:7.0
  services:
    - mysql:5.6
  script:
    - php vendor/bin/phpunit --colors

phpunit:php5.5:mysql5.7:
  image: php:5.5
  services:
    - mysql:5.7
  script:
    - php vendor/bin/phpunit --colors

phpunit:php5.6:mysql5.7:
  image: php:5.6
  services:
    - mysql:5.7
  script:
    - php vendor/bin/phpunit --colors

phpunit:php7.0:mysql5.7:
  image: php:7.0
  services:
    - mysql:5.7
  script:
    - php vendor/bin/phpunit --colors

.gitlab-ci.sh:

#!/bin/bash

# Install dependencies only for Docker.
[[ ! -e /.dockerinit ]] && exit 0
set -xe

# Update packages and install composer and PHP dependencies.
apt-get update -yqq
apt-get install git libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq

# Compile PHP, include these extensions.
docker-php-ext-install mbstring mcrypt pdo_mysql curl json intl gd xml zip bz2 opcache

# Install Composer and project dependencies.
curl -sS https://getcomposer.org/installer | php
php composer.phar install

# Copy over testing configuration.
cp .env.testing .env

# Generate an application key. Re-cache.
php artisan key:generate
php artisan config:cache

# Run database migrations.
php artisan migrate

.env.testing:

APP_ENV=testing
APP_DEBUG=true
APP_KEY=key

DB_HOST=mysql
DB_DATABASE=project_name
DB_USERNAME=root
DB_PASSWORD=secret

CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log
bashy
bashy
1 year ago (1,064,770 XP)

Great stuff :)

ohffs
ohffs
1 year ago (234,960 XP)

I've been looking into this package (when I say 'looking into' I of course mean 'finding it in my list of browser tabs once in a while and going "oh yeah, I was going to look into that"' ;-)

https://github.com/dockunit/dockunit

bashy
bashy
1 year ago (1,064,770 XP)

@ohffs Interesting. Thanks!

mehran
mehran
1 year ago (10,275 XP)

@bashy @Refringe @noeldiaz Thank You! The scripts and discuses were really helpful!! Marked this discussion as Favorite.

Fuxy22
Fuxy22
1 year ago (36,150 XP)

@Refringe what is the below code for you never posted that file and it prevents the shell script from running

.gitlab-ci.sh:

# Install dependencies only for Docker.
[[ ! -e /.dockerinit ]] && exit 0
set -xe
bashy
bashy
1 year ago (1,064,770 XP)
Refringe

@Fuxy22 It checks to see if Docker exists. If it doesn't then the script halts. @bashy pointed to an issue that explains that this method stops working with newer versions of Docker. This should work with the new version:

[[ ! -e /.dockerinit ]] && [[ ! -e  /.dockerenv ]] && exit 0

However, I have yet to test it.

lorvent

currently i am using something like envoyer for live deployments on each commit, can we do same with gitlab ci?

Mythos33

Hey there, I'm using the config of @Refringe. But I'll get this error:

Checking out ca730a3c as master...
$ bash .gitlab-ci.sh
$ php vendor/bin/phpunit --colors
Could not open input file: vendor/bin/phpunit
ERROR: Build failed: exit code 1

Does anyone know why composer doesn't install the dependencies?

Regards, Stefan

Sign In or create a forum account to participate in this discussion.