Created
October 9, 2019 05:39
-
-
Save jan-koch/b041ab21fe0df54f74db9600352691c9 to your computer and use it in GitHub Desktop.
An example Jenkinsfile that stops deployment if your method complexity is above 20, based on the static code analysis of PHPloc.
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
pipeline { | |
agent any | |
tools { | |
ant 'ant' | |
} | |
// Pull the repo first. | |
stages { | |
stage( 'Checkout Repo' ) { | |
steps { | |
checkout scm | |
} | |
} | |
stage( 'Build' ) { | |
steps { | |
script { | |
// Define a function to count all comment lines | |
linesOfComments = sh(returnStdout: true, | |
script: "vendor/bin/phploc . --exclude vendor | grep CLOC").trim() | |
// Measure method complexity | |
complexityPerMethod = sh(returnStdout: true, | |
script: "vendor/bin/phploc . --exclude vendor | \ | |
grep \'Maximum Method Complexity\'| \ | |
tr -dc '0-9'").trim() | |
Let build fail if complexity is above a certain threshold | |
if ( complexityPerMethod.toInteger() > 2000 ) { | |
error( "Build ${env.BUILD_NUMBER} failed because methods are too complex. ${env.BUILD_URL} - ${env.JOB_NAME}") | |
} | |
} | |
withAnt(installation: 'ant', jdk: 'jdk') { | |
sh "ant phploc" | |
} | |
echo "Lines of Comments: ${linesOfComments}" | |
echo "Maximum complexity in method: ${complexityPerMethod}" | |
} | |
} | |
stage( 'Deploy' ) { | |
steps { | |
// This is were Ant should run if I'm not mistaken | |
// Run git status just to log anything outstanding. | |
sh 'git status' | |
script{ | |
switch( env.BRANCH_NAME ) { | |
case "staging": | |
// Comment out the next line if the target directory | |
// does not exist on the server. | |
sh 'vendor/bin/phploy -s staging --list' | |
sh 'vendor/bin/phploy -s staging --force' | |
break | |
default: | |
// Doing nothing | |
break | |
} | |
} | |
} | |
} | |
} | |
// Run items after pipeline completion/failure | |
post { | |
always { | |
// Always clearn up the directory, regardless. | |
deleteDir() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment