Continuous Integration with Bitbucket Pipelines and Laravel5

How to configure Bitbucket Pipelines for continuous integration of a Laravel web application.

Roberto Butti
3 min readJul 11, 2017

Bitbucket Pipelines (https://bitbucket.org/product/features/pipelines) is a powerful tool provided by Atlassian for continuous integration and Laravel is a powerful framework for building Web Applications. We can use both together to test automatically our projects.

To use Bitbucket Pipelines you need an account on Bitbucket (https://bitbucket.org) and you need to create a Git repository.

It allows you to execute some commands on your code. You can execute these commands in two way: automatic or manual.

The automatic way is useful when you need to launch automated tests or code quality check every time you push your code on Git or every time you push your code on a branch.

The manual way is useful when you want to deploy on production (or stage) a specific commit.

The configuration file that drives the entire Pipelines process is named bitbucket-pipelines.yml. You need to place this file in the root of your source code.

You can find a great documentation about Pipelines here: https://confluence.atlassian.com/bitbucket/build-test-and-deploy-with-pipelines-792496469.html

In this tutorial I would like to share with you my experiments with Laravel5, automated test and Pipelines configuration.

My goal is: every time I push commits on my repository I need to execute automatically test for my Laravel web application.

The principle is simple: your commands will be executed on your source code committed on the repository, running in a Docker container.

First of all you need to select a Docker image for executing pipeline commands.

I chose phpunit docker image: https://hub.docker.com/r/phpunit/phpunit/

The content of my bitbucket-pipelines.yml for my Laravel Web Applications:

image: phpunit/phpunit:6.5.3pipelines:
default:
- step:
caches:
- composer
script:
- apk add --no-cache php7-gd php7-xmlwriter
- php -r "file_exists('.env') || copy('.env.testing', '.env');"
- composer install
- php artisan key:generate
- php artisan migrate --seed
- vendor/bin/phpunit

In the first line I set the image to use “phpunit/phpunit:6.5.3”. This docker image is built on top of Alpine Linux (https://alpinelinux.org/), so if we need to install extra packages, we can use the package manager of Alpine (apk add).

In the Yaml file we need to define pipelines -> default -> step -> script section. In this section we can add all needed commands that you want to execute after every push on your Git repository.

For Laravel (if you want to use extra modules like Laravel Cashier for Stripe or Braintree payment) you will need extra PHP modules not included in phpunit docker image: GD (to manage images) and XmlWriter (to manage XML files). To add this module you need to execute:

apk add — no-cache php7-gd php7-xmlwriter

Each line with command, it starts with the hypen.

Then, you need to set your testing configuration (database configuration, mail configuration etc) in the file .env.testing and then to copy it in to the .env file (the current configuration). Remember to commit .env.testing on your Bitbucket Git repository.

php -r “file_exists(‘.env’) || copy(‘.env.testing’, ‘.env’);”

Then, you need to install all vendors listed in composer.json file via composer command.

composer install

You need to install all your vendor, because every Pipelines execution is a fresh installation of your environment. So, for this reason you need also to generate a key for your Laravel installation (it is a typical command that you need to execute una tantum) and execute the migration:

php artisan key:generate
php artisan migrate

At the end you can execute your tests by launching phpunit:

vendor/bin/phpunit

That’s all.

Continuous Integration with Bitbucket Pipelines and Laravel

Let me know your feedback!

If you want to use Laravel 6 and GitHub Actions instead of Bitbucket pipeline, take a look to this post: Continuous Integration with GitHub Actions and Laravel 6

--

--

Roberto Butti
Roberto Butti

Written by Roberto Butti

I’m technophile. Vuejs and Laravel enthusiast! #vuejs #laravel. I love #coding

No responses yet