Jenkins Workflow Optional Node - jenkins

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()

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

Execute Build Jobs/Pipelines not on Master but only on Build Agent

Following the Jenkins Best Practices, I want to avoid that Build Jobs/Pipelines could be executed into my Jenkins Master.
To do so, I've installed the Job Restrictions Plugin, using it to configure the Master to run only some Maintenance Pipelines.
The problem is that now Build Pipelines that are configured to run on specific Agents, are not executed anymore. I see that the Build Queue continuously grows, and the Pipelines are not runned. I think that this behaviour could be related to Flyweight Executors of the Master.
So, the question is the following: How can I execute on Master just a little subset of Maintenance Pipelines and, in the mean time, execute Build Pipelines only on specific Agent?
You can configure the master node to only be used when explicitly named. Just click the master node > go to configure and change Use this node as much as possible to Only build jobs with label expressions matching this node
I found the solution that perfectly fits with my needs, here.
To quickly sum up the solution, I was to able to exclude all the user Builds from Master and run on it only the Jobs/Pipelines of a specific Jenkins folder (IuA in my case), configuring the Job Restrictions Plugin in the following way:
In order to better understand the logic behind this solution, I recommend you to give a look at link that I posted above.

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.

Jenkins: how to test the slaves

I am creating a list of Jenkins jobs for sanity test of our Jenkins build environment. I want to create layers of jobs. The first layer of jobs will check the environment, e.g. if all slaves are up, the 2nd layer then can check the integration to other tools such as GitHub, TFS, SonarQube, then the 3rd layer can run some typical build projects. This sanity test can also be used to verify the environment after any major changes to the Jenkins servers.
We have about 10 slaves created on two servers, one Windows and one Linux. I know I can create a job to run on a specific slave, therefore test if the slave is online, but this way I need to create 10 jobs just to test all slaves. Is there a best approach to check if all slaves are online?
One option is to use Jenkins Groovy scripting for a task like this. The Groovy plugin provides the Jenkins Script Console (a useful way to experiment) and the ability to run groovy scripts as build steps. If you're going to use the Script Console for periodic maintenance, you'll also want the Scriptler plugin which allows you to manage the scripts that you run.
From Manage Jenkins -> Script Console, you can write a groovy script that iterates through the slaves and checks whether they are online:
for (node in Jenkins.instance.nodes) {
println "${node.name}, ${node.numExecutors}"
def computer = node.computer
println "Online: ${computer.online}, ${computer.connectTime} (${computer.offlineCauseReason})"
}
Once you have the basic checks worked out, you can create either a standalone script in Scriptler, or a special build to run these checks periodically.
It often takes some iteration to figure out the right set of properties to examine. As I describe in another answer, you can write functions to introspect the objects available to scripting. And so with some trial and error, you can develop a script performs the checks you want to run.

Resources