How do I start a Jenkins agent from the slave? - jenkins

Jenkins v2.89.3.
I'm trying to start an agent but am getting the following error.
$ java -jar agent.jar -jnlpUrl http://localhost:8080/computer/testagent/slave-agent.jnlp
Error: Unable to access jarfile agent.jar
The documentation from the wiki that I'm following is from here:
Launch agent headlessly -
https://wiki.jenkins.io/display/JENKINS/Distributed+builds#Distributedbuilds-Differentwaysofstartingagents
The handbook section for managing nodes doesn't appear to have been written yet:
https://jenkins.io/doc/book/managing/nodes/
If anyone know what I'm doing wrong, or another way I can start the agent from the slave (not from the master), please let me know.

In the "Run from agent command line..." CLI instructions that were given by the Jenkins master: The word 'agent.jar' is a hyperlink.
Hover over that hyperlink to copy the target URL.
Use that URL with wget to retrieve agent.jar from the Jenkins master by pasting the URL into your terminal window. e.g.,
[root#Jenkins-Agent-1 ~]# wget http://jm0:8080/jnlpJars/agent.jar
--2022-03-15 01:13:45-- http://jm0:8080/jnlpJars/agent.jar
Resolving jm0 (jm0)... 192.168.0.174
Connecting to jm0 (jm0)|192.168.0.174|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1524968 (1.5M) [application/java-archive]
Saving to: ‘agent.jar’
100%[====================================================================================>] 1,524,968 --.-K/s in 0.04s
And then finish the installation by executing the command that was given by your Jenkins master. e.g.,
[root#Jenkins-Agent-1 ~]# java -jar agent.jar -jnlpUrl http://jm0:8080/computer/JA1/jenkins-agent.jnlp -secret 73393f441b43921357d959cd664d9e69d67ff7ee1f9876070ab76ee46b420e19

I found that the Swarm plugin for Jenkins did exactly what I needed.
Install plugin on Jenkins
Download swarm jar on slave
java -jar path/to/swarm-client.jar -home http://localhost:8080
Result: Slave agent registers on master as seen from the Manage Nodes section of the Jenkins UI
https://wiki.jenkins.io/display/JENKINS/Swarm+Plugin

Related

Execution a deployment script on a remote ssh server through a Jenkins pipeline

I've got a Jenkins pipeline containing stages for source loading, building and deploying on a remote machine through SSH. The problem is about the last one. I saved the script of the following template on the remote server:
#!/bin/bash
bash /<pathTo>/jboss-cli.sh --command="deploy /<anotherPath>/service.war --force"
It works fine if executed in a terminal connected to the remote server.
The best outcome I've received through Jenkins is
/<pathTo>/jboss-cli.sh: line 87: usr/bin/java/bin/java: No such file or directory
in Jenkins console output.
Tried switching between bash and sh, exporting path to java in the pipeline script etc.
Any suggestions are appreciated.
Thanks!
p.s. Execution call from Jenkins looks like:
sh """
ssh -o StrictHostKeyChecking=no $connectionName 'bash /<pathToTheScript>/<scriptName>.sh'
"""
line 87: **usr/bin/java/bin/java**: No such file or directory
As per error line it is considering path from usr not /usr. Can you check if this is what the problem is?
Sorry, I know this should be in comments section but I don't have right to add comments yet.

Jenkins pipeline job gets triggered as anonymous but not as an user/Admin

Jenkins Pipeline job doesn't trigger pipeline job using jenkins cli. When i run jenkins as anaonymous this works, but when i create a user/admin it fails.
I have a job A which has parameters and passes the same to Pipeline Job. This is a Master-slave setup. This is how i run:
sudo java -jar /home/user/jenkins-cli.jar -s $JENKINS_URL build pipeline_job -p parameter_Name="$parameter_Name" -p parameter_Name2="$parameter2_Name"
1.) I tried using options, "-auth" , "-username -password" but doesn't work.
errors:
No such command: -auth
No such command: -ssh
2.) Another option is paste the public key in SSH section http://jenkin_url/me/configure , but still it fails
error:
java.io.IOException: Invalid PEM structure, '-----BEGIN...' missing
Is there i am missing anything ?
I Found the solution,
1.) used SSH CLI.
In my case i was using master-slave environment, connection was made using SSH keys vice-versa. In order to trigger the build using Jenkins CLI, place the SSH keys both public & private and place them in http://jenkinsURL/user/username/configure
Here username= the one used to connect the nodes.
Trigger the job as below:
java -jar /home/username/jenkins-cli.jar -s $JENKINS_URL -i /home/username/.ssh/id_rsa build JOBNAME
Note: This is one way but cloudbees doesn't encourage this approach.
2.) There is new approach i.e., using API token authentication.
Go to http://jenkinsURL/user/username/configure
Copy the API token
trigger the build as below:
java -jar /home/username/jenkins-cli.jar -s $JENKINS_URL -auth username:apitoken /home/username/.ssh/id_rsa build JOBNAME
Note: For using API token option, download the latest jar file

How to restart Jenkins in Windows

Not able to restart Jenkins manually. Already tried all below ways:
http://localhost:8080/safeRestart
Error message: Jenkins cannot restart itself as currently configured.
Manage Jenkins → Restart Safely Plugin.
Error message: Jenkins cannot restart itself as currently configured.
Not able to find any Jenkins Service in services.msc.
Navigate to jenkins-cli directory in CMD mode
java -jar jenkins-cli.jar -s http://[jenkins-server]/ restart
See below screenshot for error message:
Use command line to stop and start
net stop jenkins
net start jenkins
Simply just do one thing.To restart Jenkins manually, you can use either of the following commands on Windows platform.
Clean the "AppData\Local\Temp" folder.
In search of windows type %temp%. then clean the folder.
Then try restarting Jenkins.
use java -jar jenkins.war --httpPort=8080 in cmd fo windows.
otherwise change the Port number.
java -jar jenkins.war --httpPort=8090
It worked for me!
1) Click "Start" button
2) Find and right-click Command Prompt. Then choose Run as administrator.
3) Execute the command "net stop jenkins" and "net start jenkins"
This will definitely works.
On windows, goto Run-> type "cmd" and navigate to your jenkins installation path.
Then perform the following list of commands:
To stop the jenkins:
jenkins.exe stop
To start the jenkins:
jenkins.exe start
To restart the jenkins:
jenkins.exe restart
Note:if you get an error then run the command as administrator..
Hope this will be helpful..

Cancelling Jenkins Job from Command line (CLI)

Just like 'build' command is used to start a job from Jenkins CLI, is there a way to cancel a long running job using CLI or any other command line technique. This I am trying in a windows machine and I am not looking to directly kill the process from task manager or shut down the jenkins.
Please suggest.
You can stop a Jenkins execution by calling JOB_URL/lastBuild/stop
Jenkins built-in Command Line tool does not provide a way to cancel/stop/abort a running build. But fortunately jenkins has provided other script API like Python API and Ruby API which have such ability.
Python API
class jenkinsapi.build.Build(url, buildno, job, depth=1)
stop()
Stops the build execution if it’s running :
return boolean True if succeded
False otherwise or the build is not running
API Link:https://jenkinsapi.readthedocs.org/en/latest/build.html
Ruby API
Class: JenkinsApi::Client::Job
#stop_build(job_name, build_number = 0) ⇒ Object (also: #stop, #abort)
Stops a running build of a job This method will stop the current/most recent build if no build number is specified.
API Link: http://www.rubydoc.info/gems/jenkins_api_client/1.4.2/JenkinsApi/Client/Job
This option assumes you have access to the Jenkins installation.
In Jenkins >2.195 you can do this using the Jenkins CLI jar:
# Export JENKINS_URL or add -s http://localhost[:port]/path/to/jenkins to the commands
export JENKINS_URL=http://localhost:8080/jenkins
# Get the name of the job you want to cancel
java -jar /path/to/jenkins-cli.jar -auth user:secret list-jobs
# Cancel the job
java -jar /path/to/jenkins-cli.jar -auth user:secret stop_builds <job_name>
# You can also disable the job, if needed
java -jar /path/to/jenkins-cli.jar -auth user:secret disable_job <job_name>

Is there any way to start appium server silently?

Use case: I need to start appium server on CI Jenkins and run tests right after that. Tests don't start because appium server starting in debug mode and doesn't switch to another command.
So i have jenkins on Windows machine with the following build steps (as Windows batch command):
start /B node path_to_appium_server\appium.js --address 127.0.0.1 --port 4723
timeout 10
"path_to_tests_runner\vstest.console.exe" "path_to_dll\test.dll"
And in this case, my tests cannot started because jenkins terminate first process (with appium).
Basic issue was with permissions for '*.dll' file which contains tests and which cannot be ran with bat file without 'runas' command (which is waiting for password) from Jenkins.
So my Jenkins job contains 3 Build steps:
execute Windows batch command
start node path_to_appium_server\appium.js --address 127.0.0.1 --port 4723
Run unit tests with VSTest.console (to get this build option you need install VSTest Runner plugin)
specify path to dll and command line parameters
execute Windows batch command
taskkill /F /IM node.exe
Second step resolve permission issue for dll file.
It depends on how are you starting it. In most situations if on Jenkins you have 2 ways:
Start appium and tests in different build "shell execution" steps
If you want to do it in the same build step, just start it in a background with "START /B program".
Requirement
Installed Node.js 0.10 or greater.
At least an appium server instance installed via npm.
using javaclient 3.2.0
AppiumDriverLocalService service =AppiumDriverLocalService.buildDefaultService();
service.start();
service.stop();

Resources