How to get started with continuous integration (CI) for Drupal 7
Countinous Integration helps you to automate the testing process of web development for Drupal 7. There are tons of video and presentations from the Drupal community on the benefits of CI. This tutorial will help you getting started with haveing a complete setup of CI on top of Centos 7. Through the first tutorial you will get Jenkins working and do a first testing on basic Drupal site.
First install your desired flavour of Centos. We've used Centos 7.
Install JAVA on your machine:
yum install java -y
Install Jenkins on Centos 7
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 -y
chkconfig jenkins on
service jenkins start
Open firewalld for Jenkins (in Centos 7), in other Centos versions create a rule in iptables. We prefer to make the Jenkins available from certain IPs. In Centos 7 we create a new zone and add the trusted IPs to that zone.
firewall-cmd --permanent --zone=trusted --add-source=TRUSTEDIP
You can use the above command for several IPs and or for classes of IP.
firewall-cmd --zone=trusted --add-port=8080/tcp --permanent
firewall-cmd --reload
And use this to test it:
firewall-cmd --get-active-zones
Install MariaDB (you will need this later)
yum install mariadb-server
service mariadb start
chkconfig mariadb on
/usr/bin/mysql_secure_installation
Install PhpMyAdmin for Centos 7 and add your IP used to access the service
yum install epel-release
yum install phpmyadmin
nano /etc/httpd/conf.d/phpMyAdmin.conf
and inside replace these lines with your IP that you will use to access the machine:
. . .
Require ip your_IP_address
. . .
Allow from ip your_IP_address
. . .
Require ip ip your_IP_address
. . .
Allow from ip your_IP_address
. . .
and then restart Apache with:
systemctl restart httpd.service
configure php and various other needed tools with the following command:
yum install epel-release
yum install php php-devel php-gd php-ldap php-mysql php-pear php-mcrypt php-tidy php-xml php-xmlrpc php-mbstring
php-snmp php-soap php-xml php-common curl curl-devel perl-libwww-perl ImageMagick libxml2 libxml2-devel mod_fcgi
d php-cli httpd-devel git
Install PEAR for PHP:
pear channel-discover pear.phing.info
pear install [--alldeps] phing/phing
Install HTTP_Request2 library from PEAR:
pear install HTTP_Request2
Install Composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
We need to use the all famously Drush:
pear channel-discover pear.drush.org
pear install drush/drush
We need to install Coder and CodeSniffer for Drupal 7:
composer global require drupal/coder:\<8
ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin
ln -s ~/.composer/vendor/bin/phpcbf /usr/local/bin
composer global require squizlabs/PHP_CodeSniffer:\<2
phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
pear install PHP_CodeSniffer=1.5.6
cd /root
wget http://ftp.drupal.org/files/projects/coder-7.x-2.4.tar.gz
tar xvf coder-7.x-2.4.tar.gz
cd $(pear config-get php_dir)/PHP/CodeSniffer/Standards/
and copy there the Drupal folder from the from /root/coder/. At this stage you can check to see if CodeSniffer is correctly configured with:
phpcs --version
PHP_CodeSniffer version 1.5.6 (stable) by Squiz (http://www.squiz.net)
pear install PHP_CodeSniffer
and you should also see Drupal in the list of standards:
phpcs -i
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend and Drupal
You are not now ready to setup your first test from Drupal. Connect to your Jenkins at http://hostname:8080. Configure Global Security by enable the security and allow an user to signup. Then create an account login and enable Project-based Matrix and ADD the user you just created and apply full permissions. Disable then the ability for anyone do everything on Jenkins. Update the plugins and also install Phing, PHP Pluging the preferrable flavour of GIT (GitHub or GitLab plugin). Install and choose to restart the Jenkins machine. It will automatically install any other dependable plugins.
Let's create the first project. Go to Jenkins, New Item and choose a "Free-style project". We used for the first name "drupaltest". Do not fill in now any other settings. Choose the project and press a first Build so that the workspace is created. The workspace is where your actual Drupal site will be for CI. For this first tutorial, we will just put the site through GIT. In the next howto we will also use automatic pull once a commit has been done in git.
After the workspace was created, go to the folder:
cd /var/lib/jenkins/workspace/drupaltest
We will create a proper structure for the Jenkins build.
mkdir docroot
mkdir scripts
mkdir make
mkdir reports
chown -R jenkins:jenkins *
chmod 0755 -R *
And let's create the first script to check the PHP syntax.
cd scripts
nano build-syntaxcheck.xml
and copy paste inside this text:
<?xml version="1.0"?>
<project name="phpsyntaxcheck" default="syntaxcheck_php">
<target name="syntaxcheck_php" description="Run PHP syntax checking on the project docroot.">
<fileset dir="../docroot" id="phpfiles">
<include name="*.php" />
<include name="**/*.php" />
<include name="**/*.inc" />
<include name="**/*.module" />
<include name="**/*.install" />
<include name="**/*.profile" />
<include name="**/*.test" />
</fileset>
<phplint haltonfailure="true">
<fileset refid="phpfiles" />
</phplint>
</target>
</project>
We will now create a script to check your code files according to Drupal standards.
nano build-drupalstandards.xml
and past inside this text:
<?xml version="1.0"?>
<project name="phpcodesniffer" default="phpcs">
<target name="phpcs">
<fileset dir="../docroot" id="drupalfiles">
<include name="sites/all/modules/**/*.php" />
<include name="sites/all/modules/**/*.inc" />
<include name="sites/all/modules/**/*.module" />
<include name="sites/all/modules/**/*.install" />
<include name="sites/all/themes/**/*.php" />
<include name="sites/all/themes/**/*.inc" />
</fileset>
<phpcodesniffer standard="Drupal" format="checkstyle">
<fileset refid="drupalfiles" />
<formatter type="checkstyle" outfile="../reports/checkstyle.xml"/>
</phpcodesniffer>
</target>
</project>
Update the folder structure in the script above according to your needs. Go to the project you created and choose "Configure". Choose Add Phing target and click Advanced. Add the following values:
Targets: syntaxcheck_php
Phing Build file: $WORKSPACE/scripts/build-syntaxcheck.xml
and add another Phing target for the second check (Drupal Standards).
Targets: phpcs
Phing Build file: $WORKSPACE/scripts/build-drupalstandards.xml
add a Post Build action to gather a report:
Checkstyle results: reports/checkstyle.xml
And press BUILD! You first CI testing should start now! You can check your build results in the status page of the project and the details of each build on the Build page in Console Output.
We will follow with more advanced tutorials. You might need to increase the memory limit and the execution time in php.ini.
Sorry for the long post, here is a photo with a potato.