Skip to content

Instantly share code, notes, and snippets.

@jan-koch
Created October 9, 2019 05:39
Show Gist options
  • Save jan-koch/b041ab21fe0df54f74db9600352691c9 to your computer and use it in GitHub Desktop.
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.
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