64
# ! Phil Norton Getting Started With Jenkins and Drupal DrupalCampNW 2013

Getting Started With Jenkins And Drupal

Embed Size (px)

DESCRIPTION

Jenkins is a really powerful tool for automating things like code analysis, testing and even deployment. Getting started with Jenkins, especially with Drupal, is a challenge and can be quite difficult for a beginner to the system. In this session I'll show you how to install Jenkins, how to configure things like authentication and then how to do some interesting things with the tool. I'll show some real life examples of things that can be done with the tool on your Drupal sites to do things like run cron jobs, syntax check the code or even automatically copying code to your web servers.

Citation preview

Page 1: Getting Started With Jenkins And Drupal

#!

Phil Norton

Getting Started With Jenkins and

Drupal

DrupalCampNW 2013

Page 2: Getting Started With Jenkins And Drupal

#!

Me• Phil Norton (@philipnorton42)

• #! code (www.hashbangcode.com)

• Technical Lead at Access

• Help run NWDUG

• DrupalCampNW2013 co-organizer

• Help out at PHPNW

Page 3: Getting Started With Jenkins And Drupal

#!

Jenkins• Build and automation server

• Java application

• Used to be called Hudson

• Built for continuous integration

• Helps you run repetitive tasks

• http://jenkins-ci.org/

Page 4: Getting Started With Jenkins And Drupal

#!

Page 5: Getting Started With Jenkins And Drupal
Page 6: Getting Started With Jenkins And Drupal

#!

Jenkins Drupal Examples

• Inspect your code

• Run cron

• Build documentation

• Test drush make files

• Test Drupal install profiles

• Run tests

• Deploy sites

Page 7: Getting Started With Jenkins And Drupal

#!

Installing Jenkins

• Different for most platforms

• Go to jenkins-ci.org

• Click on your platform on right hand side

• Follow instructions

Page 8: Getting Started With Jenkins And Drupal

#!#!

Installing On Ubuntu

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

Add “deb http://pkg.jenkins-ci.org/debian binary/“ to the file /etc/apt/sources.list

sudo apt-get update

sudo apt-get install jenkins

Page 9: Getting Started With Jenkins And Drupal

#!

localhost:8080

Page 10: Getting Started With Jenkins And Drupal

#!

Authentication

• Jenkins doesn’t have authentication when first installed

• Probably a good idea to enable it!

• Many different models from local user credentials to domain authentication

Page 11: Getting Started With Jenkins And Drupal

#!

Creating A User In Jenkins

• Simplest security model for beginners

• Manage Jenkins > Configure Security > Enable Security

• Manage Jenkins > Manage Users > Create User

Page 12: Getting Started With Jenkins And Drupal

#!

ooopss…

• Quite easy to lock yourself out

• Open the file config.xml remove the element <useSecurity> to remove security

Page 13: Getting Started With Jenkins And Drupal

#!

Anatomy Of Jenkins• Jenkins can be found at /var/lib/jenkins

• May contain one or more of the following:.sshconfig.xmljobs/nodeMonitors.xmlplugins/secret.keysecret.key.not-so-secretupdates/userContent/users/workspace/

Page 14: Getting Started With Jenkins And Drupal

#!

Your First Jenkins Project

Page 15: Getting Started With Jenkins And Drupal

#!

Drupal Cron

• Start off using Jenkins with something simple

• Running a Drupal cron through Jenkins is easy

Page 16: Getting Started With Jenkins And Drupal

#!

Drupal Cron• Create a ‘free-style software project’

Page 17: Getting Started With Jenkins And Drupal

#!

Page 18: Getting Started With Jenkins And Drupal

#!

Drupal Cron Build Trigger

• Select ‘build periodically’

• Crontab syntax applies

• Can also use @hourly to run the job every hour

Page 19: Getting Started With Jenkins And Drupal

#!

Drupal Cron Build Step

• Create an Execute shell build task

• Use 'wget' to call the cron page remotely

Page 20: Getting Started With Jenkins And Drupal

#!

Notifications• Useful to know if a job fails

• Email notifications are available

• Sends detailed notifications on failed jobs

Page 21: Getting Started With Jenkins And Drupal

#!

Jenkins Job List

Page 22: Getting Started With Jenkins And Drupal

#!

Jenkins Plugins

Page 23: Getting Started With Jenkins And Drupal

#!

Jenkins Plugins

• Jenkins can be extended with plugins

• Install them via the plugin manager

• Manage Jenkins > Manage Plugins

Page 24: Getting Started With Jenkins And Drupal

#!

Drupal Code Analysis In Jenkins

Page 25: Getting Started With Jenkins And Drupal

#!

Drupal Code Analysis

• Syntax checking

• Check for coding standards violations

• Analyse for duplication of code

• Check for potential problems in source code

Page 26: Getting Started With Jenkins And Drupal

#!

STAHP!

Page 27: Getting Started With Jenkins And Drupal

#!

Some Rules For Using Jenkins With Drupal

• Use source control

• Only analyse your own code as priority

• Know what you are analysing

• Understand the output

• Standardise as much as you can

Page 28: Getting Started With Jenkins And Drupal

#!

Standardise

Standard repo structure

docroot/scripts/make/

Standard theme structure

template.phptemplates/js/css/

Standard Drupal setup

sites/all/modules/contrib/sites/all/modules/custom/

Standard custom module structure

module.moduleincludes/module.inc

Page 29: Getting Started With Jenkins And Drupal

#!

Git

• Use source control to pull code into Jenkins

• To use Git you need the Jenkins Git plugin

• Best results are using SSH (you will also need some ssh keys)

• Perhaps the most complex part of the job setup

Page 30: Getting Started With Jenkins And Drupal

#!#!

Git

• Use source control to pull code into Jenkins

• To use Git you need the Jenkins Git plugin

• Best results are using SSH (you will also need some ssh keys)

• Perhaps the most complex part of the job setup

Page 31: Getting Started With Jenkins And Drupal

#!

Add your git ssh URL to Jenkins

Jenkins will give you a warning if it can’t see the repo

Page 32: Getting Started With Jenkins And Drupal

#!

Syntax Checking

Page 33: Getting Started With Jenkins And Drupal

#!

PHP Lint

• Syntax check any PHP file with

php -l myfile.php

• Can only do one file at a time

• How can we check an entire project?

Page 34: Getting Started With Jenkins And Drupal

#!

• Build tool

• Controlled with XML files

• Written in PHP

• Available through PEAR

• Integrates with Jenkins

• Certified awesome!

Page 35: Getting Started With Jenkins And Drupal
Page 36: Getting Started With Jenkins And Drupal

#!

Phing And PHP Lint

• Add a Phing build file to your source code

• What we need to do is:

1. Tell Phing what files we want to scan

2. Use the phplint Phing task to scan files

3. Fail the build if any errors found

Page 37: Getting Started With Jenkins And Drupal

#!

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

Page 38: Getting Started With Jenkins And Drupal

#!

Running PhingSyntax Check

• Add the Phing file to your source code as build.xml

• You can run this build using the following:

phing -f scripts/build.xml syntaxcheck_php

Page 39: Getting Started With Jenkins And Drupal

#!

Add Phing Build Step To Jenkins

• Use the Jenkins Phing Plugin to invoke the syntaxcheck_php Phing target

Page 40: Getting Started With Jenkins And Drupal

#!

What Next?

Page 41: Getting Started With Jenkins And Drupal

#!

Drupal Coding Standards

https://drupal.org/coding-standards

Page 42: Getting Started With Jenkins And Drupal

#!

Checkstyle

• Jenkins Plugin

• Reports on coding standards results

• Accepts a checkstyle.xml file produces lots of graphs and code reports

Page 43: Getting Started With Jenkins And Drupal

#!

PHP CodeSniffer

• Inspect code for Drupal coding standards

• Produces checkstyle.xml files for Checkstyle

• Looks for best practice in your code

Page 44: Getting Started With Jenkins And Drupal

#!

Install PHP CodeSniffer

• Install the PHP CodeSniffer package

sudo pear install PHP_CodeSniffer

• Download Coder and link the Drupal CodeSniffer standard

drush dl coder

sudo ln -sv /path/to/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal

Page 45: Getting Started With Jenkins And Drupal

#!#!

phpcs docroot/sites/all/modules/custom

Page 46: Getting Started With Jenkins And Drupal

#!#!

phpcs --standard=Drupal docroot/sites/all/modules/custom

Page 47: Getting Started With Jenkins And Drupal

#!#!

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme docroot/sites/all/modules/custom

Page 48: Getting Started With Jenkins And Drupal

#!#!

phpcs --report=checkstyle --standard=Drupal --extensions=php,module,inc,install,test,profile,theme docroot/sites/all/modules/custom

Page 49: Getting Started With Jenkins And Drupal

#!#!

phpcs --report-file=reports/checkstyle.xml --report=checkstyle --standard=Drupal --extensions=php,module,inc,install,test,profile,theme docroot/sites/all/modules/custom

Page 50: Getting Started With Jenkins And Drupal

#!#!

The Missing Element?

• Git stores the code

• phpcs produces the reports

• Jenkins and processes reports

• What fits all these elements together?

Page 51: Getting Started With Jenkins And Drupal

#!

Phing!(did I mention it was awesome?)

Page 52: Getting Started With Jenkins And Drupal

#!

phpcs Phing Module

• Tell Phing what files we want to scan

• Use the phpcs Phing module to scan the files and what standard to use

• Output the report in a checkstyle.xml file

Page 53: Getting Started With Jenkins And Drupal

#!

<?xml version="1.0"?><project name="phpcodesniffer" default="phpcs"> <target name="phpcs" depends="findcustomfiles"> <fileset dir="../docroot" id="drupalfiles"> <include name="sites/all/modules/custom/**/*.php" /> <include name="sites/all/modules/custom/**/*.inc" /> <include name="sites/all/modules/custom/**/*.module" /> <include name="sites/all/modules/custom/**/*.install" /> <include name="sites/all/themes/mytheme/*.php" /> <include name="sites/all/themes/mytheme/*.inc" /> </fileset>

<phpcodesniffer standard="Drupal" format="checkstyle"> <fileset refid="drupalfiles" /> <formatter type="checkstyle" outfile="../reports/checkstyle.xml"/> </phpcodesniffer> </target>

</project>

Page 54: Getting Started With Jenkins And Drupal

#!

<?xml version="1.0"?><project name="phpcodesniffer" default="phpcs"> <target name="phpcs" depends="findcustomfiles"> <fileset dir="../docroot" id="drupalfiles"> <include name="sites/all/modules/custom/**/*.php" /> <include name="sites/all/modules/custom/**/*.inc" /> <include name="sites/all/modules/custom/**/*.module" /> <include name="sites/all/modules/custom/**/*.install" /> <include name="sites/all/themes/mytheme/*.php" /> <include name="sites/all/themes/mytheme/*.inc" /> </fileset>

<phpcodesniffer standard="Drupal" format="checkstyle"> <fileset refid="drupalfiles" /> <formatter type="checkstyle" outfile="../reports/checkstyle.xml"/> </phpcodesniffer> </target>

</project>

Page 55: Getting Started With Jenkins And Drupal

#!

<?xml version="1.0"?><project name="phpcodesniffer" default="phpcs"> <target name="phpcs" depends="findcustomfiles"> <fileset dir="../docroot" id="drupalfiles"> <include name="sites/all/modules/custom/**/*.php" /> <include name="sites/all/modules/custom/**/*.inc" /> <include name="sites/all/modules/custom/**/*.module" /> <include name="sites/all/modules/custom/**/*.install" /> <include name="sites/all/themes/mytheme/*.php" /> <include name="sites/all/themes/mytheme/*.inc" /> </fileset>

<phpcodesniffer standard="Drupal" format="checkstyle"> <fileset refid="drupalfiles" /> <formatter type="checkstyle" outfile="../reports/checkstyle.xml"/> </phpcodesniffer> </target>

</project>

Page 56: Getting Started With Jenkins And Drupal

#!

Add PHP CodeSniffer To Jenkins

• Use the Phing plugin to invoke Phing target

Page 57: Getting Started With Jenkins And Drupal

#!

Publish PHP CodeSniffer Output

• Create a Post Build action

• Reports directory contains PHPCS report

• Checkstyle plugin creates graphs from this file

Page 58: Getting Started With Jenkins And Drupal

#!

Page 59: Getting Started With Jenkins And Drupal

#!

Page 60: Getting Started With Jenkins And Drupal

#!

Page 61: Getting Started With Jenkins And Drupal

#!

Try It Yourself

• PHP Mess Detect Phing output into PMD Analysis Results graphs in Jenkins

• PHP Copy Paste Detect Phing output into Duplicate Code Analysis results graphs in Jenkins

Page 62: Getting Started With Jenkins And Drupal

#!

Do More!

• Use Phing to run an action

• Use Jenkins to automate it

— or —

• Use Phing to generate reports

• Use Jenkins plugins to convert reports into graphs

Page 63: Getting Started With Jenkins And Drupal

#!

joind.in/10137

Page 64: Getting Started With Jenkins And Drupal

#!

Me• Phil Norton (@philipnorton42)

• #! code (www.hashbangcode.com)

• Technical Lead at Access

• Help run NWDUG

• DrupalCampNW2013 co-organizer

• Help out at PHPNW