Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins update builder shell command using groovy

I need to update 'execute shell' command of Build section in Series of Jenkins Jobs. And i am using groovy for it. Here is starting script. Although it does not seems to update.

import hudson.model.*

for(item in Hudson.instance.items) {
    if (item.name == 'TEMP-RELEASE-UPDATE') {
        println("--- Parameters for :" + item.name)
        def branches = item.scm.getBranches()
        for (builder in item.buildersList) {
            new_command =  builder.command.replaceAll('PATTERN_1','PATTERN_2')
            builder.command = new_command
            builder.save()
        }
    }
}

It normally breaks at 'builder.command = new_command'. Can someone help to modify this script and save resultant to 'execute shell' block successfully?

Thanks

like image 869
Pandya M. Nandan Avatar asked Jun 12 '26 17:06

Pandya M. Nandan


1 Answers

builder.command = new_command breaks because builder.command is read-only.

What you need to do is to instantiate new builder instance:

new hudson.tasks.Shell(new_command)

add that to the list and remove the old one.

My full script:

jobsChanged = new java.util.ArrayList()
jobsNotChanged = new java.util.ArrayList()

for(project in Hudson.instance.items) {
  new_builder = null
  old_builder = null
  for (builder in project.buildersList) {
    if (!(builder instanceof hudson.tasks.Shell)) {
      jobsNotChanged.add "$project.name"
      continue;
    }
    new_command = builder.command.replace(SOMETHING, SOMETHINGELSE)

    new_builder = new hudson.tasks.Shell(new_command)
    old_builder = builder;
    jobsChanged.add "$project.name"
  }
  if (new_builder != null) {
    // comment out below for test run
    project.buildersList.add(new_builder)
    project.buildersList.remove(old_builder)
  }

}

println ""
println "Jobs changed"
println ""

jobsChanged.each { line -> println line }

println ""
println "Jobs not changed"
println ""

jobsNotChanged.each { line -> println line }

println ""

""
like image 130
eis Avatar answered Jun 16 '26 12:06

eis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!