Goutham Nithyananda Goutham Nithyananda - 1 month ago 35
Groovy Question

Getting current timestamp in inline pipeline script using pipeline plugin of hudson

I want to get Getting current timestamp in inline pipeline script using pipeline plugin of hudson. For setting up build display name.

Inline groovy script used:

def jobName = env.JOB_NAME + "_" + new Date()
currentBuild.displayName = "$jobName"
node {
echo "job name $jobName"
}


Error on console :

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
Scripts not permitted to use new java.util.Date

Answer

Jenkins scripts are running in a sandbox, by default the Groovy script doesn't have permissions for some operations.

When you perform an operation without permissions the RejectAccessException is thrown. So you've to execute your script, and then when the exception is thrown go to:

http://yourHost/jenkins/scriptApproval/

And approve the necessary permission:

enter image description here