I'm in the process of migrating Jenkins from one server to another. I've no issues with the migration process.
But sooner I start my new server the scheduled jobs start executing, which is proving to be dangerous. I need to make sure that everything is in place before activating the new server.
Is there any way to deter any of the jobs from executing while the new server is active?
execute an '/script':
Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob.class).each {i -> i.setDisabled(true); i.save() }
Jenkins.instance.getAllItems(hudson.model.AbstractProject.class).each {i -> i.setDisabled(true); i.save() }
Not my idea, from jenkins wiki
Setup a post-initialization script that puts Jenkins into quiet mode right after startup.
https://wiki.jenkins.io/display/JENKINS/Post-initialization+script
Try using https://wiki.jenkins.io/display/JENKINS/Exclusive+Execution+Plugin. You can keep jenkins in shutdown or Quiet mode for some time till your new instance is ready to function.
Use the Jenkins CLI
To prevent any jobs from being run, use quiet-down:
java -jar jenkins-cli.jar -s http://localhost:9090 -auth user:token quiet-down
To re-enable job scheduling:
java -jar jenkins-cli.jar -s http://localhost:9090 -auth user:token cancel-quiet-down
Scheduled jobs will be added to the queue during the quiet-down time, and will be run after canceling the quiet-down. If that's not what you want, you may use clear-queue before canceling the quiet-down.
There is a little downside: in the GUI, Jenkins will announce that it is preparing for shutdown, which wouldn't be true in this case. I find that acceptable, because we use it during backup at night when no one will read the announcement anyway. However, another option would be to take nodes offline, then online again using offline-node and online-node.
Quick Setup
Only if you haven't set up Jenkins CLI already:
You can obtain the Jenkins CLI from your Jenkins server by downloading it from <your_jenkins_url>/jnlpJars/jenkins-cli.jar
Instead of using your actual password to authenticate, obtain a token from <your_jenkins_url>/me/configure
For more details, refer to the Jenkins Handbook: Jenkins CLI
Referencie: https://xanderx.com/post/cancel-all-queued-jenkins-jobs/
Run this in Manage Jenkins > Script Console:
Jenkins.instance.queue.clear()
Related
I am building Jenkins for Test / QA automation scripts, lets name it TEST_JOB. For application, I have application source code Jenkins build, name it DEV_JOB.
My scenario is when DEV_JOB completes execution (successfully), execute TEST_JOB immediately. I am aware about setting up project upstream / downstream [ Build after other projects are built ] to accomplish this task. But here, Problem is DEV_JOB is on different server than TEST_JOB. Due to which, TEST_JOB fails to recognize DEV_JOB.
Now, how would I achieve this scenario?
You can use Jenkins API for remote trigger of Job.
Say you have job on DEV_JOB on JENKINS_1, add a penultimate step(or upstream/downstream project having only this step) which invokes TEST_JOB using remote API call of JENKINS_2 server.
Example command would be
$(curl --user "username:password" "http://JENKINS_2/job/TEST_JOB/buildWithParameters?SOMEPARAMETER=$SOMEPARAMETER")
username:password is a valid user on JENKINS_2.
Avoid using your own account here but rather a 'build trigger' account that only has permissions to start those jobs.
Actually, the main issue is if I start the server then my next commond will never trigger as it always running as zap server in listening mode.
Can I run two command line in Jenkins. I have added 2 "Execute Windows batch command" still nothing works. I have added the image in same thread
I have tried by creating a batch file
cd /
cd C:\Program Files\OWASP\Zed Attack Proxy
start java -jar zap-2.6.0.jar
I am getting error as below after using above batch file
Process leaked file descriptors. See https://jenkins.io/redirect/troubleshooting/process-leaked-file-descriptors for more information
https://wiki.jenkins.io/display/JENKINS/Spawning+processes+from+build
I have also use command line arugument directly in "Execute window batch command" like:-
java -jar zap-2.6.0.jar
But the UI of zap is not starting
I have also tried "Windows Exe Runner Plugin"
https://wiki.jenkins.io/display/JENKINS/Windows+Exe+Runner+Plugin
But jenkins not allowing me to put an exe name in configuration. Looks like a bug of jenkins.
I have also tried by adding zap in environmental variable but that also not working.
Now I am out of idea.
The issue is if I am triggering zap.bat it will do not allow another command to run forward as below which is in my batch:-
Additionally, UI of zap is not open as it is open after direct clicking on zap.bat file
I have added 2 "Execute Windows batch command" still nothing works
Any suggestions will be welcome
Simple - dont start it from the jar!
Start it using the zap.sh or zap.bat scripts we provide as part of the installation :) You'll also probably want to use the -daemon flag.
Or you can use the official ZAP jenkins plugin: https://wiki.jenkins.io/display/JENKINS/zap+plugin
I have resolve this issue by creating two jobs in jenkins.
The main job trigger the first job.
Follow the steps :-
Go to the configure section of main job
Now add "Trigger/call builds on other projects" from Build option
Add the project name of zombie job
Note :- uncheck the checkbox of "Block until the triggered projects finish their builds".
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>
I am running a background process through a script , this script is invoked when Jenkin starts building. However, the jenkins build gets stuck and on looking at the console it seems it is running the process and waits for it to complete.
This process will never complete, consider this as a server listening to its client.Every build I trigger kills the server process and restarts the process, so I am perfectly handling that scenario.
Is there any way , I can build jenkins successfully?
The exact details depend on your operating system (which you did not tell), but the Jenkins wiki has a page about this: https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
There is a trick you can do in order you free a Jenkins thread.
What you can do is to execute a bash script through a ssh connection and send it to the background while saving the pid of the process somewhere so you can make checks further.
The format of the command would be:
ssh -n _hostname_ "_commands_ & echo \$! > \"_path_to_pid_file_\"" &
Example with a never-ending program:
ssh -n myhost.domain.com "tail -f /var/log/my.log & echo \$! > \"$WORKSPACE/pid\"" &
This example will spawn the tail process listening forever for new changes in the /var/log/my.log file and store its pid in the $WORKSPACE/pid file.
When executed from a Jenkins job the ssh process will exit immediately while the commands sent to the background will remain in execution in the specified host.
I do this in order to maintain always one of the services I run in my build farm in-sync with the latest code modification of it in the repository.
Just have a job that ssh' into the target machine and then kill the process, update the service and re-launches it.
This could be a bit cumbersome but it works great!
I'm planning to write a script that would choose a set of Jenkins jobs with a regexp, start their builds and wait until they finish. If most of them fail, I want to change some global env vars (I already know how to do this) and build them again. Then I want to collect the test results and format it into a nice report.
I can't put this into the individual jobs' post-build actions, I need info about all of them to write the report and to do the rebuilding.
My current idea is to use the Jenkins REST API, but before I do so: is this already implemented somewhere?
Give a look at Multi-Job plugin and see if it fits your requirement.
Else go with REST API.
Because unique requirement of our build process I had to use jenkins cli :
java -jar jenkins-cli.jar -s http://<jenkinsURL> build <job-name> -s <parameters if any>
The trailing -s makes it wait till the job is complete.