I am trying to read the server name from the xml file in a pipeline script.
My Code:
node {
def str = "<root><HTTPTargetConnection><Loadbalancer><server name=\"myserver\" /> </Loadbalancer></HTTPTargetConnection></root>";
def rootNode = new XmlParser().parseText(str);
echo rootNode.HTTPTargetConnection.Loadbalancer.server.#name.value[0];
}
Exception:
[Pipeline] End of Pipeline
*
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
unclassified field groovy.util.Node HTTPTargetConnection at
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:367)
at
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:363)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
Please help me in resolving this issue.
You're running into sandbox issues. The field in question is not authorized for use and therefore must be approved (in the script approval page).
Related
I am into a scenario where I need to read the console output and find a specific string and set this as environment variable. This variable I would be using in input to run a different script in same job.
for example: my jenkins job's console would contain something like
build_id: 123456
Can somebody help in finding this number and pass it to input.environment variable to other script in same job?
I have looked into this answer but its not working, I am getting groovy errors while running it in post build groovy script.
Jenkins pipeline, is there a way to set environment variable from console output
Script I am using:
import jenkins.model.*
jenkins = Jenkins.instance
def consoleLog = Jenkins.getInstance().getItemByFullName(env.JOB_NAME).getBuildByNumber(Integer.parseInt(env.BUILD_NUMBER)).logFile.text
def buildId = (consoleLog =~ 'build_id="(.*)"')[0][1]
echo "build_id: $buildId"
env.build_id = buildId
Error I am getting:
ERROR: Failed to evaluate groovy script.
groovy.lang.MissingPropertyException: No such property: env for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
I have to read some input from report.json file which is in the jenkins workspace. I am using the following code to read the file but it says file not found error. I am running this script on Groovy Postbuild step.
import hudson.model.*
def fileContents = new File('C:\\jenkinstest_slave\\workspace\\Cypress\\mochawesome-report\\report.json').readLines()
def result = fileContents.findAll { it.contains('passPercent') }
manager.listener.logger.println("matching word from findAll method= " +result)
Error:
Groovy script failed:
java.io.FileNotFoundException
Can someone please help me to resolve this?
Unable to get output of Failing Jenkins jobs to pipeline's console
I'm calling several jobs from my pipeline. Below is an example job 'printuser' that gets triggered from my pipeline.
I'm able to get the output of the job on my pipeline console which is my requirement.
pipeline {
agent none
stages {
stage ("Check Parameters"){
steps {
echo "In pipeline"
script {
echo "Start condition check"
}
def slaveJob = build job: 'printuser'
println slaveJob.rawBuild.log
}
}
}
}
The issue happens when 'printuser' fails in which case the failing output does not show in the pipeline console page.
I tried adding the println to the post failure block like below:
post {
failure {
script {
println slaveJob.rawBuild.log
}
}
}
But, I get the below error in doing so.
Error when executing failure post condition:
groovy.lang.MissingPropertyException: No such property: slaveJob for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
Can you please suggest how can I get the output of "FAILING" jobs to the pipeline console ?
On your post block you can print the whole job build log.
post {
failure {
echo Jenkins.getInstance().getItemByFullName('printuser').getLastBuild().logFile.text
}
}
Note that you should approve specific signatures to allow pipeline and groovy to access the above functions.
If you haven't done this yet, the above script will fail with
Start condition check Scripts not permitted to use staticMethod
jenkins.model.Jenkins getInstance. Administrators can decide whether
to approve or reject this signature.
You can follow the error message by clicking on the link of the message which will redirect you to the scriptApproval page of Jenkins where you can add the signature that needs approval to the approval list.
You should have the following list of approved signatures for the above to work
method hudson.model.ItemGroup getItem java.lang.String
method hudson.model.Job getLastBuild
method hudson.model.Run getLogFile
method jenkins.model.Jenkins getItemByFullName java.lang.String
staticMethod jenkins.model.Jenkins getInstance
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getText java.io.File
If you follow this solution, you can change the post's failure action to always so it print the log always (not only on errors) and remove the println slaveJob.rawBuild.log
When using the def keyword a variable has block scope. Try slaveJob = build job: 'printuser' without the def.
I am trying to run a groovy script in Jenkins slave node to retrieve child jobs from a folder in Jenkins slave node. Here is the groovy script I tried:
I tried some SO answers and found groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding
But this doesn't solve my problem.
Please find the code that I tried:
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import jenkins.model.*
static main(args){
def childJobFolder = "childJob"
def childJobNameList = []
def env = System.getenv()
// Setting the environment properties to variables.
def jenkinsUsername = env.UAT_JENKINS_MY_USER
def jenkinsPassword = env.UAT_JENKINS_MY_PASS
def jsonSlurper = new JsonSlurper()
// Getting the child job names from "childJob" folder
Jenkins.instance.getItemByFullName(childJobFolder).allJobs.each{
def childJobName = it.name.toString()
if(childJobName.startsWith("job-")){
childJobNameList.add(childJobName)
}
}
println "\n" + "Child Jobs Available: " + childJobNameList + "\n"
}
Here is what I got in the console:
Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson3067346520259876246
groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson3067346520259876246
at hudson3067346520259876246.run(hudson3067346520259876246.groovy:17)
Build step 'Execute Groovy script' marked build as failure
Can someone help me to fix this error? Thanks in advance!
Finally, I found out the solution for this error. This is caused by running on plain groovy script instead of system groovy script. As Jayan said the Jenkins variables only available for System groovy scripts and not for plain groovy script. For that reason I could not load Jenkins instances from plain groovy script.
I'm having problems with my Jenkinsfile.
I want to list all running workers in current job, but jenkins just fails without printing any error. Code snippet:
#NonCPS
def check_running_process() {
// Check if PR build already in progress to kill old one
def pull_id = env.ghprbPullId.toInteger()
println pull_id
def current_build_id = env.BUILD_ID.toInteger()
println current_build_id
def currentJob = Jenkins.instance.getItemByFullName('jobname')
println currentJob
}
Output:
[Pipeline] echo
2
[Pipeline] echo
47
So Jenkins stops at def currentJob = Jenkins.instance.getItemByFullName('jobname')
There is no error produced, just a build fail.
There are no errors in jenkins.log file.
This works in scripting console.
Did anyone had the same problem?
Thank you
Okay, this was my problem.
I was calling check_running_process() inside try handler.
This produced no error, but failed because
method jenkins.model.Jenkins getItemByFullName java.lang.String
signature was not approved
(nor it produced script approval request).