Created
July 29, 2024 15:19
-
-
Save kibotu/1b192f57f8437315f081228dbb1685cb to your computer and use it in GitHub Desktop.
Jenkins CI user that triggered build
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
// This script should be placed inside your pipeline script | |
def getTriggeringUser() { | |
def user = 'UNKNOWN' // Default value if user cannot be determined | |
// Check each cause for the build to find a user cause | |
currentBuild.rawBuild.getCauses().each { cause -> | |
if (cause instanceof hudson.model.Cause$UserIdCause) { | |
user = cause.getUserId() | |
} else if (cause instanceof org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamCause) { | |
// If the build was triggered upstream, recursively check upstream builds | |
def upstreamBuild = Jenkins.instance.getItemByFullName(cause.getUpstreamProject()).getBuildByNumber(cause.getUpstreamBuild()) | |
def upstreamUser = upstreamBuild.rawBuild.getCauses().find { it instanceof hudson.model.Cause$UserIdCause } | |
if (upstreamUser) { | |
user = upstreamUser.userId | |
} | |
} | |
} | |
return user | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment