Laravel with Bitbucket Pipelines

This quick-start guide shows you how to use Bitbucket Pipelines to build and test in a Laravel 5 project in a Docker container.

If you'd prefer to quickly import our demo repository with a working pipeline to experiment with, have a look at our demo laravel repo.

If you'd like to set it up by hand, most of the configuration happens in the bitbucket-pipelines.yml file that Pipelines uses to define the build.

How it works

Bitbucket Pipelines keeps your build config in a YAML file, named bitbucket-pipelines.yml.

The bitbucket-pipelines.yml file lives in your repository.

When someone pushes to the repo, Pipelines runs the build in a Docker image.

The bitbucket-pipelines.yml file is where you can specify the dependencies needed by your project.

Import the quickstart repository into Bitbucket

Import the https://github.com/laravel/quickstart-basic.git repo into Bitbucket:

  • In Bitbucket, select the Repositories tab, then Create repository, then Import repository.

We're using laravel-quickstart-basic as the repo name here.

Enable Bitbucket Pipelines

  • In your new repoository, click Pipelines, then Create your first pipeline.

Clone the repo

  • Clone the new Bitbucket repo to your local machine:

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

Add the bitbucket-pipelines.yml config file to the repo

We're going to configure a default pipeline. This pipeline will:

  • use the 7.2-fpm Docker image

  • install dependencies (git, curl) from OS packages

  • install PHP extensions for mcrypt and mysql

  • install Composer

  • use Composer to install PHP dependencies

  • set variables to control which cache, session data store, and database we use

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

  • Sleep for 5 seconds to allow the app time to start

  • use curl to ensure everything is hooked up and working.

  • run PHPunit

Here's the config file to do all that:

bitbucket-pipelines.yml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 image: php:7.2-fpm pipelines: default: - step: script: - apt-get update && apt-get install -qy git curl libmcrypt-dev mysql-client - yes | pecl install mcrypt-1.0.1 - docker-php-ext-install pdo_mysql - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer install - ln -f -s .env.pipelines .env - php artisan migrate - php artisan serve & - sleep 5 - ./vendor/bin/phpunit - curl -vk http://localhost:8000 services: - mysql definitions: services: mysql: image: mysql:5.7 environment: MYSQL_DATABASE: 'homestead' MYSQL_RANDOM_ROOT_PASSWORD: 'yes' MYSQL_USER: 'homestead' MYSQL_PASSWORD: 'secret'

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

Configure external dependencies

Laravel uses a RDBMS (MySQL by default) to store application state.

We can use Bitbucket Pipelines service containers to run containers from community managed images.

The MySQL definition is a little bit harder:

bitbucket-pipelines.yml snippet

1 2 3 4 5 6 7 8 9 definitions: services: mysql: image: mysql:5.7 environment: MYSQL_DATABASE: 'homestead' MYSQL_RANDOM_ROOT_PASSWORD: 'yes' MYSQL_USER: 'homestead' MYSQL_PASSWORD: 'secret'

We define variables for MySQL:  MYSQL_DATABASE, MYSQL_RANDOM_ROOT_PASSWORD, MYSQL_USER and MYSQL_PASSWORD.
These instruct the MySQL initialization process to create a database named 'homestead' with a user 'homestead' with password 'secret', and to assign a random password to the root user. These values must match the contents of config/database.php:

config/database.php

1 2 3 4 5 6 7 8 9 10 11 12 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ],

Then create an .env.pipelines file in the root of the reposity, as shown below. The bitbucket-pipelines.yml file copys this into the default .env location in the root of the project.

.env.pipelines

1 2 3 4 5 6 7 APP_ENV=local APP_KEY=ThisIsThe32CharacterKeySecureKey DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret

Make sure you add the .env.pipelines file before committing to the repo:

1 git add .env.pipelines

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

Push to the repository

When you commit and push to the repo in Bitbucket, Pipelines with automatically run the build. Now you've got the launchpad for your next Laravel project to build upon.

Still need help?

The Atlassian Community is here for you.