Groovy script works in console but not in job - jenkins

So I've created a Groovy script, and it works in the console. I then implemented it into my job in the field Execute Groovy Script but then it failed.
Here I have the console, and I do know the imports aren't needed, but I had to try.
But When I run my job with this code, I get this error:
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building remotely on Buildsl (hant) in workspace C:\JS\workspace\node_checker
[Vetus_node_checker] $ groovy C:\JS\workspace\node_checker\hudson633049885410603886.groovy
Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson633049885410603886
groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson633049885410603886
at hudson633049885410603886.run(hudson633049885410603886.groovy:6)
Build step 'Execute Groovy script' marked build as failure
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Finished: FAILURE

It look like you are using the "Execute Groovy script", but to execute scripts like in Script Console you must use the "Execute system Groovy script" step.
The normal execute will start a new Java/JVM with Groovy which is not "connected" to your Jenkins.
Only the system step will execute the script within the existing Java/JVM that is running Jenkins.

So yes I had to run it as a Execute system Groovy script, but I also had to give the full path to the lib I'm using, before it worked. the Script Console didn't need the jenkins.model... part, but the job does.

Related

how to execute a bash script in a jenkins declarative pipeline

I have started recently using Jenkins in windows 10. I have a freestyle job that sync from the SCM, build a C++ solution and then it runs a batch script to upload to steam. I am trying to convert it to pipeline as I realized reading the documentation how much more powerful it is. My problem is that on the step to run the .bat file it gets stuck forever, this is the step:
...
Stage('batch script'){
steps{
bat 'start C:/Users/User/.jenkins/workspace/Project/Steam Build Scripts/scripts/build_dev.bat'
}
}
...
and this is the simple .bat file:
"C:\Users\User\.jenkins\workspace\Project\Steam Build Scripts\builder\steamcmd.exe" +login "someUser" "somePassword" +run_app_build "C:\Users\User\.jenkins\workspace\Project\Steam Build Scripts\scripts\app-build-813780-dev.vdf" +quit
running the same file from the freestyle job works fine like this:
I found out the way in case somebody has the same issue, due to the path containing spaces it has to be called like this:
bat ('call "C:/Users/User/.jenkins/workspace/Project/Steam Build Scripts/scripts/build_dev.bat"');

Execute shell giving error in Jenkins Freestyle job

Whenever I am trying to do execute shell command in freestyle job in Jenkins, however when I try to do same thing while from pipeline, it is running successful.
Following is the error which I am getting in freestyle job:
It seems that the drive is full and there is no space left to execute this shell command. See this log:
So what you should do is to make sure that there is enough space on the drive where you run the script/command is available. Maybe you could switch to another partition or delete temporary or unused files.

Jenkins: Running groovy script with ant commands

I have a groovy script with ant commands on it. The script is successfully run in my local machine but when I tried it with Jenkins the groovy script always fail. Jenkins always return error that "ant can't create task or type p4Change". I already added Apache ant support in the global configuration. How do I configure ant to successfully run the groovy script I have. Any idea. Thanks.
Sample code snippets:
I have execute.groovy file with ant commands
ant=new AntBuilder()
def checkChanges {
ant.p4change(description:"Checking",port:'perforce:1666',user:optional,view:"'${workspace}'...")}
And I created a batch file that will run the execute.groovy file
call groovy execute.groovy project bopolz18 -c bopolz18.Workspace 150718
In my machine this works well but in Jenkins when I execute the batch command it fails giving the error mention above.

Executing groovy scripts on Jenkins' slaves

I'm trying to execute a simple groovy script on Jenkins' slaves using the Groovy plugin.
I'm using the execute groovy script option and not the execute system groovy script option. I'm also using the "install automatically" (install from groovy website) option of the plugin of the latest version(2.4.6) . However, the job fails and I'm getting this:
Building remotely on .... (...) in workspace C:\Jenkins_Slave\workspace\...
Unpacking https://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.6.zip to C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6 on ...
[groovy_reset_dead_slave] $ C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6\bin\groovy.bat C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy: 5: unable to resolve class jenkins.model.Jenkins
# line 5, column 1.
import jenkins.model.Jenkins
^
1 error
Build step 'Execute Groovy script' marked build as failure
Finished: FAILURE
How can I overcome this issue and make it work properly?
From the Groovy plugin page:
Groovy Script vs System Groovy Script
The plain "Groovy Script" is run in a forked JVM, on the slave where the build is run. It's the basically the same as running the "groovy" command and pass in the script.
The system groovy script, OTOH, runs inside the Jenkins master's JVM. Thus it will have access to all the internal objects of Jenkins, so you can use this to alter the state of Jenkins. It is similar to the Jenkins Script Console functionality.
When not using the system Groovy, you do not have Jenkins object in your classpath. You need to make sure the required classes are accessible from the job you are running, and pass them in the "Class path" field of the "execute groovy script" step.
We can use RemoteDignostics class
The following Script runs on master but uses RemoteDignostics to run groovy on Worker Nodes/slave.
import hudson.util.RemotingDiagnostics
import jenkins.model.Jenkins
String agent_name = 'your agent name'
groovy_script = '''
println System.getenv("PATH")
println "uname -a".execute().text
'''.trim()
String result
Jenkins.instance.slaves.find { agent ->
agent.name == agent_name
}.with { agent ->
result = RemotingDiagnostics.executeGroovy(groovy_script, agent.channel)
}
println result

Running groovy script on slave nodes

How do we configure running groovy scripts on slave nodes? am able to get groovy installation from manage jenkins section, but the scripts fail to run.
I am having a job with execute groovy steps and this job is supposed to run on "slave" nodes.
The System groovy option wont fit since it runs on master and the execute groovy on job configured to run on slave fails with error
/workspace/hudson5188055044238549912.groovy: 2: unable to resolve class jenkins.model.Jenkins
# line 2, column 1.
import jenkins.model.Jenkins
Seems the jars are not picked during run. Is there easy way to setup or which jenkins and groovy jars are required??
On the job configuration page there should be a drop down beside "Groovy Version" under the "Execute Groovy Script" block. You need to select the name of the groovy install that is on master. Jenkins will grab the necessary files from master.

Resources