Home > Back-end >  How to print Jenkin jobs details
How to print Jenkin jobs details

Time:07-15

Is there a way to list details of every jenkins job, such as the execute shell command? There is a shell script that is getting executed from two jobs, I know of one job from where it's running from. Need to find a second Jenkins job which is executing this script.

Found this link which lists all the jobs in Jenkins, this only lists the jobs but doesn't print any other details of the jobs. https://support.cloudbees.com/hc/en-us/articles/226941767-Groovy-to-list-all-jobs

CodePudding user response:

One option is to check the build logs of the Jobs. Logs typically have the command you execute. For this, you can use a script like the below.

def commandToCheck = "docker --version"

Jenkins.instance.getAllItems(Job.class).each { jobitem ->
  if(jobitem.getLastBuild() != null) {
    def log = jobitem.getLastBuild().getLog()
        if(log.contains(commandToCheck)) {
            println("The Job: "   jobitem.getName()   " has the command")
        }
  }
}

Another option is to get the Pipeline script definition and do a search there. To get the Pipeline definition you can use a script like below.

Jenkins.instance.getAllItems(Job.class).each { jobitem ->
      if(jobitem instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob) {
        if(jobitem.getDefinition() instanceof org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) {
          println("JobName "   jobitem.getName()   " || Pipeline Script: "  jobitem.getDefinition().getScript())
        }       
    }     
}
  • Related