I recently reached out to r/PHP to help make Bitbucket Pipelines even better for PHP devs. PHP has been the one of the largest communities using Bitbucket Pipelines since launch, and the overwhelming response was for Laravel with Mysql support.

I haven’t written PHP since PHPSESSIONID was bleeding edge technology, but I decided to dive into the Laravel quickstart and get it working for Pipelines.

Import the quickstart repository into Bitbucket

quickstart url: https://github.com/laravel/quickstart-basic

Enable Bitbucket Pipelines

Clone the repository

git clone git@bitbucket.org:<username>/laravel-quickstart-basic.git

Add a bitbucket-pipelines.yml to the repository

bitbucket-pipelines.yml



pipelines:

default:

- step:

script:

- apt-get update && apt-get install -qy git curl libmcrypt-dev mysql-client

- docker-php-ext-install mcrypt && docker-php-ext-install pdo_mysql

- ./install-composer.sh

- composer install

- export DB_CONNECTION=mysql

- php artisan migrate

- ./vendor/bin/phpunit

- php artisan serve &

- sleep 5 # time needed to start the app

- curl -vk

services:

- mysql image: php:7-fpmpipelines:default:- step:script:- apt-get update && apt-get install -qy git curl libmcrypt-dev mysql-client- docker-php-ext-install mcrypt && docker-php-ext-install pdo_mysql- ./install-composer.sh- composer install- export DB_CONNECTION=mysql- php artisan migrate- ./vendor/bin/phpunit- php artisan serve &- sleep 5 # time needed to start the app- curl -vk http://localhost:8000 services:- mysql definitions:

services:

mysql:

image: mysql

environment:

MYSQL_DATABASE: 'homestead'

MYSQL_RANDOM_ROOT_PASSWORD: 'yes'

MYSQL_USER: 'homestead'

MYSQL_PASSWORD: 'secret'

We’re going to create a default pipeline. This pipeline will:

install dependencies (git, curl) from OS packages

install PHP extensions for mcrypt and MySQL

install Composer

use Composer to install PHP dependencies

set environment variables to control which database we use

use Artisan to perform database migrations and start the app in the background

run PHPunit tests

sleep for 5 seconds to allow the app time to start

use curl to ensure everything is hooked up and working.

You can check your bitbucket-pipelines.yml file with our online validator.

Add the install-composer.sh helper script to the repository

install-composer.sh

#!/bin/bash COMPOSER_VERSION=${COMPOSER_VERSION:-1.4.2} # content originally from https://github.com/composer/docker/blob/0303cc2362774266be5845e9f2487312fda8b7f5/1.4/Dockerfile#L13 curl -s -f -L -o /tmp/installer.php https://raw.githubusercontent.com/composer/getcomposer.org/da290238de6d63faace0343efbdd5aa9354332c5/web/installer \

&& php -r " \

\$signature = '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410'; \

\$hash = hash('SHA384', file_get_contents('/tmp/installer.php')); \

if (!hash_equals(\$signature, \$hash)) {

\unlink('/tmp/installer.php'); \echo 'Integrity check failed, installer is either corrupt or worse.' . PHP_EOL; \

exit(1); \

}" \

&& php /tmp/installer.php --no-ansi --install-dir=/usr/bin --filename=composer --version=${COMPOSER_VERSION} \

&& rm /tmp/installer.php \

&& composer --ansi --version --no-interaction

Making install-composer executable

Ensure you set the executable permission of install-composer.sh with:

chmod +x install-composer.sh

before committing to the repository.

➜ laravel-quickstart-basic git:(master) > vi bitbucket-pipelines.yml ➜ laravel-quickstart-basic git:(master) > vi install-composer.sh ➜ laravel-quickstart-basic git:(master) > chmod +x install-composer.sh ➜ laravel-quickstart-basic git:(master) > git add -A ➜ laravel-quickstart-basic git:(master) > git commit -m "Adding bitbucket-pipelines" [master d622558] Adding bitbucket-pipelines 2 files changed, 43 insertions(+) create mode 100644 bitbucket-pipelines.yml create mode 100755 install-composer.sh ➜ laravel-quickstart-basic git:(master) > git push origin master

Configure external dependencies

Laravel uses a RDBMS (MySQL by default) to store application state. We can use Bitbucket Pipelines service containers to run a MySQL container as a database service for our test instance.

MySQL service

The MySQL definition is configured like this in the bitbucket-pipelines.yml:

bitbucket-pipelines.yml snippet

definitions:

services:

mysql:

image: mysql

environment:

MYSQL_DATABASE: 'homestead'

MYSQL_RANDOM_ROOT_PASSWORD: 'yes'

MYSQL_USER: 'homestead'

MYSQL_PASSWORD: 'secret'

We define 4 environment variable for the mysql service: MYSQL_DATABASE, MYSQL_RANDOM_ROOT_PASSWORD, MYSQL_USER and MYSQL_PASSWORD.

These instruct the mysql initialisation process to create a database named ‘homestead’, user ‘homestead’ with password ‘secret’, and to assign a random password to the root user.

Database connection

The MySQL driver for Laravel projects is configured via the .env file.

DB_HOST=127.0.0.1

DB_DATABASE=homestead

DB_USERNAME=homestead

DB_PASSWORD=secret

Credentials committed to source control should only be for testing, and should not be shared with production systems.

Now you’ve got the launchpad for your next Laravel project to build upon, with the confidence that each change is tested by Bitbucket Pipelines.