Pipeline: How to access Jenkins build information from pipeline?

2,678 views
Skip to first unread message

Lars Meynberg

unread,
Aug 22, 2016, 11:21:11 AM8/22/16
to Jenkins Users
Hi guys,

I'd like to access some information about the job and its builds. I've found this example from the cloudbees repo:

But when I try to access Jenkins.instance I get the error 
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance

Any thoughts if this is a Pipeline-Bug or the wrong method to get build information during a pipeline run?



jer...@bodycad.com

unread,
Aug 22, 2016, 1:11:09 PM8/22/16
to Jenkins Users
Hi,
You may want to take a look at the global env variables for the pipeline on your own Jenkins server:
http://JenkinsHost/job/MyJobName/pipeline-syntax/globals

replace the bold part with yours. You may find many things already defined into environment. To access them into groovy script just use the variable directly or using double quote into string:

def myVar = env.BRANCH_NAME
def myVar = "${env.BRANCH_NAME}"

As for the error you are getting, it's the sandbox policies that block the execution for "security" reason. 
Either use case:
1- You use the direct pipeline script: You can disable it into the project pipeline config page by unchecking the Use Groovy Sandbox
2- You use scm to fetch the jenkinsfile: You need to go into the Manage Jenkins and then click on In-process Script Approval and approve the wanted function until the script pass for every script possible branches.

Jerome

Lars Meynberg

unread,
Aug 23, 2016, 7:53:05 AM8/23/16
to Jenkins Users
Thanks, that was the hint in the right direction! 
Reply all
Reply to author
Forward
0 new messages