2

I would like to extract all jobs and group them by nodes. The goal of this is to have a list of builds to run a script and delete builds.

This is what I try to execute on the script:

def jobs = hudson.model.Hudson.instance.items
nodeName = 'YOUR_NODE_NAME'
jobs.each { job ->
  urls = []
  job.builds.each { build ->
    nodeName == build.builtOnStr && urls << build.absoluteUrl
  }
  urls && println("${job.name}\n\t${urls.sort().join('\n\t')}")
}

I'm getting an error and I have no ide why, this is the error:

groovy.lang.MissingPropertyException: No such property: builtOnStr for class: org.jenkinsci.plugins.workflow.job.WorkflowRun

Thanks in advance

Kristian
  • 21
  • 2

1 Answers1

0

I had the same issue, I solved this way:

import java.text.SimpleDateFormat
import jenkins.model.Jenkins
import hudson.model.*

SimpleDateFormat dateTimeParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
def jobs = Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)
def nodeName = build.getEnvironment(listener).get('NODE_NAME')
def daysAgo = build.getEnvironment(listener).get('DAYS_AGO').toInteger()
def limitDate = new Date().minus(daysAgo)

if (nodeName == "") {
  println("Node name can't be empty")
  return 1
}

jobs.each { job ->
  urls = []
  job.getBuilds().each { build ->
    if (build.getTime() > limitDate && build.getLog().contains(nodeName)) {
        urls << "${dateTimeParser.format(build.startTimeInMillis)} | $build.absoluteUrl | $build.result"
      }
    }
    urls && println("${job.name}\n${urls.sort().join('\n')}")
}

I couldn't find a way to gather the node label, I ended up searching it in the log

It is a modified answer from this one https://stackoverflow.com/a/30909035/5374189

omarzl
  • 589
  • 4
  • 9
  • That is missing something for sure. ` No such property: build for class: Script1` if you use Build, it will go a step forward, but not much further, listener not defined either. – pablosan Mar 16 '23 at 15:34