Last active
January 21, 2020 20:30
-
-
Save Enrico2/80b5f381354c8fe78677e67bb170d102 to your computer and use it in GitHub Desktop.
Gradle (w/ groovy) git diff log for kubernetes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ext.gitDiffLog = { String stack, String serviceName, String k8sCluster -> | |
new ByteArrayOutputStream().with { out -> | |
exec { | |
commandLine = envwrapper + ['git', 'describe', '--abbrev=10'] | |
standardOutput = out | |
} | |
def toGitRev = out.toString().trim() | |
new ByteArrayOutputStream().with { out2 -> | |
exec { | |
commandLine = envwrapper + ['kubectl', 'get', 'po', '-ojson', | |
'--namespace', stack.toLowerCase(), '--context', k8sCluster, '-l', "name=$serviceName"] | |
standardOutput = out2 | |
} | |
def raw = out2.toString().trim() | |
def fromGitRev = new JsonSlurper().parseText(raw).items.find { | |
it?.metadata?.annotations?.monorepo != null | |
}.metadata.annotations.monorepo | |
def originalGitRev = fromGitRev | |
def isCommit = true | |
new ByteArrayOutputStream().with { out3 -> | |
// Test if the commit in remote is one that we actual recognize. | |
// If it is not log is going to spew out all history | |
exec { | |
commandLine = envwrapper + ['git', "cat-file", "-t", fromGitRev] | |
standardOutput = out3 | |
ignoreExitValue true | |
} | |
isCommit = out3.toString().trim().equals("commit") | |
if (!isCommit) { | |
fromGitRev = "HEAD~20" | |
} | |
} | |
// Find commits we are adding to deployment | |
def plusCommits = "" | |
new ByteArrayOutputStream().with { out3 -> | |
exec { | |
commandLine = envwrapper + ['git', 'log', "$fromGitRev..$toGitRev", "--date=short", "--pretty=%h | %ad | %an | %s"] | |
standardOutput = out3 | |
} | |
def outString = out3.toString().trim() | |
outString.eachLine { | |
plusCommits += "+ " + it + "\n" | |
} | |
} | |
// Find commits that were on the remote deployment but are not going to be anymore. | |
def minusCommits = "" | |
new ByteArrayOutputStream().with { out3 -> | |
exec { | |
commandLine = envwrapper + ['git', 'log', "$toGitRev..$fromGitRev", "--date=short", "--pretty=%h | %ad | %an | %s"] | |
standardOutput = out3 | |
} | |
def outString = out3.toString().trim() | |
outString.eachLine { | |
minusCommits += "- " + it + "\n" | |
} | |
} | |
def silent_git = (System.getenv("SILENT_GIT") ?: "false").toBoolean() | |
// If the commits exist on different branches then we will get different values for each going from the parent node. | |
// The plus commits are commits that are being added to the deployment, and the minus commits are commits that will no longer be present | |
def logResult = plusCommits + minusCommits | |
if (!isCommit) { | |
logResult = "Original Commit is bad with Hash: $originalGitRev. Instead showing past 20 commits \n" + logResult | |
} | |
if (!silent_git) { | |
println("Git log for $fromGitRev..$toGitRev: \n$logResult") | |
} | |
return logResult | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this line:
We annotate all of our pods with
monorepo
that contains the git sha.