Bitbucket Pipelines and Laravel

James Fairhurst
3 min readOct 18, 2016

--

I recently got notified of Pipelines in Bitbucket which is a nice feature, I’ve used similar tools such as Travis CI & Codeship previously which are great but having CI built into your private Bitbucket repos is super handy. Turns out it’s relatively straight forward to get up & running. I wanted to quickly get a brand new 5.3 Laravel app connected so this is what I did.

Don’t want to dilly-dally around here? Go straight to the Bitbucket repo here. (15/05/17 — updated to 5.4)

Getting Setup

Head over to the Pipelines nav section and enable Pipelines, choose PHP and click Next. Bitbucket will then open the file for editing so that you can add custom Laravel commands. First I wanted the bare minimum to run so I added a command to copy the example .env file & generate a key (these can be found in the composer.json file scripts section):

# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/VYk8Lw for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: phpunit/phpunit:5.0.3

pipelines:
default:
- step:
script: # Modify the commands below to build your repository.
- php -r "file_exists('.env') || copy('.env.example', '.env');"
- composer install
- php artisan key:generate
- phpunit

Once the tests passed I wanted to add database testing for Sqlite. I added the DatabaseMigrations trait to the ExampleTest file and created a new simple test to see a user’s email in the users table after creating it with a factory:

<?phpuse Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
use DatabaseMigrations;
/**
* A basic database test example.
*
* @return void
*/
public function testDatabase()
{
$user = factory(App\User::class)->create();
$this->seeInDatabase('users', [
'email' => $user->email
]);
}
}

Next in order to use Sqlite I added env variables to the phpunit.xml file:

<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

</php>

I originally had the DB_DATABASE set to database/database.sqlite and had to create the file with a command in bitbucket-pipelines.yml however I can just use :memory: to use an in-memory db which simplifies things and speeds up the tests.

Huzzah Passing Tests!

Passing tests!

The link to my repo with all this is below, any issues or feedback just create an issue on the tracker:

Lots of other things that are possible so checkout the docs such as Slack integration on build status.

--

--