-
-
Save mikepsinn/0736e2d73fd5a5f3d8bc558a9c3dccb3 to your computer and use it in GitHub Desktop.
Jenkinsfile for php static analysis using docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
properties([ | |
pipelineTriggers([ | |
triggers: [ | |
[ | |
$class: 'com.cloudbees.jenkins.plugins.BitBucketTrigger', | |
] | |
] | |
]), | |
]) | |
node('master') { | |
try { | |
stage('build') { | |
# worked with bitbucket, for build on push. Github might be easier. | |
#git url: '[email protected]:st0rmis' | |
} | |
stage('Pulling') { | |
sh 'docker pull jakzal/phpqa' | |
sh 'docker pull phpdoc/phpdoc' | |
} | |
stage('Prepare directory') { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa chmod -R 777 build' | |
sh 'rm -rf build/logs' | |
sh 'mkdir -p build/logs' | |
sh 'rm -rf build/docs' | |
sh 'mkdir -p build/docs' | |
sh 'rm -rf build/phpmetrics' | |
sh 'mkdir -p build/phpmetrics' | |
sh 'rm -rf build/code-browser' | |
sh 'mkdir -p build/code-browser' | |
} | |
stage('Install dependencies') { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa composer install --ignore-platform-reqs --no-scripts --no-progress --no-suggest' | |
} | |
stage("Testing") { | |
parallel ( | |
"PHPCodeSniffer": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpcs --report=checkstyle --report-file=build/logs/checkstyle.xml --standard=PSR2 --encoding=UTF-8 --ignore="*.js" app/ || exit 0' | |
replaceFilePath('build/logs/checkstyle.xml') | |
recordIssues enabledForFailure: true, aggregatingResults: true, tool: checkStyle(pattern: 'build/logs/checkstyle.xml') | |
}, | |
"PHPStan": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpstan analyse --level 3 app || exit 0' | |
}, | |
"PhpMetrics": { | |
/* sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpmetrics --report-html=build/logs/phpmetrics.html ./ || exit 0' */ | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpmetrics --report-xml=build/phpmetrics/phpmetrics.xml --violations-xml=build/phpmetrics/violations.xml --report-html=build/phpmetrics/quality.html ./ || exit 0' | |
publishHTMLReport('build/phpmetrics/quality.html/', 'index.html', 'PHPMetrics') | |
}, | |
"PHPMessDetector": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpmd app/ xml cleancode,codesize,unusedcode --reportfile build/logs/pmd.xml || exit 0' | |
replaceFilePath('build/logs/pmd.xml') | |
recordIssues enabledForFailure: true, aggregatingResults: true, tool: pmdParser(pattern: 'build/logs/pmd.xml') | |
}, | |
"PHPMagicNumberDetector": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpmnd app/ --xml-output=build/logs/phpmnd.xml --exclude=tests --progress --non-zero-exit-on-violation --ignore-strings=return,condition,switch_case,default_parameter,operation || exit 0' | |
}, | |
"PHPCopyPasteDetector": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpcpd --log-pmd build/logs/pmd-cpd.xml app/ || exit 0' | |
replaceFilePath('build/logs/pmd-cpd.xml') | |
recordIssues enabledForFailure: true, aggregatingResults: true, tool: cpd(pattern: 'build/logs/pmd-cpd.xml') | |
}, | |
"PHPCoverageTest": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpdbg -qrr ./vendor/bin/phpunit --coverage-text' | |
}, | |
# Not sure if works as intended. Need to test with properly documented project | |
"PHPDocumentor": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app phpdoc/phpdoc -d ./app -t ./build/docs --ignore "vendor/,tests/" --template="responsive-twig" --template="checkstyle" || exit 0' | |
publishHTMLReport('build/docs', 'index.html', "Documentation") | |
}, | |
"PHP-CB": { | |
sh 'docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app jakzal/phpqa phpcb --log ./build/docs --output build/code-browser --source ./app' | |
publishHTMLReport('build/code-browser', 'index.html', "CB") | |
} | |
) | |
} | |
} catch (error) { | |
throw error | |
} finally { | |
//cleanup | |
} | |
} | |
def replaceFilePath(filePath) { | |
sh "sed -i 's#/usr/src/app/#${workspace}/#g' ${filePath}" | |
} | |
def publishHTMLReport(reportDir, file, reportName) { | |
if (fileExists("${reportDir}/${file}")) { | |
publishHTML(target: [ | |
allowMissing : true, | |
alwaysLinkToLastBuild: true, | |
keepAll : true, | |
reportDir : reportDir, | |
reportFiles : file, | |
reportName : reportName | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment