Using Jenkins for Drupal and WordPress – Your First Job

This is part 3 in a series. If you are not familiar with Jenkins, please read Part 1 and Part 2 first.

This walkthrough should give you an overview of the Freestyle Project.

Our first Jenkins job we will set up is to run Drupal Cron every hour, with a 1 hour throttle.

Jenkins — Drupal Cron

  1. From the main menu select New Item
  2. Enter a Name for the job and select Freestyle Project, click OK at the bottom of the form.

    You should now be on the config screen.
  3. Check Discard Old Builds, cleaning up older builds will help the admin interface perform better. You can choose between Days or Number of Jobs to keep.
  4. Check Build periodically, this sets the schedule for the build. Jenkins cron system adds a ‘H’ symbol. This is a hashed value based on the job name.
  • H * * * * Would once an hour
  • H H * * * Would once a day
  • H H(0-7) * * * means some time between 12:00 AM (midnight) to 7:59 AM.
  1. In the Build section choose Add build step and Execute shell
  2. On your Drupal site navigate to /admin/config/system/cron and copy the cron URL
  3. End the command text box enter the wget -O - -q -t 1
  4. Save and Build now. If all went well the job will be successful.

While curl works for cron, you will need ssh for more robust remote work. Using SSH Agent Pluginyou can add ssh keys per project, folder or globally. If you have access to ssh in to the server it’s best to add the trusted_hosts via sudo su -l jenkins (setup is beyond the scope of this article).

If you do not have shell access you can use -o StrictHostKeyChecking=no for the first run of the job. Removing the flag after you have verified connection will ensure the remote server has not changed.

ssh -o StrictHostKeyChecking=no -t [email protected] "/usr/local/bin/drush cron -y --root=/var/www/"

Once you have ssh access setup and working you can perform other functions; here are a few examples.

Back up the database

ssh -t  [email protected]  "/usr/local/bin/drush sql-dump -y --root=/var/www/  --gzip --result-file=~/dbbackup/backup.sql"
scp  [email protected]:dbbackup/backup.sql.gz /var/livedb/backup.sql.gz

Flush cache

ssh -t  [email protected]  "/usr/local/bin/drush cr -y --root=/var/www/"

Import config

ssh -t  [email protected]  "/usr/local/bin/drush cim -y --root=/var/www/"

WordPress Cron

ssh -t  [email protected]  "/usr/local/bin/wp cron event run --all --path=/var/www/"

WordPress Flush Cache

ssh -t  [email protected]  "/usr/local/bin/wp cache flush --path=/var/www/"

These examples are just the start of what Jenkins can do for you. If you have questions please leave them in the comments.

Using Jenkins for Drupal and WordPress – Navigating and Setup

Our second in the Using Jenkins for Drupal and WordPress series, we will cover the interface and a few key administration settings. ReadPart 1 – Installation


Jenkins interface out to the box can look utilitarian to some if you are not used to it. There are also elements of confusion as it can change or disappear based on the type of page you are on or plugins you have installed.

When you first start Jenkins you will be greeted with this welcome screen

On the left side of the screen, you will find the Main Menu, Build Queue and Build Executor Status

New Item – Brings up the job type selection page.
People – Listing of people Jenkins is aware of. This could be users with a password or names it has pulled from git commits.
Build History – Timeline and list view of the job build history.
Manage Jenkins – Admin and configuration settings for the system, adding plugins, managing users, and permissions.
Credentials – Quick link to the values you have setup. Here you can add ssh keys, APT tokens, and other settings. Values can be used in your Jobs and pipeline scripts.
Build Queue – Jobs waiting to be run.
Build Executor Status – Active Jobs

Manage Jenkins

For now, we will just cover the Configure System area, where you will make the bulk of the system-wide changes and the Manage Plugins.

Manage Plugins

There are four tabs on the plugin page Updates, Available, Installed and Advanced. The sections are self-explanatory, I myself have not found a need to change any settings in the Advanced tab. I encourage you to explore and read about the available plugins and try a few out on a testing environment.

Configure System

Here are a few settings I use and update as needed including Slack notification and core settings.

Maven Project Configuration Section:

\# of executors – The number of jobs Jenkins can run at one time. For what we are doing you will need at least 2. If you want to run parallel testing you will want to run 4 or more.
Labels – When running multiple Jenkins instances in master/slave Labels allow you do ‘tag’ where a process or job is run.
Quiet period – When this option is checked, newly triggered builds of this project will be added to the queue, but Jenkins will wait for the specified period of time before actually starting the build.
SCM checkout retry count – When working with git how often should Jenkins retry checkout.

Global properties

Checking on the Environment variables setting will expose the list of variables configured. Names are usually all uppercase i.e. LIVE_SERVER_PATH, USERNAME,

Using Environment variables are a good idea for security items like DeployBot API Keys or for items that could change like remote IP addresses.

Jenkins Location Section:

Jenkins URL – The URL for Jenkins server
System Admin e-mail address – Email address of the admin

Global Slack Notifier Settings

I recommend adding the Jenkins App
Base URL – If you are using the Slack Jenkins Bot the URL
Integration Token Credential ID – create a secret text entry using the token from Slack, the ID should lowercase no spaces, the Description is free-form text.

Is Bot User? – Checked

The next post we will cover building your first job.

Using Jenkins for Drupal and WordPress – Installation

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('', '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

chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

Install PHPLoc6

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

chmod +x pdepend.phar
sudo mv pdepend.phar /usr/local/bin/pdepend
pdepend  --version

Install PHP Mess Detector9

wget -c
chmod +x phpmd.phar
sudo mv phpmd.phar /usr/local/bin/phpmd
phpmd  --version

Install PHP Copy/Paste Detector (PHPCPD)10

chmod +x phpcpd.phar
sudo mv phpcpd.phar /usr/local/bin/phpcpd
phppcpd  --version

Install PHP Documentation Generator11

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 - | sudo apt-key add -
sudo sh -c 'echo deb 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 http://localhost:8080/ substitute localhost with the server IP address if you are installing remotely. You should see a screen like the one below.

First screen you see after installing Jenkins
First screen you see after installing Jenkins
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

Initial welcome screen for Jenkins
Initial welcome screen for Jenkins

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....

Jenkins user form
Jenkins user form

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 http://localhost:8080/jnlpJars/jenkins-cli.jar
java -jar jenkins-cli.jar -auth [user]:[apitoken] -s http://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 http://localhost:8080 safe-restart

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