Jenkins Groovy script to Get Current Build User Info and Change Build Job Display Name

Veeresh Kumar
2 min readOct 30, 2021

This article will help you to Change the Build Job Display name to custom name and fetches the current running build job Username and User Id.

About:

There are two functions in the script:

  • newBuildDisplayName

It takes CUSTOM_DISPLAY_NAME String as input and changes the Current Build Job display name to it.

Syntax: newBuildDisplayName(<string>)

  • getBuildUserInfo

It returns the Username and User Id of user running the Current Jenkins Job.

Syntax: getBuildUserInfo()

Follow the below steps to run and see the functionality:

  • Copy and Paste the below script to new pipeline job in Jenkins.
  • Next, run the job by clicking Build Now, and see the job console output:
  1. Shows the changed build Display Name. Here, have passed Display Name as “Build#$BUILD_NUMBER” input. If you notice in the script line number 14.
  2. Shows the Current Build job Username.
  3. Shows the Current Build job User Id.

You can store the 2 &3 outputs to any variables and use it for any other purpose.

Note: During first few runs the job might fail, due to Script being blocked by Jenkins. Please make sure to go and approve them from the link provided the in the job output or In-Script approval option in Manage Jenkins.

The above script needs below methods to be approved.

method hudson.model.Cause$UserIdCause getUserId
method hudson.model.Cause$UserIdCause getUserName
method hudson.model.Run getCause java.lang.Class
method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild

Hope it helps !! Happy Scripting !! Thank You !!

--

--

Veeresh Kumar

He is a Sr. DevOps Engineer loves to automate cloud infrastructure and App deployments. #Terraform #Azure #Jenkins #Ansible #Chef #Bash #PowerShell #AWS #GCP