B A C K
How to Get Started with Continuous Integration (CI) for Drupal 7

How to Get Started with Continuous Integration (CI) for Drupal 7

Knowledge Centre

Continuous Integration (CI) helps automate the testing process in Drupal 7 web development. While the Drupal community offers numerous resources about CI benefits, this tutorial focuses on establishing a complete CI setup on CentOS 7. We'll walk through setting up Jenkins and implementing basic Drupal site testing.

System Prerequisites

Our tutorial uses CentOS 7 as the base operating system. Let's begin by installing the necessary components.

Installing Java

First, we need to install Java, which is required for Jenkins:

yum install java -y

Installing Jenkins

  • # Add Jenkins repository
  • wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
  • # Import Jenkins repository key
  • rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
  • # Install and configure Jenkins
  • yum install jenkins -y
  • chkconfig jenkins on
  • service jenkins start

Configuring Firewall

For CentOS 7, we'll configure firewalld to allow Jenkins access. It's recommended to restrict access to trusted IPs:

  • # Add trusted IP to firewall zone
  • firewall-cmd --permanent --zone=trusted --add-source=TRUSTED_IP_ADDRESS
  • # Configure port access
  • firewall-cmd --zone=trusted --add-port=8080/tcp --permanent
  • firewall-cmd --reload
  • # Verify configuration
  • firewall-cmd --get-active-zones

Database Setup

Install and configure MariaDB:

  • # Install MariaDB
  • yum install mariadb-server
  • service mariadb start
  • chkconfig mariadb on
  • # Secure the installation
  • /usr/bin/mysql_secure_installation

Installing phpMyAdmin

  • # Install EPEL repository and phpMyAdmin
  • yum install epel-release
  • yum install phpmyadmin
  • # Configure access control
  • nano /etc/httpd/conf.d/phpMyAdmin.conf

Update the phpMyAdmin configuration to restrict access to your IP:

  • # Replace YOUR_IP_ADDRESS with your actual IP
  • Require ip YOUR_IP_ADDRESS
  • Allow from YOUR_IP_ADDRESS

Restart Apache to apply changes:

systemctl restart httpd.service

PHP and Development Tools

Install PHP and required dependencies:

  • 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_fcgid php-cli httpd-devel git

Installing Development Dependencies

Set up PEAR, Composer, and other tools:

  • # Install PEAR and Phing
  • pear channel-discover pear.phing.info
  • pear install --alldeps phing/phing
  • pear install HTTP_Request2
  • # Install Composer
  • curl -sS https://getcomposer.org/installer | php
  • mv composer.phar /usr/local/bin/composer
  • # Install Drush
  • pear channel-discover pear.drush.org
  • pear install drush/drush

Installing Code Quality Tools

Set up Coder and PHP_CodeSniffer for Drupal 7:

  • # Install Coder and PHP_CodeSniffer
  • composer global require drupal/coder:\<8
  • composer global require squizlabs/PHP_CodeSniffer:\<2
  • # Configure PHP_CodeSniffer
  • ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin
  • ln -s ~/.composer/vendor/bin/phpcbf /usr/local/bin
  • phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
  • # Install Drupal Coder
  • 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

Verify the installation:

  • phpcs --version
  • phpcs -i # Should list Drupal in available standards
  • Setting Up Jenkins Project
  • 1. Access Jenkins at `http://hostname:8080`
  • 2. Configure security settings:
  • - Enable security
  • - Set up project-based matrix authorization
  • - Create an administrator account
  • 3. Install required plugins:
  • - Phing Plugin
  • - PHP Plugin
  • - Git Plugin (GitHub or GitLab)

Creating Your First Project

  • 1. Create a new freestyle project named "drupaltest"
  • 2. Set up the workspace structure:
  • ```bash
  • cd /var/lib/jenkins/workspace/drupaltest
  • mkdir docroot scripts make reports
  • chown -R jenkins:jenkins *
  • chmod 0755 -R *

Creating Build Scripts

Create the PHP syntax check script (`scripts/build-syntaxcheck.xml`):

  • <?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>

Create the Drupal coding standards check script (`scripts/build-drupalstandards.xml`):

  • <?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>

Configuring Jenkins Build

  • 1. Add Phing build steps:
  • - First target: `syntaxcheck_php` with build file `$WORKSPACE/scripts/build-syntaxcheck.xml`
  • - Second target: `phpcs` with build file `$WORKSPACE/scripts/build-drupalstandards.xml`
  • 2. Add post-build action:
  • - Configure Checkstyle report path: `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.

Important Notes

You may need to increase PHP memory limit and execution time in php.ini for larger projects

Always test your CI configuration in a development environment first

Regular maintenance of your CI server is important for optimal performance

Consider implementing automated deployment in future iterations