Running jobs in Jenkins slave node using Groovy script - jenkins

I am confused about whether a Jenkins job can be run using Groovy script in the slave node. I referred a StackOverflow answer [1] which says that System Groovy script jobs can be run in master and not in slave, and to run a job in slave it has to be Groovy script rather than System Groovy Script. Can someone clarify me whether we can run a slave job using System Groovy Script? Since I am trying through Groovy script I am unable to access few Jenkins instances. Please suggest me a better way with an explanation. Thanks in advance

I have finally found that it is only possible to run jobs in Jenkins slave node using Grovvy script. The system groovy script runs inside the Hudson master's JVM. Thus it will have access to all the internal objects of Hudson, so we can use this to alter the state of Hudson. It is similar to the Jenkins Script Console functionality.

Related

Can Jenkins Scriptler be Configured for a Groovy Script on the Master Node to be used For Builds on Remote Nodes?

On Jenkins, I have a Groovy script written that I would like to run across many different projects which are in turn, restricted to run on various slave nodes. The groovy script resides on the master node. Using scriptler and adding a "Execute system groovy script" to the build section will cause Jenkins to look for the defined script file on the node that the project was previously restricted to run on rather than the default master groovy script folder. I was hoping to have all projects utilize the same script on the master node.
The script does work as intended as I can copy the contents of the groovy script into the build step and that works.
Thanks!

Using string.execute() in Windows Jenkins slave run on master

I am trying to use the String.execute() under the scope node('node_name), in a Jenkinsfile that runs on a Windows Jenkins slave.
After some issues, i discovered that this specific method runs on my Jenkins master (linux), not on the slave.
There is a reason for that?
Is there a way to use that method and it will run on the slave?
Thanks
Your Jenkinsfile needs to include agent { <labelname served by slave>} either immediately below pipeline { or in the particular stage.
DSL for the agent keyword is only currently supported in these two possible locations.

How to add build server details in Jenkins pipeline?

I am having Jenkins in one server and my build server is different. How to point build server in Jenkins pipeline so that my application will build in build server
Using grade and java.
Do we need to use node('Build 1') inside stage?
Suggest me some sample code please.
In Jenkins, your build server called slave machine or Jenkins nodes, which you need
Firstly add this "buildserver" into Jenkins nodes in advance, then you will get node name (or label them like ubuntu-buildserver), see one jenkins distributed build blog
Secondly in scripted pipeline you specify/reference this name in node
node("ubuntu-buildserver")
If you use declarative pipeline, check syntax#agent part.
It is similar for other global configuration like credentialsId, you need define those parameters in jenkins and refer to use them in your pipeline script.

How to run a script on a slave node from Master's System Groovy script in a Job

I know that in groovy pipeline scripts you can run certain scriptblock in slave workspace, but can you do similar thing from a job system groovy command?

Difference between execute groovy script and the execute system groovy script in jenkins?

Can anyone explain the different between the execute groovy script and the execute system groovy script in jenkins? And how to call the script to slave using execute system groovy script.
To execute a groovy script on the slave machine, you should use groovy plugin
Quote
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.
First part of your question is answered in the same page
The system groovy script, OTOH, runs inside the Hudson master's JVM.
Thus it will have access to all the internal objects of Hudson, so you
can use this to alter the state of Hudson. It is similar to the
Jenkins Script Console functionality.
Another point on system Groovy scripts to be aware of. While the documentation says that it always runs on the Jenkins master, I've found through painful means that it isn't true if it's in a job that is triggered by another job. In that case, make sure you specifically restrict it to run on the master or bad things will happen.

Resources