I haven’t found any walk-through about setting up jenkins for cakephp projects, and since I got to do it and I have the steps fresh in mind, here is the process.
Setup a Virtual Private Server
VPS are cheap these days, I got one for ~5$ a month on DigitalOcean and chose a clean image of Centos 7. If you’re new to linux or VPS, have a look at those articles:
- Initial Server Setup with CentOS 7
- Additional Recommended Steps for New CentOS 7 Servers
- An Introduction to Securing your Linux VPS
Set yourself up for your PHP project
You need php, and pear
yum install php php-pear
And any other php extension that your project requires
yum install php-intl php-mbstring php-xml php-fileinfo
Composer might need a little more memory than the default php setting. Edit the /etc/php.ini file, find the memory limit and jack it up. 1024M made the job for me.
memory_limit = 1024M
And of course, composer, which we’ll install in the /usr/bin directory so that jenkins user can access it
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer
Xdebug will also be needed
yum install php-devel
yum groupinstall "Development tools"
pecl install xdebug
echo "zend_extension=\"/usr/lib64/php/modules/xdebug.so\"" > /etc/php.d/xdebug.ini
Jenkins relies on java
yum install java-1.7.0-openjdk
Install Jenkins itself
yum install wget
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins
If you have a firewall up, open the port 8080, here’s an example for firewalld:
firewall-cmd --zone=public --add-port=8080/tcp --permanent
We’ll also use Ant to write our build scripts
yum install ant
Fire up jenkins
sudo /etc/init.d/jenkins restart
systemctl restart jenkins.service
You should now be able to point your browser your VPS on port 8080 and access your Jenkins server!
Install PHP Tools (optional)
These tools are commonly used to test php projects. You could add them to your composer dependencies, you could download them in your test script too. For the sake of demonstration, here’s how to install them globally straight on the test server.
chmod +x phpunit.phar
mv phpunit.phar /usr/bin/phpunit# Php CodeSniffer
pear install PHP_CodeSniffer-2.3.4# PHPLOC
chmod +x phploc.phar
mv phploc.phar /usr/bin/phploc# PDepend
chmod +x pdepend.phar
mv pdepend.phar /usr/bin/pdepend# PHPMD
chmod +x phpmd.phar
mv phpmd.phar /usr/bin/phpmd# PHPCPD
chmod +x phpcpd.phar
mv phpcpd.phar /usr/bin/phpcpd# phpDox
chmod +x phpdox.phar
mv phpdox.phar /usr/bin/phpdox
Your server is installed and accessible. So lets start by creating users accounts. Use the menu “Manage Jenkins > Configure Global Security” and check “Enable security”. For an quick setup, under “Security Realm” we’ll use “Jenkins own user database” and make sure that we check “Allow users to sign up”. This way we can create an account later on. Then check “Logged-in users can do anything”. You can now use the signup form and create an account, which should have all permissions by default. You can now head back to the security config and disable sign ups so that no one else registers.
Under “Manage Jenkins > Manage plugins” you’ll find a variety of nice plugins. Here are my favorites:
- GIT plugin : automatically fetch projects from github
- All of the plugins listed here
Create a project
From Jenkins’ main panel, choose “New Item”. Give it a name and make it a “Freestyle project”.
On the following page, scroll to the “Source Code Management” section and check “Git”. Provide the project’s url so the Jenkins knows where to fetch the code. If you were to try and run the build right now, the project would be cloned on your server and complete with success.
The final step is to specify a build script to run, and for this purpose we’ll use Ant. Under the “Build” section, add a build step, make it “Invoke Ant” and leave the path blank. This will instruct the jenkins to look for a build.xml file in your repo.
If for some reason you’d rather keep the build.xml file outside of the repo (not advised), you could copy it to the freshly cloned repo. To do so, add an “Execute shell” build step above “Invoke Ant” and add the following line
cp /my/favorite/path/build.xml ./
The build.xml file is where all of the processing is defined once your project is cloned. Here is a boilerplate build file that updates composer once the project is cloned and then runs phpunit.
<?xml version="1.0" encoding="UTF-8"?>
<project name="requirejs" default="default"> <target name="default" depends="composer,phpunit" /> <target name="composer">
</target> <target name="phpunit" depends="composer">
To use all of the nifty tools and plugins that we have installed, have a look at the following build file as a reference.
You can now run your build. If you face any issue with builds failing, click on the specific build that failed and in the left menu you’ll find “console output”. You should find all necessary hints to understand why the build has failed.
You’re now all set to keep that project clean, tested and automated.