How to get username logged in Jenkins from Jenkins Pipeline Plugin?
August 3, 2023 2023-08-15 5:22How to get username logged in Jenkins from Jenkins Pipeline Plugin?
You can access the current user’s username who triggered in the pipeline build using “BuildUser” wrapper in Jenkins.
Lets see how to retrieve the username of the user who triggered the build:
node {
wrap([$class: 'BuildUser']) {
def user = env.BUILD_USER_ID
echo "Username who triggered the build: ${user}"
}
}
In this script, the wrap([$class: ‘BuildUser’]) block ensures that the user information is available within the enclosed block. The env.BUILD_USER_ID variable holds the username of the user who triggered the build. Finally, the echo step is used to print out the retrieved username.
Make sure that the Build User Vars Plugin is installed and properly configured in your Jenkins instance for this approach to work correctly.