Skip to content

Instantly share code, notes, and snippets.

@kibotu
Created July 29, 2024 15:19
Show Gist options
  • Save kibotu/1b192f57f8437315f081228dbb1685cb to your computer and use it in GitHub Desktop.
Save kibotu/1b192f57f8437315f081228dbb1685cb to your computer and use it in GitHub Desktop.
Jenkins CI user that triggered build
// 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