hudson CI: how to delete all jobs? - jenkins

I have about 100 jobs on my hudson CI, possible to mass delete them ?

The easiest way, IMHO, would be to use script. Go to http://your.hudson.url/script/
Delete jobs by running:
for(j in hudson.model.Hudson.theInstance.getProjects()) {
j.delete();
}
And this way gives you an option to easily use condition to filter out jobs to delete.
FOR JENKINS
Current versions (2.x):
for(j in jenkins.model.Jenkins.theInstance.getAllItems()) {
j.delete()
}
Older versions:
for(j in jenkins.model.Jenkins.getInstance().getProjects()) {
j.delete();
}

Just delete the job directories:
cd $HUDSON_HOME/jobs
rm -rf <JOB_NAME>
See: Administering Hudson

You can programmatically use the XML api (or use the JSON flavor if you prefer that):
http://your.hudson.url/api/xml?xpath=//job/name&wrapper=jobs
Returns:
<jobs>
<name>firstJob</name>
<name>secondJob</name>
<!-- etc -->
</jobs>
Now iterate over the job names and do a post request to
http://your.hudson.url/job/your.job.name/doDelete
(You can do this with any programming language you like that supports XML and HTTP)

I had similar manageability problems with a Hudson instance that was running 500+ build jobs - it was impractical to manually maintain that many jobs using the gui. However, you can provision jobs in Hudson remotely and programatically by using the CLI - which is supplied as a jar file [http://wiki.hudson-ci.org/display/HUDSON/Hudson+CLI].
The command to delete a job would be something like:
**java -jar hudson-cli.jar -s http://host:port/ delete-job jobname**
And the rest of the commands you will need are here:
**java -jar hudson-cli.jar -s http://host:port/** help
I wrapped the cli in python and created an XML file from which to hold the build configuration - then I could use this to manipulate my running instances of Hudson. This also provided the ability to 'reset' the CI instance back to a known configuration - handy if you suspect build failures were caused by manual changes in the UI or if you are using a different CI server for each environment you deploy to (ie dev, test, prod) and need to provision a new one.
This has also got me out of a few binds when badly written plugins have mangled Hudson's own XML and I've needed to rebuild my instances. Hudson is also I/O bound and for really loaded instances it is often faster to boot Hudson from scratch and populate it's configuration this way.

Related

Jenkins Execute Groovy Script Before Loading Plugins

I need to change some plugin config files before plugins are loaded. I looked into the init.groovy.d, however it seems to run Groovy scripts in that directory after plugins have been loaded and therefore would require a restart to apply. Is there a way to run Groovy scripts before Jenkins loads the plugins?
What you are requesting is not necessary. Generally, when adding plugins, they come unconfigured. Jenkins starts, loads the plugins, then you can configure via init.groovy, CasC, etc., similar to you adding via GUI (add, restart, configure).
We start w/war file, wrapper, init.groovy.d, plus a variant of the docker install_plugins.sh. Other than the war, the wrapper and wrapper.conf, install_plugins.sh and plugin list, and all the init scripts are controlled in a git repo, which we pull down.
dumping the plugins into the plugins dir, then launch jenkins.sh.
The init.groovy runs automatically after initialization and configures all system, global, tools and plugins values, as well as credentials values, also creating/configuring nodes.
NB: best to use 1 init script per section or plugin as a failure in any init script will quietly fail, effectively skipping the rest of the script.
You may need to .save() after setting most parameters via init.goovy. Perhaps that's why you did not see the changes.
If you were really paranoid, you could first invoke Hudson.instance.doQuietDown(), which effective blocks the queue (multiple init.groovy scripts execute in lexical order), do all the configurations, then invoke doCancelQuietDown(), but we'd had no issues w/o that.
This approach (init.groovy.d) works fine, but looking to switch to JCasC now that it's matured. CasC is a simpler to manage (again, using separate config files for each plugin) and read.

Update Jenkins Plugins via Artifactory

I want to update Jenkins plugin via Artifactory.
Create a remote repo named Jenkins-update
Create a local repo named jenkins-update-center
Get the update-center.json from repo Jenkins-update to local and modify the URL from 'http://updates.jenkins-ci.org/' to my own URL 'https://artifacts.xxx.com/artifactory/Jenkins-update/' in update-center.json, then put update-center.json into local repo.
#!/bin/sh
curl -L -o /tmp/update-center.json http://localhost:8081/artifactory/Jenkins-update-cache/update-center.json
sed -i 's#http://updates.jenkins-ci.org/#https://artifacts.xxx.com/artifactory/Jenkins-update/#g' /tmp/update-center.json
curl -L -uuser:pass -T /tmp/update-center.json "http://localhost:8081/artifactory/jenkins-update-center/update-center.json"
Change the default update site from 'http://updates.jenkins-ci.org/' to 'https://artifacts.xxx.com/artifactory/jenkins-update-center/update-center.json' in Jenkins
There is an error 'SHA-512 digest mismatch: expected=49a22dc23f739a76623d10128b6803f79e0489de3ded0f1d01f3dfba4557136c7f318baaf4749a7713ec4b3f56633f2ac3afc4703e87d423ede029d68f84c74d in 'update site 'default''' when I click 'check now' button.
What should I do to make Jenkins update plugins from Artifactory?
Tkx
As soon as the content of update-center.json changed you need to re-generate "signature" section of this file.
For that you need to generate your key pair (see more details in How to create a local mirror of public Jenkins update site?)
Also you may use the following proposed approach :
there is probably a better way, by having a sandbox Jenkins on a system that has access to the internet. You update the server using the UI and then you can test that updated Jenkins thoroughly. When done, you just need to copy the war and hpi files over to your 'production' Jenkins. now you have even a nice process and QA in place.
Another way is to setup a transparent https proxy between your Jenkins and Artifactory server - in that case update-center.json will not change and signature verification should work fine.
With best regards,
Dmytro Gorbunov
As of 2023-01-10 there is a problem with making a mirror of the jenkins plugins on artifactory.
Artifactory documentation decribes only how to create a mirror: https://jfrog.com/knowledge-base/how-to-configure-artifactory-as-a-mirror-for-jenkins-plugins/
But this is not a complete solution. Because this leads to the situation when every plugin shall be manually updated. Having plugins with bunch of dependencies it is huge effort.
There is a need to generate a file: update-center.json
There is an internal jenkins tool to do this: https://github.com/jenkins-infra/update-center2, but documentation is poor and contains vague statements like:
With a few modifications it could easily be used to generate your corporate update center as well.
Without clear description, what shall be done.
I tried to follow steps and completely failed. Tool require some special environment variables, which are also not documented and so on.
So as of my experience mirroring jenkins plugins on artifactory is practically not possible. And honestly spoken, I would like to be wrong here.

How can I create multiple performance test result reports on Jenkins?

I have a small problem about JMeter integration with Jenkins and I hope you can solve it. Let me explain it;
I have 3 different jmx (JMeter) files. I can run it via Windows bash command and I successfully create jtl log file for Performance plugin on Jenkins. Windows bash command is this for one of them;
cd C:\apache-jmeter-4.0\bin\ && jmeter -Jjmeter.save.saveservice.output_format=xml -n -t C:\apache-jmeter-4.0\load_tests\testrun.jmx -l C:\apache-jmeter-4.0\load_tests\logs\testrun.jtl
However, Performance plugin allows only one performance test result. I want separate graphics and reports for each test file. When add "Publish performance test result report" and type "C:\apache-jmeter-4.0\load_tests\logs\*.jtl", it combines all result. As I said I need separate test result reports.
I havent used it much with Jenkins.
But, if it allows for 1 then you can create 3 jenkin plans and execute them and all will get their individual report. Until, you have some specific dependencies between the 3 jmx plans.
Jenkins performance plugin states
If you just have one report file, the graph of this reports will appear on the main page.
If you have more than one report file, you have to click on “Performance Trend” and the graphs will appear.
So more than one report file is possible.
I found the solution. My Jenkins version is not compatible with JMeter 4.0 that's why I use JMeter 3.3 and it works perfectly. Thanks for all your answers.

How to delete a build from Jenkins job workspace

I wonder if it is possible to remove only one build (including artifacts) from job workspace.
I tried to "Delete Build" in Build History but all it does is remove build reference from Build History table. I know I can ssh to a server and delete files from the command line but I am looking for a way to do it from Jenkins web interface.
After installing Workspace Cleanup Plugin I am able to wipe out current workspace but I want to keep my other builds in the workspace.
In your Jenkins instance, to be able to have folder/per build - set flag "Use custom workspace" in your job's settings. Here is a brief help info from the setting description:
For each job on Jenkins, Jenkins allocates a unique "workspace directory."
This is the directory where the code is checked out and builds happen.
Normally you should let Jenkins allocate and clean up workspace directories,
but in several situations this is problematic, and in such case, this option
lets you specify the workspace location manually.
One such situation is where paths are hard-coded and the code needs to be
built on a specific location. While there's no doubt that such a build is
not ideal, this option allows you to get going in such a situation.
...
And your custom directory path would look like this:
workspace\$JOB_NAME\$BUILD_NUMBER ~> workspace\my-job-name\123
where $JOB_NAME will be "my-job-name" and $BUILD_NUMBER is the build number, eq. "123".
There is one nasty problem with this approach and this is why I wouldn't recommend to use it - Jenkins will not be able to reclaim disk space for outdated builds. You would have to handle cleanup of outdated builds manually and it is a lot of hassle.
Alternative approach, that gives you more control, tools and is able to keep disk space usage under control (without your supervision) is to use default workspace settings and archive your build output (files, original source code, libraries and etc.) as a post-build action. Very-very handy and gives you access to a whole bunch of great tools like, Copy Artifact Plugin or ArtifactDeployer Plugin in other jobs.
Hope that info helps you make a decision that fits your needs best.
I also use "General/Advanced/Use custom workspace" (as in #pabloduo's answer) on a Windows machine with something like:
C:\${JOB_NAME}\${BUILD_NUMBER}
Just wanted to add a solution for getting rid of the build job's workspaces.
I use Groovy Events Listener Plugin for this.
Using the plug-in's standard configuration I just use the following Groovy script:
if (event == Event.JOB_DELETED){
new File(env.WORKSPACE).deleteDir()
}
And now the custom workspace is deleted when the build job is deleted.
Just be aware that this would also delete non-custom workspaces (because the event is triggered for all jobs on your Jenkins server).

Multiple Environments in Travis CI

Travis has an easy way to test a project against different PHP versions.
Now I want to run tests for plugins. For that I wrote a script that is called in the install phase of .travis.yml which checks out the main project and moves my plugin source into the correct directory. Then the tests are run. So far so good.
Now I would like to provide two of these scripts. One that checks out the main project's current master branch and one that checks out the latest stable version. The plugin should be tested against both checkouts in completely separate test runs just like it is ran against different PHP versions.
Is there a simple way to configure this in .travis.yml?
You need to use env option:
env:
- TEST_NAME=my_test_1
- TEST_NAME=my_test_2
- TEST_NAME=my_test_3
script:
- ./test-run.sh --test-name=${TEST_NAME}
documentation (Set environment variables section)
example

Resources