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.

Requirements:

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:

YAML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

pipelines:

default:

- step:

caches:

-composer

-vendor-directory

image: zaherg/php-7.1-xdebug-alpine:latest

script:

-composerinstall--no-progress--no-suggest--prefer-dist

-cp.env.testing.env

-sed-i"s/DB_DATABASE=.*/DB_DATABASE=mytest_database/".env

-sed-i"s/DB_USERNAME=.*/DB_USERNAME=root/".env

-sed-i"s/DB_PASSWORD=.*/DB_PASSWORD=test_user_password/".env

-phpartisankey:generate

-phpartisanmigrate

-phpvendor/bin/phpunit.

services:

-mysql

definitions:

caches:

vendor-directory: vendor

services:

redis:

image: redis

mysql:

image: mysql:5.7

environment:

MYSQL_DATABASE: 'mytest_database'

MYSQL_ROOT_PASSWORD: 'test_user_password'

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

Caches section will cache the following folders:

~/.composer/cache: which will allow us to load the dependency directly from the cache instead of pulling them from the internet.

vendor directory: this way we don’t need to install everything every time we run our tests.

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.

Script section: contain all the commands that you want to execute so I do the following:

Install the dependency using composer install

Create a new copy from file .env.testing and call it .env

Then do some regex to change the default mysql user to the one am using in mysql service.

Then run migration and the PHPUnit test.

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:

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

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

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

YAML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

default:

- step:

caches:

-composer

-vendor-directory

image: zaherg/php-7.1-xdebug-alpine:latest

script:

-composerinstall--no-progress--no-suggest--prefer-dist

-cp.env.testing.env

-sed-i"s/DB_DATABASE=.*/DB_DATABASE=mytest_database/".env

-sed-i"s/DB_USERNAME=.*/DB_USERNAME=root/".env

-sed-i"s/DB_PASSWORD=.*/DB_PASSWORD=test_user_password/".env

-phpartisankey:generate

-phpartisanmigrate

-phpvendor/bin/phpunit.

services:

-mysql

-redis

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