Continuous Integration with Docker and Bitbucket Pipelines

Quick guide to using Docker and Bitbucket Pipelines to test your code

Joel Caballero
Five & Done
2 min readOct 10, 2017

--

In an effort to create great maintainable code that lasts through the ages, we’ve started to implement Continuous Integration practices into our flow. In the past, it was difficult to maintain the same environments across all developers and servers and so Unit Tests weren’t always first priority. However, now that Docker has become such an intricate part of our development flow and Bitbucket Pipelines uses Docker under the hood to run test, it seems only natural to use both together.

So what does it take to run a unit test? If you’re like me and have tried to Dockerize your life, add the below method to your bashrc or zshrc file.

phpunit () {
docker run \
-t \
--rm \
--volume $(pwd):/app:rw \
5ndn/phpunit \
"$@"
}

The code will run whenever you type phpunit in your console. That particular flavor of container is geared towards Laravel framework but should work for most scenarios. If you’d like more information about the container you can visit the Docker hub page:

https://hub.docker.com/r/5ndn/phpunit/

The Continuous part

Now in order to take advantage of this container inside of pipelines, we’ll need to create our bitbucket-pipelines.yml file below:

pipelines:
default:
- step:
name: Install
image: composer
caches:
- composer
script:
- composer install --ignore-platform-reqs
artifacts:
- vendor/**
- step:
name: Test
image: 5ndn/phpunit:5.7
script:
- phpunit

The first step uses the official composer image repository to run composer install. We run that with the ignore-platform-reqs flag in order to avoid errors related to dependencies that are already present in the phpunit container. The second step uses the same container we’ve created to run the tests locally on any machine. In my case, it will find a phpunit.xml file that I usually include in all of my projects that has other configurations.

Things to consider

For simple applications the above two snippets will work just fine, but with almost any project, things usually get more complicated. Also, Bitbucket pipelines are new still and I’ve noticed my tests failing for reasons internal to Pipelines.

Another thing to consider is that the 5ndn version of phpunit might not be what you need. Although, anyone is welcome to use it, it’s tailored to our projects.

--

--