Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically approve Jenkins System Groovy Script

Tags:

jenkins

groovy

Jenkins Job DSL introduced Script Security in 1.6, this gives some builds the following error:

ERROR: Build step failed with exception
org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException: script not yet approved for use

We run Jenkins masters entirely through configuration using plugins.txt and a set of Groovy scripts to configure Jenkins, and do not allow any configuration to be done through the UI, so we also disable the ability to login as an admin.

How can we programmatically pre-approve scripts through the Jenkins groovy configuration?

like image 243
Highway of Life Avatar asked Jan 27 '23 21:01

Highway of Life


1 Answers

Below is for approving two method with groovy on jenkins.

def scriptApproval = org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval.get()

String[] signs = [
    "method org.jenkinsci.plugins.workflow.steps.FlowInterruptedException getCauses",
    "method org.jenkinsci.plugins.workflow.support.steps.input.Rejection getUser"
    ]

for( String sign : signs ) {
    scriptApproval.approveSignature(sign)
}

scriptApproval.save()
like image 80
yun abs Avatar answered Feb 02 '23 00:02

yun abs