Created
April 18, 2016 09:25
-
-
Save jaysoncena/4b18951c3c5e68a9049ad340878761e9 to your computer and use it in GitHub Desktop.
Jenkins - Get the user who triggered the current or the parent job
This file contains hidden or 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
def triggeredBy = "---" | |
def iterateCause(b) { | |
upstreamcause = b.getCause(hudson.model.Cause.UpstreamCause.class) | |
if (upstreamcause != null) { | |
job = Jenkins.getInstance().getItemByFullName(upstreamcause.getUpstreamProject(), hudson.model.Job.class) | |
if (job != null) { | |
upstream = job.getBuildByNumber(upstreamcause.getUpstreamBuild()) | |
if (upstream != null) { | |
iterateBuild(upstream) | |
} | |
} | |
} | |
usercause = b.getCause(hudson.model.Cause.UserIdCause.class) | |
if (usercause != null) { | |
triggeredBy = usercause.getUserName() | |
} | |
return; | |
} | |
iterateCause(build) |
thank's !
thanks!
if you just need the build-number or just the upstream project name, you can use something like.
def upstream_project = "${currentBuild.getBuildCauses()[0].upstreamProject}"
echo "Build Caused by ${upstream_project}"
I wasn't authorized to use the rawBuild
method, so I did it like this:
pipeline {
agent any
stages {
stage('Print user') {
steps {
script {
if(currentBuild.upstreamBuilds){
print("Parent")
print(currentBuild.upstreamBuilds[0].getBuildCauses()[0].userId)
} else {
print("No parent")
print(currentBuild.getBuildCauses()[0].userId)
}
}
}
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for a snippet, it put me in a right way with upstream causes.
In 2020 and Jenkins 2.235.2 i've done it working with this: