Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Skip CI based on changes in PR (#59939) #60018

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -185,17 +185,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