diff --git a/vars/buildCause.groovy b/vars/buildCause.groovy new file mode 100644 index 0000000..7360d7a --- /dev/null +++ b/vars/buildCause.groovy @@ -0,0 +1,51 @@ +// vars/buildCause.groovy + +/** + * Return the reason for the build + */ +Boolean call(Map config = [:]) { + + String buildUser = "Unknown" + if (currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause)) { + println "CAUSE ${currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause).properties}" + } else { + println "currentBuild.rawBuild.getCause(hudson.model.Cause\$UserIdCause) is null" + println "so, " + currentBuild.rawBuild.getCause(hudson.triggers.TimerTrigger$TimerTriggerCause) + println "and " + currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').isEmpty() + } + /* [userName:Brian J. Murrell, + userIdOrUnknown:bmurrell, + userId:bmurrell, + class:class hudson.model.Cause$UserIdCause, + userUrl:user/bmurrell, + shortDescription:Started by user Brian J. Murrell] + */ + def causes = currentBuild.getBuildCauses() + + // Get a specific Cause type (in this case the user who kicked off the build), + // if present. + def specificCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause') + println "specificCause: " + specificCause + + println currentBuild.getBuildCauses().shortDescription[0] + + + /* + def buildCauses = currentBuild.rawBuild.getCauses() + //echo buildCauses + echo buildCauses.getShortDescription() + echo buildCauses.getUserId() + echo buildCauses.getUserName() + echo buildCauses.getUserUrl() + echo buildCauses.hashCode() + if (buildCauses.contains("hudson.triggers.TimerTrigger")){ + buildUser = "TimerTrigger" + }*/ /* else { + wrap([$class: 'BuildUser']) { + buildUser = "${BUILD_USER}" + } + }*/ + echo "Initiated by: ${buildUser}" + + return !currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').isEmpty() +} diff --git a/vars/startedByTimer.groovy b/vars/startedByTimer.groovy new file mode 100644 index 0000000..1a93439 --- /dev/null +++ b/vars/startedByTimer.groovy @@ -0,0 +1,13 @@ +// vars/startedByTimer.groovy + +/** + * Return the reason for the build + * + * This needs to be here rather than in pipeline-lib or Jenkinsfile due to + * Scripts not permitted to use method net.sf.json.JSON isEmpty. + */ +Boolean call(Map config = [:]) { + + return !currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').isEmpty() + +}