How to use multiple labels to select a node in a Jenkins Pipeline script? - jenkins

Intro:
We are currently running a Jenkins master with multiple slave nodes, each of which is currently tagged with a single label (e.g., linux, windows, ...)
In our scripted-pipeline scripts (which are defined in a shared library), we currently use snippets like the following:
node ("linux") {
// do something on a linux node
}
or
node ("windows") {
// do something on a windows node
}
Yet, as our testing environment grows, we now have multiple different Linux environments, some of which have or do not have certain capabilities (e.g., some may be able to run service X and some may not).
I would like to label my slaves now with multiple lables, indicating their capabilities, for example:
Slave 1: linux, serviceX, serviceY
Slave 2: linux, serviceX, serviceZ
If I now need a Linux slave that is able to run service X, I wanted to do the following (according to this):
node ("linux" && "serviceX") {
// do something on a linux node that is able to run service X
}
Yet, this fails.
Sometime, also a windows slave gets selected, which is not what I want to achieve.
Question: How can i define multiple labels (and-combined) based on which a node gets selected in a Jenkins scripted pipepline script?

The && needs to be part of the string, not the logical Groovy operator.

Related

How to specify custom path for remote node on Jenkins?

I've a Jenkins server. It runs on D:\Jenkins.
In my jenkins file, I've the following:
pipeline {
agent {
node{
label 'windows-node'
customWorkspace "${JENKINS_HOME}\\${env.BRANCH_NAME}"
}
}
//...
}
This was working fine and tries to use the D:\jenkins\feature\testbranch (by example).
I now setup a new node, and it has only one disk, C:\
The remote node has the Remote root directory configured as C:/ws. So I was expecting that my output folder would be C:/ws/feature/testbranch.
But it seems it tries to access the D:\jenkins\feature\testbranch on the remote node. How to use the node specific root folder in an Jenkins file?
So that variable appears to be specific to the master. You probably need a environment variable which will point to a location that is consistent across your slaves (like WKSPC_LOC), set it in your windows environment and use something like customWorkspace "${WKSPC_LOC}\${env.BRANCH_NAME}", note that you still need to set this up in every slave. In general for CI, having different things being treated same creates problems. So a slave should be the same (consistent). As of now I don't think there is way to have different workspace locations for master and slave. In linux you could have used sym links, but I can't think of a solution for windows, except what I mentioned before, but I still doubt it will work.

Jenkins will look for available node

I have a job in jenkins that run batch file on few nodes. I did it using node parameter. I also added condition that it will run few times. This job is running in parallel but when one host is stack I would like it to continue running this job on other nodes. This is not working. Jenkins is going to the first parameter in the node and trying to run on it and if it is stuck it will not continue to other node. I would like jenkins to look for available node. I tried using multi job plunging and it also didnt work. Also tried working with label but it is not running in parallel....any help ?
I think you can add labels to your node , if you have 4 slaves on ly 2 are windows both will have the label windows , than in your job use the Restrict where this project can be run , add the label value - windows
if you need to run the same job in parallel you can create a matrix job , the same flow will run on several slaves.

To run commands on different linux nide by jenkins

I want to jenkins to execute a list of commands on different linux node in a network.
What steps should I take to run a command on another linux node by adrressing its ip address
if I understood you correctly you should add this node as a slave machine to the Jenkins.
go to Manage Jenkins section and then to Manage Nodes and just add a new Node
once you added the nodes.
in pilpeline groovy script
use :
node('node1'){
//command execution
}
node('node2'){
//command execution
}

[Jenkins]Why User-Defined axis doesn't work with slaves

I created a multi-configuration project that is running a couple of automation tests. I have an user-defined axis that is running on a single node and I want to parallelize the process.
I have the following configuration:
My problem is that both jobs will be running one the same node.
At first it wil start "EU_Washroom" and then "EU_Linen".
"EU_Linen" is not running on the 5th(JenkinsQFT5) node, instead is running on 4, disregarding the combination filter(QF_SCRIPT_NAME == "EU_Linen" && slave=="Jenkins-QFT5").
Also at the end of the name of the job, the name of the node is added.
Is it a bug or am I doing something wrong? I'm using Jenkins version 1.598
EDIT:
I deleted the combination filter and all 4 combinations were running on a single node.
Both nodes are set with the usage: "Utilize this node as much as possible"
EDIT #2:
I deleted the user-defined Axis, and let only the Slave-Axis and it acts the same.
The console of the job that should ran on Jenkins-QFT5: Building remotely on Jenkins-QFT4 in workspace c:\JenkinsSlave\lib\workspace\8.03.08-QF-tests\TestParalelFor8\Jenkins-QFT5
In Jenkins ver. 2.5 this is working well.
It was a bug in Jenkins v1.598

Jenkins: Slave selection based on user's choice

I'm trying to configure a jenkins job so that based on the choice of the user the node/slave should be selected.
example : if choice = windows ->slave1
if choice = Linux ->slave2
I've tried with configuration matrix i'm getting error that the nodes are offline.Is there any plugin to do such selections in jenkins
The simplest is to create two build jobs that are then tagged to a specific slave.
When you configure the job you can add labels to it that specify the requirements for this job.
Example:
linux-build, add the label linux
windows-build: add the label windows
Then, when you create the slaves you need to assign them labels as well specifying the capabilities.
Example:
Windows system, add the windows label
Linux system, add the linux label
After this your builds will automatically go to the correct system and you never have to specify anything again. This is better then having to manually specify and trigger a job.
One more advantage, if the job fails you know why.. maybe you linux job always succeeds but you windows job always fails, if you combine this in one job you can't really see this pattern but if you have two separate builds you will immediately see a dark cloud forming over you windows build.
Checkout the following plugin:
https://wiki.jenkins-ci.org/display/JENKINS/NodeLabel+Parameter+Plugin
I used the Jenkins CLI to implement the dynamical slave selection.
1) create two jobs: job A triggers job B
2) at job A, input the followings at Build/Execute Shell
if choice = windows, SERVER=slave1
if choice = Linux, SERVER=slave2
java -jar jenkins-cli.jar -s http_to_jenkins-server:port build buildname -p SERVER_LABEL=$SERVER -v -w --username yourusername --password yourpassword
3) at job B, select "This build is parameterized" and add a String Parameter SERVER.
Hope it helps.

Resources