Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env groovy
def runBuild(String nodeName = null, notifyGithub = true, Closure<Void> job) {
if (nodeName) node(nodeName) { runBuildInternally(job, notifyGithub) }
else runBuildInternally(job, notifyGithub)
}
private def runBuildInternally(Closure<Void> job, boolean notifyGithub) {
withGithubNotifier(notifyGithub) {
decorateBuild {
currentBuild.result = 'SUCCESS'
catchError {
job()
}
}
}
}
def decorateBuild(Closure<Void> job) {
wrap([$class: 'TimestamperBuildWrapper']) {
ansiColor('XTerm') {
job()
}
}
}
def withGithubNotifier(boolean enabled, Closure<Void> job) {
if (enabled) notifyGithub('STARTED')
job()
if (enabled) notifyGithub(currentBuild.result)
}
def notifyGithub(String result) {
switch (result) {
case 'STARTED':
setGitHubPullRequestStatus(context: env.JOB_NAME, message: "Build started", state: 'PENDING')
break
case 'FAILURE':
setGitHubPullRequestStatus(context: env.JOB_NAME, message: "Build error", state: 'ERROR')
break
case 'UNSTABLE':
setGitHubPullRequestStatus(context: env.JOB_NAME, message: "Tests are broken", state: 'FAILURE')
break
case 'SUCCESS':
setGitHubPullRequestStatus(context: env.JOB_NAME, message: "All is good", state: 'SUCCESS')
break
}
}
@NonCPS
def String getScmChanges(truncateLength = 140) {
def changeString = ""
echo "Gathering SCM changes"
def changeLogSets = currentBuild.rawBuild.changeSets
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]
truncatedMsg = entry.msg.take(truncateLength)
changeString += " - ${truncatedMsg} [${entry.author}]\n"
}
}
return changeString ?: " - No new changes"
}
def runTestAndArchiveResult(String gradleTestTask, String reportFolder, String reportPattern, boolean allowEmpty=false) {
Throwable error = null
try {
gradlew gradleTestTask
} catch (e) {
error = e
}
if (!fileExists(reportFolder)) throw error
junit allowEmptyResults: allowEmpty, testResults: "${reportFolder}/${reportPattern}"
}
def gradlew(String task) {
bash "./gradlew clean ${task} -PdisablePreDex "
}
def bash(String command) {
println "Bash shell executes -> $command"
result = sh(returnStdout: true, script: """#!/bin/bash -l
$command
""")
if (result) return result.trim()
}
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment