How to configure JaCoCo Jenkins plugin through Shell or API - jenkins

Is there a way to configure the JaCoCo Jenkins Plugin coverage threshold through a shell script or API? For ex: I want to make an app to change code coverage threshold values for my Jenkins Jobs. How would I do it if I want my Jenkins instance abstracted?

Okay, turns out its a bit simple really. Plugin configurations are stored in an XML file. Global configurations in .jenkins root folder and job specific configurations in $HOME/.jenkins/jobs/{JOB_NAME}/config.xml.
Modify the config.xml file to store new configurations. This configuration file is exposed by each job at http://<SERVER>:<PORT>/jenkins/job/<JOB NAME>/config.xml. Since Jenkins loads this data at first load, you need to execute 'Reload Configuration From Disk' in Global configuration.
Since we're updating the XML from an API, you need to tell jenkins to reload configuration from an API as well. To do that, execute a shell to use jenkins_cli.jar's reload-configuration command.
Reference : Does anyone know how to reload hudson configuration without restarting?

Related

Jenkins How to check which jobs are consuming a file from Config File Provider?

There are around 3 files inside Config Files ($JENKINS_URL/configfiles/) which are consumed by approximately 500 of 2000 jobs in my Jenkins instance. I am trying to find the 500 jobs which will be impacted if I make a change on a config file. Is there any way to categorize the files and jobs?
I tried using the Configuration Slicer plugin but that was not able to provide this information. Clicking on each job and navigating through the configuration is proving to be cumbersome. Thanks.
Plugin Usage plugin is probably a more suitable tool than Configuration Slicing.
It does not scan pipeline at this time, only freestyle.
Or you can just grep the job config.xml for the plugin string plugin="config-file-provider and your Jenkinsfile for configFileProvider. Or scan w/groovy.
If you are also using Managed Scripts plugin, then also scan for managed-scripts

Is there a way we can download archived artifacts in jenkins based on build name (Create a formatted version number)

We are setting up the job to generate executable file by gathering different components (All these tagged) , We need a way to get these components based on the name of the build, I know copy artifacts will do but i would like to put this on script, Is there way (Api or something else) can download archived artifacts? once all these components present it is easy to create a installer
I have tried there are multiple curl and wget commands which accept username and password , But I need something without username and password as script runs on jenkins workspace we dont need to pass the password
If you want to interact with Jenkins via scripts there are two ways:
1. Jenkinspipeline
With Jenkinspipelines you can define the builds with Groovy scripts whereby you can use copyArtifact via the Groovy DSL. Its not actually a script its a build definition defined with a Groovy DSL. This should be the way when a Jenkins Job is gathering stuff from other Jobs.
https://jenkins.io/doc/book/pipeline/
2. Jenkins CLI
With the Jenkins CLI you can interact with Jenkins via a shell script. This should be the way when you want to gather stuff from outside of Jenkins.
https://wiki.jenkins.io/display/JENKINS/Jenkins+CLI
If Jenkins is secured then I think you will have to provide credentials when using the Jenkins CLI. With Jenkinspipelines you don't need credentials, because they are executed in Jenkins. But you need to define permissions on the Jenkins Jobs (or in the Pipelines) so that Jenkins Jobs can access Artifacts of other Jobs. (CopyArtifactPermission)

Global Jenkins script that will be executed before a build is started

I'm searching for a way to execute automatically a global configured script BEFORE a Jenkins job will be started.
My use case is, all Jenkins jobs are only allowed to start if a specific environment variable is set.
If a variable is not set, the build should be aborted.
I found the Global Post Plugin https://wiki.jenkins.io/display/JENKINS/Global+Post+Script+Plugin, i only need the oposite what this Plugin does.
Maybe there's another solution?
I needed to chmod my /data/jenkins/.npm and /data/jenkins/.sbt directories before running all my builds.
I could either add a prebuild step to every job (redundant and messy) or I could go under Manage Jenkins -> Configure System.
We have a Cloud -> Amazon EC2 configuration section with "Init script" - you can add what you want to run there on slave startup.
However, if you really want something to run something for every job (not enough to run on jenkins slave startup) then you probably don't want to manually configure it for each job.
I suggest you look into Jenkins DSL as you can define preBuildSteps section on any/all job(s) which can then reference a common snippet (eg. a shell script to run).
Partial Solution:
Take a look at the Global Pre Script plugin. This plugin is less feature-rich than the Global Post Script plugin, but it should do at least a part of what you want. It notably lacks the option to abort the build, but it is able to manipulate parameters or other preconditions that your jobs rely on. You may also be able to submit a PR to add some means of preventing the build from executing.
Some options:
Modify Global Pre Script to be able to cleanly abort the build from groovy.
Change your existing jobs to check for a precondition (manually or via script). This not the most scalable option.
Replace your existing jobs with Pipeline jobs and use Shared Libraries to bottleneck the logic. (This is what I do).
Generate your jobs using the Job DSL Plugin and enforce a pre build step in every generated job. (This is what I also do)
Limitations:
Something to keep in mind for both global plugins: neither plugin provides a proper build step. The groovy code executes on the master.
One use case that neither plugin will handle is a between-job slave cleanup/sanity check.

Jenkins: Can I Generate groovy script from existing Job?

I have a job in Jenkins and want the groovy script for the same? Is there a way I can do that?
I have created a Job using Jenkins, like add shell Command, Sync from repo,etc. Now I wish to have groovy script for the same, but I dont want to go to the trouble of writing the entire thing again. Is there something like Export to .groovy?
P.S. I am not sure of the correct tags.
You want to copy your configuration using Groovy script to create a new job or to persist.
Below Links are helpful for clone/ copy and create jobs using groovy script.
http://jenkins-ci.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-td4097412.html
Can I use Jenkins CLI or some groovy scripts to create a new job
https://wiki.jenkins-ci.org/display/JENKINS/Clone+all+projects+in+a+View
In case you want to persist your Job, always backup your resources file such as config.xml, jenkins.xml,etc..
you can recreate job from config.xml which holds all your job configuration
Check out this plugin. It seems to be appropriate for your purpose. Though it has some restrictions for plugins in your jobs that are not capable of Pipeline syntax. Anyway, it could be useful for code generation.

Jenkins Auto Reload Configuration Possible?

I have two Jenkins servers with sharing one jobs directory to make high availability.
However if one server built any of the workspace, another server will fail to built it, because they store next available built number in the memory.
So I need a plugin that can auto reload job's configuration periodically. Does anyone know about a suitable plugin?
I don't know whether this kind of plugin exists or not. But to reload job's configuration periodically, you can use a programmatically way by combining with the crontab utility.
Step 1:
Write a Groovy script reload-config to do the reload configuration.
Step 2:
java -jar jenkins-cli.jar -s http://jenkins_url/ groovy reload-config.groovy
Step 3:
Setup an crontab task to execute Step 2.

Resources