View changes to job configuration

222 views
Skip to first unread message

Jakub Bocheński

unread,
Nov 22, 2017, 10:41:51 AM11/22/17
to job-dsl-plugin
Hi,
we have seed jobs that manage 80+ jobs and it's time consuming to review actual changes after each run.

As a quick solution I'm trying to use the Job Config History plugin and some groovy magic.

1. Get a list of affected jobs via

 build
.getActions(javaposse.jobdsl.plugin.actions.GeneratedJobsBuildAction)
   
.collectMany{ action -> action.modifiedObjects.collect{ it.jobName } }
2. Use Job Config History plugin to get a diff of configuration for each job (still working on this part)
3. Show the aggregated diff

I wanted to share this in case people have better solutions to this issue.

Also it would be awesome to have support for this built into job-dsl.
If job-dsl could at least mark which jobs had their config actually changed (as opposed to just touched) that would also be very helpful.

Our end goal here is to have the ability to preview the changes *before* they are applied.

Jakub Bocheński

unread,
Nov 22, 2017, 11:35:56 AM11/22/17
to job-dsl-plugin
as 2. this is how you can get a diff output (formatted for HTML)

def action = project.getActions(hudson.plugins.jobConfigHistory.JobConfigHistoryProjectAction)[0]

action
.metaClass.getLines = { String timestamp1, String timestamp2 ->
 
     
final XmlFile configXml1 = getOldConfigXml(timestamp1);
       
final String[] configXml1Lines = configXml1.asString().split("\\n");
       
final XmlFile configXml2 = getOldConfigXml(timestamp2);
       
final String[] configXml2Lines = configXml2.asString().split("\\n");

       
final String diffAsString = getDiffAsString(configXml1.getFile(),
                configXml2
.getFile(), configXml1Lines, configXml2Lines);

       
final List<String> diffLines = Arrays.asList(diffAsString.split("\n"));
       
return getDiffLines(diffLines);
}



action
.getLines('2017-11-20_12-51-06','2017-11-20_19-35-59')

Jakub Bocheński

unread,
Nov 23, 2017, 10:39:40 AM11/23/17
to job-dsl-plugin
Here is a complete script that will print a diff for each job changed by the current build


def since = build.startTimeInMillis

println
'Since : '+new Date(since)

def jobs = build
   
.getActions(javaposse.jobdsl.plugin.actions.GeneratedJobsBuildAction)

   
.collectMany{ action -> action.modifiedObjects.collect{ it.jobName } }

   
.collect{ Jenkins.instance.itemMap[it] }


println
'Count : '+jobs.size()

def filtered = jobs
.collect { it.getAction(hudson.plugins.jobConfigHistory.JobConfigHistoryProjectAction) }
.findAll { action ->
  since
<= action.jobConfigs[0].parsedDate().time
}

println
'Filtered : '+filtered.size()


filtered
.each { action ->
 
  action
.metaClass.getDiff = { String timestamp1, String timestamp2 ->

   
         
final XmlFile configXml1 = getOldConfigXml(timestamp1);
         
final String[] configXml1Lines = configXml1.asString().split("\\n");
         
final XmlFile configXml2 = getOldConfigXml(timestamp2);
         
final String[] configXml2Lines = configXml2.asString().split("\\n");

 
          getDiffAsString
(configXml1.getFile(),
                  configXml2
.getFile(), configXml1Lines, configXml2Lines);
 
}

  println action
.getDiff(action.jobConfigs[1].date, action.jobConfigs[0].date)

}

println
''

Jakub Bocheński

unread,
Nov 23, 2017, 12:10:30 PM11/23/17
to job-dsl-plugin
You can find a version adapted to a Groovy Post Build Step here: https://gist.github.com/jakub-bochenski/66bff585a330e19a7bd5a33694059a0d
Reply all
Reply to author
Forward
0 new messages