Skip to content

Commit

Permalink
Skip CI based on changes in PR (elastic#59939)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianseeders committed Mar 12, 2020
1 parent a963f79 commit 8f9f022
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
library 'kibana-pipeline-library'
kibanaLibrary.load()

kibanaPipeline(timeoutMinutes: 135) {
kibanaPipeline(timeoutMinutes: 135, checkPrChanges: true) {
githubPr.withDefaultPrComments {
catchError {
retryable.enable()
Expand Down
8 changes: 0 additions & 8 deletions vars/githubPr.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,6 @@ def getNextCommentMessage(previousCommentInfo = [:]) {
.join("\n\n")
}

def withGithubCredentials(closure) {
withCredentials([
string(credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7', variable: 'GITHUB_TOKEN'),
]) {
closure()
}
}

def postComment(message) {
if (!isPr()) {
error "Trying to post a GitHub PR comment on a non-PR or non-elastic PR build"
Expand Down
11 changes: 10 additions & 1 deletion vars/kibanaPipeline.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,26 @@ def runErrorReporter() {
}

def call(Map params = [:], Closure closure) {
def config = [timeoutMinutes: 135] + params
def config = [timeoutMinutes: 135, checkPrChanges: false] + params

stage("Kibana Pipeline") {
timeout(time: config.timeoutMinutes, unit: 'MINUTES') {
timestamps {
ansiColor('xterm') {
if (config.checkPrChanges && githubPr.isPr()) {
print "Checking PR for changes to determine if CI needs to be run..."

if (prChanges.areChangesSkippable()) {
print "No changes requiring CI found in PR, skipping."
return
}
}
closure()
}
}
}
}
}


return this
52 changes: 52 additions & 0 deletions vars/prChanges.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

def getSkippablePaths() {
return [
/^docs\//,
/^rfcs\//,
/^.ci\/.+\.yml$/,
/^\.github\//,
/\.md$/,
]
}

def areChangesSkippable() {
if (!githubPr.isPr()) {
return false
}

try {
def skippablePaths = getSkippablePaths()
def files = getChangedFiles()

// 3000 is the max files GH API will return
if (files.size() >= 3000) {
return false
}

files = files.findAll { file ->
return !skippablePaths.find { regex -> file =~ regex}
}

return files.size() < 1
} catch (ex) {
buildUtils.printStacktrace(ex)
print "Error while checking to see if CI is skippable based on changes. Will run CI."
return false
}
}

def getChanges() {
withGithubCredentials {
return githubPrs.getChanges(env.ghprbPullId)
}
}

def getChangedFiles() {
def changes = getChanges()
def changedFiles = changes.collect { it.filename }
def renamedFiles = changes.collect { it.previousFilename }.findAll { it }

return changedFiles + renamedFiles
}

return this
9 changes: 9 additions & 0 deletions vars/withGithubCredentials.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def call(closure) {
withCredentials([
string(credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7', variable: 'GITHUB_TOKEN'),
]) {
closure()
}
}

return this

0 comments on commit 8f9f022

Please sign in to comment.