Testing Laravel with Bitbucket Pipelines

Bitbucket Pipelines is Atlassian’s answer to travis, providing tight integration with Bitbucket for running tasks, such as automated tests, after pushing to a repository.

Pipelines is built on Docker but prior experience isn’t necessary. An advantage of having Docker at its heart is that you can almost infinitely customise your build environment to your project needs, tying your environment down to PHP 5.0.1 if you really need to.

In order to use Docker you must have an image. Pipelines only supports specifying a single image to define your environment, contrary to the popular opinion of having a single container for each of your services. You can find one ready made for a PHP 7 / MySQL environment at hub.docker.com/r/punkstar/bitbucket-pipelines-php7-mysql.

Central to getting Pipelines running is the bitbucket-pipelines.yml, the file that Pipelines will read when it comes to building your project. While not great, I thoroughly recommend giving Atlassian’s documentation a good read.

Here’s an example configuration to get you started:

image: punkstar/bitbucket-pipelines-php7-mysql

pipelines:
  default:
    - step:
        script:
          - service mysql start
          - mysql -h localhost -u root -proot -e "CREATE DATABASE yourtestdatabase;"
          - mv /etc/php/7.0/cli/conf.d/20-xdebug.ini /etc/php/7.0/cli/conf.d/20-xdebug.ini.disabled
          - composer config -g github-oauth.github.com lalalalal
          - composer install --no-interaction --no-progress --prefer-dist
          - php artisan key:generate
          - php artisan migrate
          - mv /etc/php/7.0/cli/conf.d/20-xdebug.ini.disabled /etc/php/7.0/cli/conf.d/20-xdebug.ini
          - vendor/bin/phpunit  --coverage-clover=coverage.xml
          - if [ $? -eq 0 ]; then bash <(curl -s https://codecov.io/bash); fi

Once you have this file in the root of your repository, enable Pipelines on your Bitbucket account, push to your repository and your build will start!