Jenkins build pipelines and PHP Unit tests and what Docker can do for you

Jenkins build pipelines and PHP Unit tests and what Docker can do for you

Some time ago I didn’t like Jenkins Pipelines at all. I preferred to concatenate Jenkins jobs by triggering one Job after the other in a job chain using the build triggers. Together with the Build Flow Plugin you get a good overview of the job chain. This workes like a charm and I like the approach. I know about the bugs and flaws of the Build Flow Plugin but I accept that.

But then I updated my environment to Jenkins 2 and I came over the Pipelines again. I got curious and started reading about the Pipelines because I could use them instead of the Build Flow Plugin and use them instead of concatenating Jenkins jobs with build triggers. After some reading, for me Pipelines are some kind of „Software defined Builds“.  And the idea to have a build script written in Groovy was quite nice. You can develop scripts and have all the possibilities of a modern programming language like conditions, try/catch blocks and so on.

At the same time I wanted to make my PHP project ready for PHP 7. But how to make sure to not break anything by updating database drivers or changing code to run in PHP 7? I remembered from former Java projects I was involved that there are unit tests. And luckily there are unit tests for PHP as well. I started to get familar how unit tests in PHP work and what you can do.

Unit tests can cover database functionality as well. But there are some prerequisites to run unit tests on databases. You need a database with the database schema created before starting the unit tests and for sure you need test data which you have to load into the database to have a well defined environment to run the tests with reproducable results. My first approach was to create a test database on a database server, create the schema and run the tests. But my PHP project supports MySQL and PorgreSQL. So I would need two test databases. And as always I have no suitable PostgreSQL installation available.

Why not combine Jenkins Pipelines, PHP unit tests and Docker? A MySQL/MariaDB Database can run in a Docker container and a PostgreSQL database can run there as well. Why not use dockerized MySQL/MariaDB and PostgreSQL? I decided to use database containers without created database inside. The database schema I will create after the database is running in the Docker container. And why not put the PHP application into a Docker cotainer as well and bundle it with a database to make tests easier. For that I use a Docker compose file bundling application and database comtainers together.

And that’s the point Jenkins Build Pipelines come into play. With Groovy I have a programming language which I can use to catch errors and use conditions to run the job depending on job parameters. As in my opinion scripted pipelines have less limitations than declarative ones, I started to dig into Groovy and scriptes pipelines. Luckily there is a snippet generator in Jenkins included to help starting scripting wit generating snippets.

Pipeline example

Pipeline example

The picture shows the pipeline I setup for my PHP project. Let’s dig a little bit deeper. One of my favourite features is that I can run things in parallel in the pipeline as I did before with the concatenated Jenkins Jobs. The above pipeline breaks as soon as one of the stages fails. Before stopping some cleanup will be done depending on the stage to break and a nitification will be sent to inform about the build failure.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert