Get username logged in Jenkins from Jenkins Workflow (Pipeline) Plugin Get username logged in Jenkins from Jenkins Workflow (Pipeline) Plugin jenkins jenkins

Get username logged in Jenkins from Jenkins Workflow (Pipeline) Plugin


Did you try installing the Build User Vars plugin? If so, you should be able to run

node {  wrap([$class: 'BuildUser']) {    def user = env.BUILD_USER_ID  }}

or similar.


To make it work with Jenkins Pipeline:

Install user build vars plugin

Then run the following:

pipeline {  agent any  stages {    stage('build user') {      steps {        wrap([$class: 'BuildUser']) {          sh 'echo "${BUILD_USER}"'        }      }    }  }}


Here's a slightly shorter version that doesn't require the use of environment variables:

@NonCPSdef getBuildUser() {    return currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()}

The use of rawBuild requires that it be in a @NonCPS block.