In an example pilepline I saw a variable named tool and is used like this:
echo "${tool 'vs2017'}"
// Outputs C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin
My question is, what kind of variable is this? and how can I see what includes in this variable. I tried to print it like this but it fails:
tool.each { k, v -> echo 'k'}
groovy.lang.MissingPropertyException: No such property: tool for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:271)
at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
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 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 just written the below groovy script to read list of files in the Jenkins workspace an check if there is any file name starts with report. If it's I need to print those values too.
build.getWorkspace().list().each
{
println it.getName()
if(it.name.startsWith('report'))
{
println it
}
}
Exception:
groovy.lang.MissingPropertyException: No such property: it for class:
Script1 at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:243)
at
org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
at
org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
at
org.kohsuke.groovy.sandbox.impl.Checker$checkedGetProperty$0.callStatic(Unknown
Source) at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:230)
at Script1$_run_closure1.doCall(Script1.groovy:12)
How can I find out the list of files in workspace and filter them by using name using groovy?
I have the following pipeline script:
node {
def myStep = sh
myStep "ls -la"
}
I thought steps were visible as variables and could be assigned to variables so that they can be used later (for example choosing a different step depending on some conditions).
However, this fails with:
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: myStep for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:232)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:282)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:286)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:3)
at ___cps.transform___(Native Method)
How can I put a step in a variable to use it later without hardcoding its name?
You can write a method in your pipeline that wraps the behavior you want. It will have access to the script variables.
node {
myStep("ls -la")
}
def myStep(String script) {
sh(script)
}
My current workaround:
node {
def myStep = { script ->
sh script
}
myStep("ls -la")
}
I have been trying to find a solution for changing build parameters programmatically using jenkins pipeline plugin where i have jenkinsfile with following content:
#!/usr/bin/env groovy
import hudson.model.*
properties(
[
parameters(
[
string(defaultValue: 'win64', description: 'PLATFORM', name: 'PLATFORM'),
string(defaultValue: '12.1.0', description: 'PRODUCT_VERSION', name: 'PRODUCT_VERSION')
]
)
]
)
stage('working with parameters'){
node('master'){
def thr = Thread.currentThread()
def build = thr?.executable
def paramsDef = build.getProperty(ParametersDefinitionProperty.class)
if (paramsDef) {
paramsDef.parameterDefinitions.each{ param ->
if (param.name == 'PLATFORM') {
println("Changing parameter ${param.name} default value was '${param.defaultValue}' to 'osx10'")
param.defaultValue = "osx10"
}
}
}
}
}
But it fails everytime with error as :
groovy.lang.MissingPropertyException: No such property: executable for class: java.lang.Thread
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
After searching a lot i could find somewhere that this script needs to run as system groovy script and everywhere they are referring to Groovy plugin but as i am using pipeline project, i am not able to understand how can i force pipeline to execute my scripts as system groovy script.
Please solve my queries as i have left no link on google unopened :(