Skip to content

Instantly share code, notes, and snippets.

@aurimasbachanovicius
Last active October 19, 2018 07:13
Show Gist options
  • Save aurimasbachanovicius/289f18e45b02a8aaae7dfae4859c85cd to your computer and use it in GitHub Desktop.
Save aurimasbachanovicius/289f18e45b02a8aaae7dfae4859c85cd to your computer and use it in GitHub Desktop.
Jenkins Pipeline Groovy function to execute a command with changed files as argument. Can be used for linters which must check only changed files between stages.
@NonCPS
def executeCommandWithFiles(command) {
def changeLogSets = currentBuild.changeSets
def filesString = ""
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
filesString += " ${file.path}"
}
}
}
def cmd = "${command} ${filesString}"
sh "${cmd}"
}
pipeline {
stages {
stage('Code Quality') {
steps {
"PHP Code Sniffer" : {
ansiColor('xterm') {
executeCommandWithFiles("---script for code sniffer which accepts arguments as files---")
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment