Laravel with Bitbucket Pipelines

This quickstart shows you how to use Bitbucket Pipelines to build and test a Laravel project in a Docker container.

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 repo.

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, click , then Repository, then Import repository:

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

 

Enable Bitbucket Pipelines

  • In your new repo, click Pipelines, then Get started for free:

 

Clone the repo

  • Clone the new Bitbucket repo to your local machine:
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-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 environment 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
image: php:7-fpm
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 CACHE_DRIVER=redis
          - export SESSION_DRIVER=redis
          - export DB_CONNECTION=mysql
          - php artisan migrate
          - php artisan serve &
          - sleep 5
          - 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'

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


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

 

install-composer.sh
#!/bin/bash

COMPOSER_VERSION=${COMPOSER_VERSION:-1.4.2}

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

 

Make sure you set the executable permission of install-composer.sh before committing to the repo:

chmod +x install-composer.sh


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
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 MySQL:  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. These values must match the contents of config/database.php:

config/database.php
'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,
],

 

These environment variables are loaded from .env in the root of the project by Composer.

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.

Push to the repo

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.
Last modified on Nov 6, 2017

Was this helpful?

Yes
No
Provide feedback about this article

Not finding the help you need?

Ask the community

Powered by Confluence and Scroll Viewport.