Issue
I have the following in a Jenkins file where I am trying to get some text from an XML file:
pipeline {
agent any
stages {
stage('Start') {
steps {
echo 'Build start'
script {
def xml = readFile "${env.WORKSPACE}/config.xml"
def xmlContents = new XmlParser().parseText(xml)
def text = xmlContents.text()
echo 'contents are...'
echo text
}
}
}
.....
This causes the following exception
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.util.XmlParser parseText java.lang.String
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:262)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:161)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:156)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:160)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:9)
According to a solution to this problem as posted here, I need to go in and set the In-process Script Approval
, however I have done this a couple of times..
Also, elsewhere, I have seen comments to disable sandbox mode, but I do not have this option as shown in other screen shots...
Perhaps things have changed.
So, my question is, how can I read the contents of an XML file if can't use the XMLParser? Or how can I use the XMLParser?
Solution
I have since found that you just need to keep approving each individual method (including constructors), one at a time. Ie, you may approve one, and then you get the next error.
I did not notice that they were subtley different approvals required.
Answered By - peterc
Answer Checked By - Katrina (JavaFixing Volunteer)