Ce Jenkinsfile est l’équivalant du fichier build.ant fournit par le site http://jenkins-php.org/ .

Mise en place

Création d’un répertoire “build” à la racine du projet

composer.json

...
    "require-dev": {
        ...
        "jakub-onderka/php-parallel-lint": "^0.9.2",
        "phpunit/phpunit": "^6.4",
        "squizlabs/php_codesniffer": "^3.1",
        "phploc/phploc": "^4.0",
        "sebastian/phpcpd": "^3.0",
        "phpmd/phpmd": "^2.6",
        "pdepend/pdepend": "^2.5",
        "theseer/phpdox": "^0.10.1"
    },
...

Jenkinsfile

pipeline {
    agent any
    environment {
        http_proxy = 'http://proxywebsrv.tech.sits.credit-agricole.fr:8080/'
        https_proxy = 'http://proxywebsrv.tech.sits.credit-agricole.fr:8080/'
    }
    stages {
        stage('Prepare') {
            steps {
                sh '''
composer install --no-ansi --no-progress --no-suggest --no-scripts
rm -rf build/api build/coverage build/logs build/pdepend build/phpdox
mkdir  build/api build/coverage build/logs build/pdepend build/phpdox
'''
            }
        }
        stage('PHP Syntax check') {
            steps {
                sh 'vendor/bin/parallel-lint --exclude vendor/ .'
            }
        }
        stage('Test'){
            steps {
                sh 'vendor/bin/phpunit -c build/phpunit.xml || exit 0'
                step([
                    $class: 'XUnitBuilder',
                    thresholds: [[$class: 'FailedThreshold', unstableThreshold: '1']],
                    tools: [[$class: 'JUnitType', pattern: 'build/logs/junit.xml']]
                ])
                publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'build/coverage', reportFiles: 'index.html', reportName: 'Coverage Report', reportTitles: ''])
            }
        }
        stage('Checkstyle') {
            steps {
                sh 'vendor/bin/phpcs --report=checkstyle --report-file=build/logs/checkstyle.xml --standard=PSR2 --extensions=php --ignore=autoload.php --ignore=vendor/ --ignore=tests/ src || exit 0'
                checkstyle pattern: 'build/logs/checkstyle.xml'
            }
        }
        stage('Lines of Code') {
            steps {
                sh 'vendor/bin/phploc --count-tests --exclude vendor/ --log-csv build/logs/phploc.csv --log-xml build/logs/phploc.xml src'
                script {
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d900.csv',
                        group: 'phploc',
                        title: 'A - Lines of code', useDescr: false,
                        yaxis: 'Lines of Code',
                        csvSeries: [[
                            exclusionValues: 'Lines of Code (LOC),Comment Lines of Code (CLOC),Non-Comment Lines of Code (NCLOC),Logical Lines of Code (LLOC)',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d901.csv',
                        group: 'phploc',
                        title: 'B - Structures Containers',
                        yaxis: 'Count',
                        csvSeries: [[
                            exclusionValues: 'Directories,Files,Namespaces',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d902.csv',
                        group: 'phploc',
                        title: 'C - Average Length',
                        yaxis: 'Average Lines of Code',
                        csvSeries: [[
                            exclusionValues: 'Average Class Length (LLOC),Average Method Length (LLOC),Average Function Length (LLOC)',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d903.csv',
                        group: 'phploc',
                        title: 'D - Relative Cyclomatic Complexity',
                        yaxis: 'Cyclomatic Complexity by Structure',
                        csvSeries: [[
                            exclusionValues: 'Cyclomatic Complexity / Lines of Code,Cyclomatic Complexity / Number of Methods',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d904.csv',
                        group: 'phploc',
                        title: 'E - Types of Classes',
                        yaxis: 'Count',
                        csvSeries: [[
                            exclusionValues: 'Classes,Abstract Classes,Concrete Classes',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d905.csv',
                        group: 'phploc',
                        title: 'F - Types of Methods',
                        yaxis: 'Count',
                        csvSeries: [[
                            exclusionValues: 'Methods,Non-Static Methods,Static Methods,Public Methods,Non-Public Methods',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d906.csv',
                        group: 'phploc',
                        title: 'G - Types of Constants',
                        yaxis: 'Count',
                        csvSeries: [[
                            exclusionValues: 'Constants,Global Constants,Class Constants',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d909.csv',
                        group: 'phploc',
                        title: 'H - Types of Functions',
                        yaxis: 'Count',
                        csvSeries: [[
                            exclusionValues: 'Functions,Named Functions,Anonymous Functions',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d907.csv',
                        group: 'phploc',
                        title: 'I - Testing',
                        yaxis: 'Count',
                        csvSeries: [[
                            exclusionValues: 'Test Classes,Test Methods',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d908.csv',
                        group: 'phploc',
                        title: 'AB - Code Structure by Logical Lines of Code',
                        yaxis: 'Logical Lines of Code',
                        csvSeries: [[
                            exclusionValues: 'Logical Lines of Code (LLOC),Classes Length (LLOC),Functions Length (LLOC),LLOC outside functions or classes',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                    plot csvFileName: 'plot-7cc376ec-b513-455f-84d2-97e68468d90a.csv',
                        group: 'phploc',
                        title: 'BB - Structure Objects',
                        yaxis: 'Count',
                        csvSeries: [[
                            exclusionValues: 'Interfaces,Traits,Classes,Methods,Functions,Constants',
                            file: 'build/logs/phploc.csv',
                            inclusionFlag: 'INCLUDE_BY_STRING', displayTableFlag: false, url: ''
                        ]],
                        numBuilds: '100', style: 'line', exclZero: false, keepRecords: false, logarithmic: false, yaxisMaximum: '', yaxisMinimum: ''
                }
            }
        }
        stage('Copy paste detection') {
            steps {
                sh 'vendor/bin/phpcpd --log-pmd build/logs/pmd-cpd.xml --exclude vendor src || exit 0'
                dry canRunOnFailed: true, pattern: 'build/logs/pmd-cpd.xml'
            }
        }
        stage('Mess detection') {
            steps {
                sh 'vendor/bin/phpmd src xml build/phpmd.xml --reportfile build/logs/pmd.xml --exclude vendor/ || exit 0'
                pmd canRunOnFailed: true, pattern: 'build/logs/pmd.xml'
            }
        }
        stage('Software metrics') {
            steps {
                sh 'vendor/bin/pdepend --jdepend-xml=build/logs/jdepend.xml --jdepend-chart=build/pdepend/dependencies.svg --overview-pyramid=build/pdepend/overview-pyramid.svg --ignore=vendor .'
            }
        }
        stage('Generate documentation') {
            steps {
                sh 'vendor/bin/phpdox -f build/phpdox.xml'
                publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'build/api', reportFiles: 'index.html', reportName: 'Technic documentation', reportTitles: ''])
            }
        }
    }
}

Fichiers de configuration (exemples de base) :

build/phpdox.xml

	

    
        
            
            
        
        
            
                
            
        
    

build/phpmd.xml



    
        My custom rule set that checks [Nom du projet]...
    
    
    
    
    
    
    

build/phpunit.xml



       
  
    
      ../tests/
    
  
  
    
    
    
    
  
  
    
      ../_libs