k8s in Active Choices Reactive Reference Parameter script - jenkins

In my Jenkins pipeline the user should select machine name from "Choice Parameter". Let's say there are 2 machines: machine1 and machine2.
The next parameter should be dynamic Kubernetes namespaces list. I suppose I need to use "Active Choices Reactive Reference Parameter", that will run "kubectl get namespaces" command on selected machine: machine1 or machine2. The result of this command should appear as a list parameter for selection.
I googled a lot, but didn't succeed to make it running. Pls advise.
How can I debug "Active Choices Reactive Reference Parameter"? If I use in "Active Choices Reactive Reference Parameter" println command, where it will appear?

You cannot connect with different machines from the groovy code that you have shared.
The command is still running on the same machine where your jenkins job is running and getting list of cluster from the same machine.
you can set multiple clusters in your kubectl config and run the kubectl command accordingly.
Code logic should be -
if selected_machine = machine1
kubectl config use-context machine1;kubectl get ns
further we can explore Kubernetes API to interact with the cluster and get the NS for cluster accordingly.

Related

Ansible prompt with jenkins job

I'm running an Ansible playbook where I've added a "prompt" command to install one or other software depending on the user input.
At the playbook execution under Jenkins, there is :
"WARNING : Not waiting for response to prompt as stdin is not interactive".
I've been inspired by several topics as
pause ansible playbook for user confirmation, whether to run rest tasks
or
Ansible - Using user input to chose a variable
or
Ansible vars_prompt for roles
to make some tries, but the same WARNING appears each time.
I've wondered if there was a parameter to add in the ansible.cfg file to force the interactive mode, or something "mandatory" to write in the playbook (I've searched on https://docs.ansible.com/), but found nothing on GooXX either.
What you are describing is a Jenkins feature. Jenkins launches job as non-interactive mode so you wouldn’t be able to take parameter from ansible playbook while the job is executing.
You can however move the required parameter to Jenkins job for user input and then pass to ansible. For example, in case of jenkins pipeline parameters block can be used. These parameters then can be passed to ansible via --extra-vars and/or extras option of ansible plugin.

New Agent per build with Scripted Pipeline

I built a scripted pipeline that requires docker to spin up to run the new code being deployed.
Since each docker instance runs on port 8081, only one build can run per agent.
Each build agent has 4 available executors, so I need to manually set the executors to 1 each time an agent spins up.
Is there any way I can still continue using a scripted pipeline and make sure that only one job is executed per agent?
If you have only one job you could use the following property to get running only the latest build
disableConcurrentBuilds()
You could also use the lock/milestone steps to block a stage
https://jenkins.io/blog/2016/10/16/stage-lock-milestone/
Finally you could set the port dynamically to be something else than 8081
with a bash command and python:
PORT=$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')
then you whould pass the PORT variable to your docker command in order to map the internal port to the new external one we got from the variable

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: 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.

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