3 min read



This is the first in a series on Using Jenkins for Drupal and Wordpress. Over the next few posts I will cover which plugins to use, server-side software needed, how to back up the remote database, testing each commit and more.

Jenkins is an open source automation server which enables developers around the world to reliably build, test, and deploy their software.1

The commands that I will be using have been tested using Ubuntu 16.04 2. Jenkins will run on Mac, Windows and most Unix/Linux based servers Java 8 (either JRE or JDK) If you do not have a server for the install you can get one for a low as $5 a month from Digital Ocean. Though Jenkins will run on the low-end server for testing for production I would highly recommend a multi-core setup, the $40 plan or larger.

As a first run, we are going to run Jenkins on the same host as our testing server. As you become more familiar with working with Jenkins I suggest running a master and multiple slave agents. Do not try to run Jenkins on your Drupal or Wordpress production servers.

Pre-Installation setup

Remote into your server with a user that has sudo privileges, it is not advised to use the root user.

To us Jenkins for php testing we need to install a few tools first.

sudo apt-get update
sudo apt-get install -y --no-install-recommends apt-utils


Install PHP and git

sudo apt-get install -y php php-cli php-xsl php-json php-curl php-intl php-mcrypt php-pear curl php-mbstring git ant php-mysqlnd zip
sudo apt-get clean -y

Install a webserver3

sudo apt-get apache
sudo apt-get nginx

Install Composer4

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

Install PHPUnit5

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

Install PHPLoc6

wget https://phar.phpunit.de/phploc.phar
chmod +x phploc.phar
sudo mv phploc.phar /usr/local/bin/phploc
phploc  --version

Install PHP CodeSniffer7

sudo pear install PHP_CodeSniffer
phpcs --version

Install PHP Depend8

wget https://static.pdepend.org/php/latest/pdepend.phar
chmod +x pdepend.phar
sudo mv pdepend.phar /usr/local/bin/pdepend
pdepend  --version

Install PHP Mess Detector9

wget -c https://static.phpmd.org/php/latest/phpmd.phar
chmod +x phpmd.phar
sudo mv phpmd.phar /usr/local/bin/phpmd
phpmd  --version

Install PHP Copy/Paste Detector (PHPCPD)10

wget https://phar.phpunit.de/phpcpd.phar
chmod +x phpcpd.phar
sudo mv phpcpd.phar /usr/local/bin/phpcpd
phppcpd  --version

Install PHP Documentation Generator

wget https://phpdox.de/releases/phpdox.phar
chmod +x phpdox.phar
sudo mv phpdox.phar /usr/local/bin/phpdox
phpdox --version


Now that we have the requirements done we can install Jenkins and the needed plugins:

wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

During the install a jenkins user will be created, if you are running Jenkins on a staging server I suggest adding jenkins to the www-data group

sudo usermod -g www-data jenkins

Add Drupal and Wordpress Coding standards to Jenkins user

sudo su - jenkins
# Install Composer tools
# composer parallel install plugin
composer global require hirak/prestissimo
# Drupal Coder, PHP_CodeSniffer, and Drupal Coding Standards
composer global require drupal/coder
# Adds WordPress Coding Standards
composer global require wp-coding-standards/wpcs:dev-master
# Sets Config for PHP_CodeSniffer
sudo phpcs --config-set installed_paths /var/lib/jenkins/.composer/vendor/drupal/coder/coder_sniffer,/var/lib/jenkins/.composer/vendor/wp-coding-standards/wpcs

Open your browser and navigate to https://localhost:8080/ substitute localhost with the server IP address if you are installing remotely. You should see a screen like the one below.

[caption id=”attachment_122” align=”alignnone” width=”1986”]First screen you see after installing Jenkins First screen you see after installing Jenkins[/caption]

sudo more /var/lib/jenkins/secrets/initialAdminPassword

Choose the “Install Suggest Plugins” options, or you can customize your own. Then create your admin user. If successful you will see the “Welcome to Jenkins” message

[caption id=”attachment_124” align=”alignnone” width=”2514”]Initial welcome screen for Jenkins Initial welcome screen for Jenkins[/caption]

Next, we will need to get the API Token for your admin user. Click on People, the user you created, then Configure, finally Show API Token....

[caption id=”attachment_123” align=”alignnone” width=”2510”]Jenkins user form Jenkins user form[/caption]

Copy the token to a safe place for use in the command line.

Open the terminal again to add the required plugins. Replace the user and API token with your own.

wget https://localhost:8080/jnlpJars/jenkins-cli.jar
java -jar jenkins-cli.jar -auth [user]:[apitoken] -s https://localhost:8080/ install-plugin checkstyle \
cloverphp crap4j dry htmlpublisher jdepend plot pmd violations warnings xunit publish-over-ssh \
ansicolor bitbucket slack lockable-resources pipeline-milestone-step

java -jar jenkins-cli.jar -auth [user]:[apitoken] -s https://localhost:8080 safe-restart

That is it! You have installed Jenkins and everything required for testing PHP projects.