How to test your Laravel application with Bitbucket Pipelines

Bitbucket Pipelines is a CI service that you can use to test your application, yes it has its own limitation, but remember it is still new and hopefully it will get better. Today I had the chance to work with it, and trust me I was not planning to do so, am a happy Gitlab-ci user, but why not.

Each of these service has its pros and cons, and am not planning to go over it and list it, just do a small reading to find the differences, so today I’ll talk about how to test your Laravel Application with Bitbucket Pipelines and how you can get it fast.


For sure you need to know Laravel and how to write tests, this post will not talk about it, and it would be nice if you know a little bit of Docker.

Enabling Pipelines:

You will need to enable the pipelines from the Repository Settings as the following image

Get Your Hands Dirty:

Now that we enabled the pipelines we will need to write the configuration file to tell it exactly what we need to do and how, so first we need to create a Yaml file and we should be called  bitbucket-pipelines.yml and inside that file we will have the following:

the first line indicates that we will start pipelines, and the default behaviour should be the following:

  1. Caches section will cache the following folders:
    1. ~/.composer/cache: which will allow us to load the dependency directly from the cache instead of pulling them from the internet.
    2. vendor directory: this way we don’t need to install everything every time we run our tests.
  2. Image section will pull a docker image from Docker hub, and it can be any image you want, here am using my image which I built based on Alpine and PHP 7.1 with xDebug enabled for it, and it also has Composer which will make life easier and faster. The main idea I found is that you don’t need to build your image every time, it’s time-consuming and it will also affect the payments, as Bitbucket Pipelines bills you per minutes and you have 50min free on the free accounts and 500min on paid, and you will have to pay $10 to get extra 1000min.
  3. Script section: contain all the commands that you want to execute so I do the following:
    1. Install the dependency using composer install
    2. Create a new copy from file .env.testing and call it .env
    3. Then do some regex to change the default mysql user to the one am using in mysql service.
    4.  Then run migration and the PHPUnit test.
  4. Services section: define the services which are connected to our tests environment, here we are linking it with only mysql.

So all the above will do what you need testing your code, now we only need to look at the definitions section.

The definitions section will hold the defined resources you want to use elsewhere in your pipeline configuration. So here we define the path for our vendor directory as the directory for composer is already defined by Bitbucket team.

The services section within the definitions will allow us to have as many services as we want, and those mostly will be the databases that you want your code to have access to, so let’s explain that section a bit, am not going into deep details as you can read it from here:

  1. redis and mysql are just the name that we want to give to the services that we want to use, you can such as use database instead of mysql  and in the services section in default we use database
  2. The image tag is defining the docker image which we should pull, here we are pulling the latest version of redis and version 5.7 from mysql
  3. Since redis needs no environment variables we just add them to mysql, and to know more you should check this article which is rich with examples.

so now if you need to link redis to our default section you just need to add it to the services section so it will be like this

And we are done, now when you push your code to bitbucket this will trigger the pipeline and test your code.

But what if we want to have different tests based on the branch or tags .. etc, this is simple, such as the following code will do the same tests as the default but only on the master branch

you can read more about this feature at this link.


Filed under: Code, docker, Laravel, PHP

1 Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Comment *

Name *
Email *

20 − three =