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

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.

Related

Why declarative pipelines need to run on master if there are build executors available?

I'm using recent Jenkins version 2.286 and since this update there is an security hint: "You should set up distributed builds. Building on the controller node can be a security issue. See the documentation."
But I'm already doing so with three Jenkins nodes and I also fully understand the security implications.
The problem here is, that there are two jobs that need to run an the master, since they are the jobs that deploy those Jenkins nodes. That means I can not reduce the build executors to 0.
I've also tried using the Job Restrictions plugin to restrict which jobs can run on the master. This problem here is that all my jobs are waiting for the master queue do have a free slot available. I wonder why, because they all are declarative pipelines and define something like:
agent {
label 'some-different-node-label'
}
Which means they aren't really executed on the master node.
Questions here are:
Is this intentionally that all jobs require the master node before switching the agent?
Is there any configuration option to change that?
Is there a way to execute the deploy jobs on master, even if there aren't any executed defined (to bypass that behavior)?
Thanks.
With declarative pipelines the lightweight code checkout is done on the Master node to get a Jenkinsfile for that job. While this doesnt use an executor on the Master perhaps the Job Restriction Plugin is still blocking this (I havent used it before so cannot comment)
Also certain pipeline actions are delegated back to the Master node as well (e.g. the withAWSParameterStore step.
If you look at the console output for a Declarative pipeline job, you will see lots of output (mainly around library checkouts or git checkouts) before you see the start of the pipeline [Pipeline] Start of Pipeline. All that is done on the Master.
Unfortunately this cannot be changed as the Master needs to do this work to find out which agent type to delegate the job to.
Depending on how you are running you agents, you could use something like the EC2 Cloud Plugin to generate you agent nodes which wouldn't require a job to do it

Running jobs in Jenkins slave node using Groovy script

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.

How to checkout and run pipeline file from TFS on specific node in Jenkins?

I am trying to run a pipeline job that get its' pipeline file from TFS but the mapping of the workspace and the checkout is done on the Master instead of the Slave.
I have Jenkins-master which is installed on a linux machine and I connected a windows machine as a slave to it. I created a pipeline job with 'Pipeline script from SCM' option selected for TFS.
How can I make the windows slave run that pipeline job?
The master can't run that job because it is running on linux and it fails when it is trying to map a workspace to TFS in order to download the pipeline script and run it.
Even if I create another pipeline job and select to hard-code a script to run my original pipeline job like this:
node('WIN_SLAVE') {
build job: 'My_Pipeline'
}
It doesn't work.
And I can see in the output that the initiali script (above) is in fact running on my windows slave, but when it's building the job 'My_Pipeline' it still tries to map a workspace to the Jenkins-master at it's linux machine path /var/jenkins/... and it fails.
If the initial pipeline script ran at the windows slave, why does the other pipeline script not running on the same node? Why is it trying again to checkout the pipeline file from TFS to the Jenkins-Master?
How can I make the windows slave checkout the pipeline file and run it?
Here are some things to check...
Make sure you disabled the original job, or you are completely redefining it for running on the slave, because you indicated you set up “another job” for the slave. It appears that this other job is just triggering the previous job, rather than defining its own specifications. When the job is ran on the slave, it’s just running whatever settings are in that original job.
Also, If you have the box checked to build when a change is pushed to TFS, then your original job could still be trying to run every time a change is made to TFS.
Verify the slaves Remote root directory is set properly in the slave configuration under Manage Jenkins -> Manage Nodes.
Since this slave job is triggering the other job you originally created on the master, then it will build on the master as expected.
Instead of referencing the My_Pipeline job, change the My_Pipeline job itself to run on the slave. If you are using a declarative Pipeline for the original job, then change that original job to run on the slave within the original job settings. You can do it similarly to how you have indicated above, just define the node in the original job.
If the original job is a freestyle project, there is a checkbox titled Restrict where this project can be run. Check that and include the name of the slave in the Label Expression. When you run the job, it will then be restricted to the slave.
Lastly, posting the My_Pipeline job will be helpful.

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.

Jenkins Workflow Optional Node

We have a section of our pipeline that needs to be run on slaves running windows so we have a section similar to:
node("Windows") {
// Do some windows stuff
}
Because this part of the pipeline isn't mission critical I'd prefer the whole job not to fail if the Windows slaves are down for some reason.
Is there some way of having some logic here that says "do this if a Windows slave is up but skip if there all down".
You can use Conditional Build Step plugin. So you can wrap you windows part of workflow script into slave validation of some sort.
The answer to this question gave me the information I needed.
Jenkins How to find if a given slave is running a Job
So I was able to use:
node.getComputer().isOffline()

Resources