Laravel with Bitbucket Pipelines
This quickstart shows you how to use Bitbucket Pipelines to build and test a Laravel project in a Docker container.
Import this repository by visiting bitbucket.org and log in. Then:
click the plus sign
under import select Repository
give your imported repo a name
On the left navigation bar, click Pipelines and scroll down to view the bitbucket-pipelines.yml file. Click Enable
Watch your build run!
Note: Bitbucket Pipelines includes fifty free minutes per account, at the time of writing. You can check your team or account's minutes usage for the month by clicking your Avatar > Bitbucket settings > Plan details.
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 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 email@example.com:<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:
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:
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:
'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.
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:
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: