A way to request ips of available Jenkins slaves by label in a freestyle job? - jenkins

Is there a possibility to request the list of Jenkins slave ips,
inside of a Jenkins freestyle job,
when executing a shell script?
Maybe as an environment variable?

You can determine the IP address(es) of a slave node via groovy. See this answer.
So you could proceed as follows:
Create a groovy build step that will write the IP addresses of all slaves of interest to a text file
In you shell script build step, read IP addresses from that file.
As an example, this groovy code will print the names and IP addresses of all slaves with label mylabel:
import hudson.model.Computer.ListPossibleNames
slaves = Hudson.instance.slaves.findAll { it.getLabelString().split() contains 'mylabel' }
slaves.each {
println "slave '${it.name}' has these IPs: " + it.getChannel().call(new ListPossibleNames())
}
Sample output:
slave 'foo' has these IPs: [10.162.0.135]

Related

Run script on multiple hosts using jenkins pipeline groovy script

I am new to Jenkins pipeline groovy script.
I have a job param named 'HOSTNAMES' which will take some host name values separated by comma as an input. I need to execute some scripts on all these hosts in parallel mode. How to achieve this using jenkins groovy script. Pls guide me.
Assuming all these nodes (hostnames) are connected to the Jenkins server and the HOSTNAMES parameter is a list of the node names (as they appear in the jenkins server) you can use the parallel step to achieve what you want.
You will have to transform each node name into a map entry representing the parallel execution branch and then run them using the parallel keyword.
something like:
def hosts = HOSTNAMES.split(',')
def executions = hosts.collectEntries { host ->
["Running on ${host}" : {
node(host) {
// The code to run on each node
stage("Execution") {
echo 'example code on #{host}'
...
}
}
}]
}
parallel executions
You can also run it in one line if you want:
parallel hosts.collectEntries { ...

Jenkins unable to build on kubernetes setup

I have a problem with Jenkins build and this is what I am seeing in the build log:
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Still waiting to schedule task
‘Jenkins’ doesn’t have label ‘ci’
I am not sure what does that actually means.
I have tried to check the configuration under "Configure Clouds" in Jenkins and it looks like this:
I would suggest few changes do it like this
Keep everything blank for jenkins tunnel. Jenkins will automatically will pick it up.
If you deployed this jenkins instance in kubernetes cluster then please use internal address for jenkins_url like http://jenkins.infrastructure.svc i assume your jenkins service name is jenkins and it is ClusterIP
For Pod template you can just use name and labels and remove container template section. kubernetes will pick it up default.
Please use the same label to schedule job as agent means you gave the label name different and using as diffrent means your jenkins looking for ci agent which is not present so you should replace with jenkins-jenkins-slave in your pipeline

Not able to use Jenkins labels in pipelines

I can't get Jenkins labels to work in Jenkins while using pipelines.
My node is defined with a label say "poolA", and the following pipeline should attempt to run on that node but it doesn't.
Any ideas why?
Console says:
MyAgentis reserved for jobs with matching label expression; Jenkins
is reserved for jobs with matching label expression
This is my pipeline:
node{
label 'poolA'
echo " jenkins pipeline for rapidx node"
}
My node is defined as follows:
Only build job with label expression ....
Launch method : Java webstart
Avail : Keep this agent online as much as possible
Executors # : 1
Other settings unchanged
Note that I am behind a firewall (no internet access during execution) using
Jenkins 2.73.2
This is a clean install of jenkins on windows, the only change I made was to enable Java web start.
Try the following syntax for the pipeline:
node("poolA"){
echo "Jenkins pipeline for rapidx node"
}

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
}

Finding IP of a Jenkins node

A windows slave node connected to Jenkins server through "Java web start". The system information of the node doesn't have it's IP address.
I had to run through all the slaves node we had, and find which machine (ip address) corresponds to the slave node in Jenkins.
Is there a way to find the IP address of a slave node from Jenkins itself?
Through the Script Console (Manage Jenkins -> Nodes -> Select a node -> Script Console) of the node we can execute groovy script. Run the following command to get the IP address.
println InetAddress.localHost.canonicalHostName
The most efficient and platform-independent way to find out the IP is probably the following groovy code for the "global" Script Console on the master:
import hudson.model.Computer.ListPossibleNames
def node = jenkins.model.Jenkins.instance.getNode( "myslave" )
println node.computer.getChannel().call(new ListPossibleNames())
In the console, this yields (for example)
Result
[192.168.0.17]
The result is a list of strings, as there's potentially multiple IP addresses on one machine.
Since this does not require the node-specific consoles, it's easy to add a loop around the code that covers all nodes.
To answer this same question on a non-windows Jenkins slave:
Get the IP address:
println "ifconfig".execute().text
Get the hostname:
println "hostname".execute().text
From the Web Interface
Go to the node's Log link:
http://jenkins.mycompany.com:8080/computer/my_node_name/log
The first line should say something like:
JNLP agent connected from /10.11.12.123
screenshot
This is very similar to what deepak explained but I added images along the short steps.
In Jenkins UI click:
Manage Jenkins -> Nodes -> Select a node -> Script Console
then run println InetAddress.localHost.canonicalHostName
In your Jenkins job if its in groovy or else echo the ifonfig
sh "/sbin/ifconfig -a | grep inet"
To get the ip on a Windows slave:
Navigate to the Script Console (Manage Jenkins -> Nodes -> Select a node -> Script Console)
println "ipconfig".execute().text
Can also be found through the Jenkins UI:
Manage Jenkins --> Manage Nodes --> Click Node name --> Configure
This should display both the public and private ip address of that node

Resources