BitBucket Pipeline configuration for PHP, MongoDB and Symfony

Recently I’ve been playing around with BitBucket and their Pipelines. Just to let you know BitBucket Pipelines is an integrated CI/CD service built into Bitbucket. It basically means that on every commit you make your tests will be ran and your code will be deployed.

They say in their official website that it has a really basic and simple configuration and as far as I could experiment, it really is.

Configuration example for JavaScript/NodeJS

Let me show you a quick example, that worked just out of the box for one of my nodejs projects.

BitBucket pipeline configuration for NodeJs project, using the provided default configuration.

# This is a sample build configuration for JavaScript.
# Check our guides at https://confluence.atlassian.com/x/14UWN 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: node:6.9.4

pipelines:
default:
- step:
name: Build and Test
caches:
- node
script: # Modify the commands below to build your repository.
# installs public and private moduless.
- npm install
# run the tests.
- npm test

It’s just that simple. Enable Pipelines in your Bitbucket repo and upload a file called bitbucket-pipelines.yml to your repo.

With that configuration on every commit to every branch your tests will be ran. And they run pretty fast by the way.

Configuration PHP, MongoDB and Symfony

In my case, I needed something more complex for one of my projects, running PHP with Symfony and using MongoDB as database to run all the integration tests.

So after reading a couple of articles and some documentation about the pipelines I found out that if I’d build my own docker image with all the needed stuff inside, my pipeline configuration will be really simple.

Let’s see firs th Bitbucket pipeline configuration:

# This is a sample build configuration for PHP/Symfony/MongoDB.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: moisesbelchin/php-mongodb

pipelines:
default:
- step:
name: Build and Test
caches:
- composer
script: # Modify the commands below to build your repository.
# start mongodb service.
- mongod --config /etc/mongod.conf --fork
# app/config
- cp app/config/parameters.yml.dev app/config/parameters.yml
# installs deps.
- composer install
# run integration tests.
- bin/test -c app --group=integration-tests

As you can see it’s exactly as simple as we saw before for the JavaScript/NodeJS app.
Bitbucket will pull the provided image in that case my custom docker image. After that will start mongodb service, will install deps using composer and then will run the tests using PHPUnit.

Build your own image

Now that we have the pipeline configured the hard work is done on the docker image.
You can find the used image in this example here: https://hub.docker.com/r/moisesbelchin/php-mongodb

Let’s see how you can build your own image.

In order to build your own image you need to have some knowledge about docker. This is not the goal of this article, but let’s quickly say that you only need to create a file called Dockerfile and run some docker commands to build, test and push your image.

For my project I needed: PHP 5.6, MongoDB 3.2, composer and PHPUnit 5.6. So that’s what I installed in my docker image.

FROM debian:jessie-slim

MAINTAINER Moises Belchin

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 \
&& echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.2 main" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
mongodb-org \
mongodb-org-server \
ca-certificates \
php5 \
php5-mongo \
php5-mcrypt \
curl libcurl3 php5-curl \
wget \
libssl-dev \
unzip \
&& apt-get clean autoclean -y \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN wget https://phar.phpunit.de/phpunit-5.6.phar \
&& chmod +x phpunit-5.6.phar \
&& mv phpunit-5.6.phar /usr/local/bin/phpunit

# This Dockerfile doesn't need to have an entrypoint and a command
# as Bitbucket Pipelines will overwrite it with a bash script.

I based my image on debian:jessie-slim to reduce a bit the size of the image and on top of that I installed mongodb, php5 and some other required packages. After that I installed composers and PHPUnit.

Once you have your Dockerfile ready it’s time to build the image by running:

docker build -t moisesbelchin/php-mongodb .

Test your image

Now that we have the image built, let’s test that everything is installed correctly. To be able to test the image you can run some containers using your custom image, you can go inside the continer and execute some commands, such as starting mongodb or review PHP versions.

Run the image locally and go inside:

docker run -i -t moisesbelchin/php-mongodb /bin/bash

Now, inside the container you can run:

# Check versions
$ php --version
$ mongo --version

# start mongo and check it
$ mongod --config /etc/mongod.conf --fork
$ mongo

Push the image to Docker hub

Now that everything is working as expected, let’s push our image to our Docker hub so Bitbucket pipelines will be able to pull that image and use it on our projects.

Use your credentials to login in Docker Hub.

docker login

Push your new image.

docker push moisesbelchin/php-mongodb

Final note

As you could see Bitbucket pipelines are really easy to setup, even if you have something custom with different software involved. You put the software in your custom docker image and your Pipeline config will stay really simple just running simple commands.

Just as a final comment I’m gonna provide you a quick extra example on how to create another custom image using PHP 7.

Dockerfile for PHP 7

FROM php:7.1.1
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
mongodb \
mongodb-server \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

More info

You’ll find all the files for this example in this repository:

https://github.com/mbelchin/bitbucket-pipelines-php-symfony-mongodb/

github

Leave a comment

Create a website or blog at WordPress.com

Up ↑