GitLab CI - Minimum requirements for PHPUnit
I do most of my coding in PHP. Always have, likely always will. Yes, I dabble in other technology, but I am getting to a point where I spend more time planning the structure of software and doing code reviews. Basically, I'm becoming a manager! But as I keep coding and come up with other projects to build, I inevitably come up against needing a new CI pipeline. This is so I can run the unit tests via PHPUnit.
Normally copy and paste comes in at this point, or looking at creating a template to include. My latest projects (more about that another time) saw me start by copying an existing template for the setup script. The before_script
section of the file looked like
before_script:
- DEBIAN_FRONTEND=noninteractive apt update
- DEBIAN_FRONTEND=noninteractive apt-get -y install zip unzip wget apache2 curl php7.4 libapache2-mod-php php7.4-mysql php7.4-dom php7.4-mbstring php7.4-zip php7.4-xdebug php7.4-curl mysql-client php7.4-redis php7.4-gd
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Great. It works, but there's a lot there which this project doesn't require. In fact, I figured I only needed PHP, Curl, and XDebug (for code coverage), so I chopped it down to:
before_script:
- DEBIAN_FRONTEND=noninteractive apt update
- DEBIAN_FRONTEND=noninteractive apt-get -y install curl php php-xdebug php-curl
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Simple. Everything was there to run. It worked on my machine (doesn't it always!), and I was ready for my first pipeline success...
Ok, not what I hoped for, but there we are. A little look at the error, and we've missed some of the extensions which are required by PHPUnit. I added those, and the updated YAML section became:
before_script:
- DEBIAN_FRONTEND=noninteractive apt-get update
- DEBIAN_FRONTEND=noninteractive apt-get -y install curl php php-xdebug php-curl php-dom php-json php-mbstring php-pdo
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Gah! Not again! I was sure I was on to a winner that time, but thankfully the console output helps.
Following that message, I added zip, unzip, and php-zip to the install line, making the before_script become:
before_script:
- DEBIAN_FRONTEND=noninteractive apt-get update
- DEBIAN_FRONTEND=noninteractive apt-get -y install curl php php-xdebug php-curl php-dom php-json php-mbstring php-pdo zip unzip php-zip
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Once committed, the pipeline ran, with the following result:
Success! There we have it, the minimum amount to install for PHPUnit to run a successful pipeline on GitLab is:
- curl
- php
- php-xdebug (this is actually optional - but I needed it)
- php-curl
- php-dom
- php-json
- php-mbstring
- php-pdo
- zip
- unzip
- php-zip
Make sure they are part of your installation (either as part of a script, or as part of the docker image), and you should be able to get a successful pipeline running quickly and easily.