设为首页 收藏本站
查看: 773|回复: 0

[经验分享] Template for Jenkins Jobs for PHP Projects----------http://jenkins-php.org/

[复制链接]

尚未签到

发表于 2017-4-2 10:54:54 | 显示全部楼层 |阅读模式
Template for Jenkins Jobs for PHP Projects
  Most web applications are changed and adapted quite frequently and quickly. Their environment, for example the size and the behaviour of the user base, are constantly changing. What was sufficient yesterday can be insufficient today. Especially in a web
environment it is important to monitor and continuously improve the internal quality not only when developing, but also when maintaining the software.
  Jenkins is the leading
open-source
continuous integration server. Thanks to its thriving plugin ecosystem, it supports building and testing virtually any project.
  The goal of this project is to provide a standard template for Jenkins jobs for PHP projects.



Required Jenkins Plugins
  You need to install the following plugins for Jenkins:


  •   Checkstyle (for processing
    PHP_CodeSniffer logfiles in Checkstyle format)

  •   Clover PHP (for processing
    PHPUnit code coverage xml output)

  •   DRY (for processing
    phpcpd logfiles in PMD-CPD format)

  •   HTML Publisher (for publishing the
    PHPUnit code coverage report, for instance)

  •   JDepend (for processing
    PHP_Depend logfiles in JDepend format)

  •   Plot (for processing
    phploc CSV output)

  •   PMD (for processing
    PHPMD logfiles in PMD format)

  •   Violations (for processing various logfiles)

  •   xUnit (for processing
    PHPUnit logfiles in JUnit format)

  You can install these plugins using the web frontend at

http://localhost:8080/pluginManager/available
or using the Jenkins CLI:
wget http://localhost:8080/jnlpJars/jenkins-cli.jar
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin \
checkstyle cloverphp dry htmlpublisher jdepend plot pmd violations xunit
java -jar jenkins-cli.jar -s http://localhost:8080 safe-restart
  In the above, replace localhost:8080 with the hostname and port of your Jenkins installation.



Required PHP Tools
  The required PHP tools should be installed using the PEAR Installer, the backbone of the
PHP Extension and Application Repository that provides a distribution system for PHP packages.
  Depending on your OS distribution and/or your PHP environment, you may need to install PEAR or update your existing PEAR installation before you can proceed with the following instructions.
sudo pear upgrade PEAR usually suffices to upgrade an existing PEAR installation. The
PEAR Manual explains how to perform a fresh installation of PEAR.
  The following two commands (which you may have to run as root) are all that is required to install the required PHP tools using the PEAR Installer:

pear config-set auto_discover 1
pear install pear.phpqatools.org/phpqatools pear.netpirates.net/phpDox




Build Automation
  The Apache Ant build.xml (download) build script orchestrates the execution of the various tools. It assumes that the rule sets for PHP_CodeSniffer
and PHPMD are located at build/phpcs.xml and build/phpmd.xml.

<?xml version="1.0" encoding="UTF-8"?>
<project name="name-of-project" default="build">
<target name="build"
depends="prepare,lint,phploc,pdepend,phpmd-ci,phpcs-ci,phpcpd,phpdox,phpunit,phpcb"/>
<target name="build-parallel"
depends="prepare,lint,tools-parallel,phpunit,phpcb"/>
<target name="tools-parallel" description="Run tools in parallel">
<parallel threadCount="2">
<sequential>
<antcall target="pdepend"/>
<antcall target="phpmd-ci"/>
</sequential>
<antcall target="phpcpd"/>
<antcall target="phpcs-ci"/>
<antcall target="phploc"/>
<antcall target="phpdox"/>
</parallel>
</target>
<target name="clean" description="Cleanup build artifacts">
<delete dir="${basedir}/build/api"/>
<delete dir="${basedir}/build/code-browser"/>
<delete dir="${basedir}/build/coverage"/>
<delete dir="${basedir}/build/logs"/>
<delete dir="${basedir}/build/pdepend"/>
</target>
<target name="prepare" depends="clean" description="Prepare for build">
<mkdir dir="${basedir}/build/api"/>
<mkdir dir="${basedir}/build/code-browser"/>
<mkdir dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/pdepend"/>
<mkdir dir="${basedir}/build/phpdox"/>
</target>
<target name="lint" description="Perform syntax check of sourcecode files">
<apply executable="php" failonerror="true">
<arg value="-l" />
<fileset dir="${basedir}/src">
<include name="**/*.php" />
<modified />
</fileset>
<fileset dir="${basedir}/tests">
<include name="**/*.php" />
<modified />
</fileset>
</apply>
</target>
<target name="phploc" description="Measure project size using PHPLOC">
<exec executable="phploc">
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="pdepend" description="Calculate software metrics using PHP_Depend">
<exec executable="pdepend">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="phpmd"
description="Perform project mess detection using PHPMD and print human readable output. Intended for usage on the command line before committing.">
<exec executable="phpmd">
<arg path="${basedir}/src" />
<arg value="text" />
<arg value="${basedir}/build/phpmd.xml" />
</exec>
</target>
<target name="phpmd-ci" description="Perform project mess detection using PHPMD creating a log file for the continuous integration server">
<exec executable="phpmd">
<arg path="${basedir}/src" />
<arg value="xml" />
<arg value="${basedir}/build/phpmd.xml" />
<arg value="--reportfile" />
<arg value="${basedir}/build/logs/pmd.xml" />
</exec>
</target>
<target name="phpcs"
description="Find coding standard violations using PHP_CodeSniffer and print human readable output. Intended for usage on the command line before committing.">
<exec executable="phpcs">
<arg value="--standard=${basedir}/build/phpcs.xml" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="phpcs-ci" description="Find coding standard violations using PHP_CodeSniffer creating a log file for the continuous integration server">
<exec executable="phpcs" output="/dev/null">
<arg value="--report=checkstyle" />
<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
<arg value="--standard=${basedir}/build/phpcs.xml" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--log-pmd" />
<arg value="${basedir}/build/logs/pmd-cpd.xml" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="phpdox" description="Generate API documentation using phpDox">
<exec executable="phpdox"/>
</target>
<target name="phpunit" description="Run unit tests with PHPUnit">
<exec executable="phpunit" failonerror="true"/>
</target>
<target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser">
<exec executable="phpcb">
<arg value="--log" />
<arg path="${basedir}/build/logs" />
<arg value="--source" />
<arg path="${basedir}/src" />
<arg value="--output" />
<arg path="${basedir}/build/code-browser" />
</exec>
</target>
</project>
  Here is an overview of the tasks defined in the above build.xml (download) script that are intended to be directly invoked:


  •   build is the default build target. It invokes all the tools in such a way that XML logfiles are written to default locations.

  •   build-parallel is the same as build (see above) except that it executes the tools in parallel.

  •   clean can be used to clean up (delete) all build artifacts (logfiles, etc. that are produced during the build).

  •   lint can be used to perform a syntax check of the project sources using
    php -l.

  •   phpdox can be used to generate API documentation.

  •   phpcs can be used to find coding standard violations and print human readable output. This task is intended for usage on the command line before committing.

  •   phploc can be used to measure the project size.

  •   phpmd can be used to perform project mess detection and print human readable output. This task is intended for usage on the command line before committing.

  •   phpunit can be used to run unit tests.

  The other tasks can, of course, also be invoked directly but that is not their intended purpose. They are invoked by the tasks listed above.





PHPUnit
  The phpunit task in the build.xml (download) above assumes that an XML configuration file for PHPUnit is used to configure the following logging targets:

<logging>
<log type="coverage-html" target="build/coverage" title="Name of Project"
charset="UTF-8" yui="true" highlight="true"
lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="junit" target="build/logs/junit.xml"
logIncompleteSkipped="false"/>
</logging>
  You can download a sample
phpunit.xml.dist and place it in your project root to get started.
  More information can be found in the
documentation for PHPUnit.



phpDox
  The phpdox task in the build.xml (download) above assumes that an XML configuration file for phpDox is used to configure the API documentation generation:

<phpdox xmlns="http://phpdox.de/config">
<project name="name-of-project" source="src" workdir="build/phpdox">
<collector publiconly="false">
<include mask="*.php" />
<exclude mask="*Autoload.php" />
</collector>
<generator output="build">
<build engine="html" enabled="true" output="api"/>
</generator>
</project>
</phpdox>




PHP_CodeSniffer
  The phpcs and phpcs-ci tasks in the build.xml (download) above assume that an XML configuration file for PHP_CodeSniffer is used to configure the coding standard:

<ruleset name="name-of-your-coding-standard">
<description>Description of your coding standard</description>
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<!-- ... -->
</ruleset>
  More information can be found in the
documentation for PHP_CodeSniffer.



PHPMD
  The phpmd and phpmd-ci tasks in the build.xml (download) above assume that an XML configuration file for PHPMD is used to configure the coding standard:

<ruleset name="name-of-your-coding-standard"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Description of your coding standard</description>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity" />
<!-- ... -->
</ruleset>
  More information can be found in the
documentation for PHPMD.





Build Artifacts
  Executing the build.xml (download) script above will produce the following
build directory:

build
|-- api ...
|-- code-browser ...
|-- coverage ...
`-- logs
|-- checkstyle.xml
|-- clover.xml
|-- jdepend.xml
|-- junit.xml
|-- phploc.csv
|-- pmd-cpd.xml
`-- pmd.xml
  These build artifacts will be processed by Jenkins.





Using the Job Template


  •   Fetch the jenkins-cli.jar from your jenkins server:

    wget http://localhost:8080/jnlpJars/jenkins-cli.jar
  •   Download and install the job template:

    curl https://raw.github.com/sebastianbergmann/php-jenkins-template/master/config.xml | \
    java -jar jenkins-cli.jar -s http://localhost:8080/jenkins create-job php-template
      or add the template manually:

    cd $JENKINS_HOME/jobs
    mkdir php-template
    cd php-template
    wget https://raw.github.com/sebastianbergmann/php-jenkins-template/master/config.xml
    cd ..
    chown -R jenkins:jenkins php-template/
  •   Reload Jenkins' configuration, for instance using the Jenkins CLI:

    java -jar jenkins-cli.jar -s http://localhost:8080 reload-configuration
  •   Click on "New Job".

  •   Enter a "Job name".

  •   Select "Copy existing job" and enter "php-template" into the "Copy from" field.

  •   Click "OK".

  •   Disable the "Disable Build" option.

  •   Fill in your "Source Code Management" information.

  •   Configure a "Build Trigger", for instance "Poll SCM".

  •   Click "Save".






Troubleshooting

No "Copy existing job" option / "php-template" project does not show up
  Jenkins cannot find the php-template job. Make sure you cloned to the right directory. Check the permissions to make sure the Jenkins user has read and write access to the directory. Restart Jenkins.

General Setup Issues
  Check the management panel http://localhost:8080/manage and the
Jenkins log for messages.





Further Reading
  Integrate Your PHP Project with Jenkins is a book that explains how you can leverage Jenkins to monitor the various aspects of software quality in a PHP software project.
  The planning, execution, and automation of tests for the different layers and tiers of a PHP-based web application is also outside the scope of the aforementioned book.
Real-World Solutions for Developing High-Quality PHP Frameworks and Applications is an excellent resource on these matters.



Support
  Issues are tracked on GitHub.
  The #jenkins-php channel on the
FreeNode IRC network can be used to discuss all issues related to Jenkins and PHP.
  thePHP.cc offers consulting and training that set you on a path to create, maintain and extend sustainable software of high quality with PHP and leverage Jenkins to monitor the
various aspects of software quality.





  The "Template for Jenkins Jobs for PHP Projects" by
Sebastian Bergmann and his contributors is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359033-1-1.html 上篇帖子: Jenkins持续集成——参数化构建 下篇帖子: Jenkins Configuration(3)Shell Script
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表