Jenkins Job DSL for timestamps is not working - jenkins

I am working with Jenkins Job DSL and can not seem to get a couple of the tags to work. One of those is timestamps(). In each of my jobs we have checked off the following field
Add timestamps to the Console Output
and I believe this translates to timestamps(). When I run the seed job to create the job Add timestamps to the Console Output is not checked.
My code:
wrappers {
preBuildCleanup {
deleteDirectories(false)
cleanupParameter()
}
timeout {
absolute(10)
}
timestamps()
colorizeOutput("String colorMap = 'xterm'")
}
The DSL was created with the XML Job To DSL plugin for Jenkins which added that field.
I may have misinterpreted something here but it seems pretty straight forward.
Any help would be greatly appreciated. I've google quite a bit but have not found anything that might help.
Thank you.

It required a restart of the Jenkins instance and now it is displaying as expected.

Related

How to know where a Jenkins job is created in groovy code

When a Jenkins JobDSL seed job finishes creating other jobs, it shows a list of generated jobs.
For example:
GeneratedJob{name='my_example'}
GeneratedJob{name='my_mvn-test'}
Is there a way to print out at which line and in which file a job is created?
For instance:
10: job ("${prefix}-${prodname}-${suffix}") {
11: ...
...
20: }
Here line 10 is the location of job creation.
We have a big bunch of jobs generated in different dsl/groovy src files, and those jobs don't use fixed job names in the code, so it's hard to find where they are created without knowing the source code well.
Searched for things like Job DSL API hooks, but with no luckā€¦
You can change your jobdsl code to print that information, you should be able to add println with custom messages anywhere in your code and log whatever is relevant to you.
It requires changing your code but it's a one off cost and it's worth evaluating against your current maintenance cost.
You can even add this information to the description of each job, so you don't depend on the seeder job logs.

Finding jobs generated by Job DSL plugin programmatically

We have a mix of DSL-seeded and manually created jobs on our Jenkins server.
I'd like to find all jobs NOT generated by DSL (or all generated by DSL at any time in the past).
I found no indication in job's config.xml that it was generated by DSL.
So, is it possible and how?
Well, I have the same problem as you.
I wonder how the link "Seed job" within the generated job is created. I don't see it in all of the generated jobs, either.
Unfortunately, I didn't get very far with my research.
In the script console, I listed the methods for one of my jobs (let's call it foo) :
Jenkins.instance.getItems().each {
if (it.getName() == 'foo') {
println it
it.class.getMethods().each { method ->
println method
}
}
};
However, I didn't see any methods containing jobdsl there.
I found a file $JENKINS_HOME/javaposse.jobdsl.plugin.ExecuteDslScripts.xml that contains generated job names and their seed jobs. But I don't know whether there is an official JobDSL API for reading it.
So... if you find more information, I'd be glad to know - good luck!

List jobs NOT using Discard Old Builds

Is there a way to get a list of Jenkins jobs that are NOT using the "Discard Old Builds" option?
I found a few jobs that people have created that are NOT using this plugin and I'm trying to enforce the usage of it:
How about following these steps:
Get the list of jobs : <jenkins_url>/api/json?tree=jobs[name,url]
Parse through each job config.xml <jenkins_url>/job/<job_name>/config.xml
Look for xpath=//properties/jenkins.model.BuildDiscarderProperty
There could be much more efficient ways to do it too!
You will have to use Jenkins API(I've used JSON api for e.g.) and look for the below property
"property" : [
{
"_class" : "jenkins.model.BuildDiscarderProperty"
}
],
If present it indicates that Discard old Builds is checked.
So you can iterate through all the jobs and the job that don't have this property is your culprits.
Hope this helps :)

How do I perform if/else operations in a jenkins workflow build?

I'm sure there's an easy answer for this but I was unable to find it elsewhere.
I have a Jenkins workflow job with parameters. What I want is to skip a build job depending on the value of a parameter. Something like:
if(param["MYPARAM"]){
build("jorb1")
}
build("jorb2")
Does anyone know how I'd accomplish this?
Turns out the answer is really easy. Hopefully this will help someone else. If statements do work in the DSL configuration. I guess its based on groovy (which I have zero experience with). Anyway my guess was just about right other than specifying the params incorrectly. Below is an example of checking a string parameter:
if(params["MYPARAM"]=="some_value"){
build("jorb1")
}
build("jorb2")

Can Jenkins show me the total number/percent of broken builds per month?

I have a Jenkins server that builds/tests about 50 projects. Unfortunately, some of these builds fail, but I don't have a good way to measure whether build failures are increasing or decreasing in frequency over time.
What I'd like is something along these lines:
A report that shows me, over the course of a month, how many jobs were unstable/failed
A report that says "X Days without a broken build" (kind of like at construction sites)
A "Red/Green calendar", that would show on a per-day basis whether any builds were broken
I didn't see any plugins that visualized data in any of these ways, but I'm willing to scrape the Jenkins logs to get the information. Is there a better way to see data similar to this?
I think this work pretty decent using the API. You can get all jobs from your view, then go into the job details and get the build numbers and build date. With those build numbers you can get the corresponding status. You would have to do some coding to collect and display the data, but this would be a possible way.
Another possibility would be using a Groovy script over the console in Manage Jenkins. I do not have much experience working with that feature though, but as you have access to the internal representation it should be pretty easy to get some data out of there.
Finally, the optimal solution would be to write a plugin that does the work, but this is of course also the solution that requires the most effort and know-how.
The Global Build Stats plugin might provide the reporting you're looking for.
(And if you already considered this plugin, I'm curious what problems you ran into.)
As #pushy mentions, the Groovy script console is a good tool to use for these types of statistics gathering. You can use the groovy script in the remote API as well. Here is a starting point for gathering information from all jobs matching a pattern.
def jobPattern='pattern'
Hudson.instance.getItems(Project).each {project ->
def results = [:]
if (project.name.contains(jobPattern)) {
results."$project.name" = [SUCCESS:0,UNSTABLE:0,FAILURE:0,ABORTED:0]
def build = project.getLastBuild()
while (build){
//println "$project.name;$build.id;$build.result"
results."$project.name"."$build.result" = results."$project.name"."$build.result" +1
build=build.getPreviousBuild()
}
}
results.each{name,map->
map.each{result,count->
println "$name : $result = $count"
}
}
}
"Done"
Use this as a start and modify according to your specific requirements.
Try build metric plugin along with Global Build Stat plugin.

Resources