Skip to content

Instantly share code, notes, and snippets.

@marcelaraujo
Created February 24, 2021 17:55
Show Gist options
  • Save marcelaraujo/992a5de5207b806123507a914a6981db to your computer and use it in GitHub Desktop.
Save marcelaraujo/992a5de5207b806123507a914a6981db to your computer and use it in GitHub Desktop.

Revisions

  1. marcelaraujo created this gist Feb 24, 2021.
    43 changes: 43 additions & 0 deletions extract-libraries-jobs.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    import java.util.regex.Pattern
    import java.util.regex.Matcher
    import jenkins.branch.BranchSource
    import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject

    Pattern p = Pattern.compile("Loading\\slibrary\\s(jenkins-(\\w+)-library(@.*)?)");

    Jenkins.instance.getAllItems(WorkflowMultiBranchProject.class).each { it ->
    if (it.fullName.contains("Maintenance")) {
    return
    }

    //def source = it.getSourcesList().get(BranchSource.class).getSource()
    //def owner = source.repoOwner
    //def repository = source.repository
    //println "${owner}/${repository}"
    //println it.getAllJobs().dump()

    def job = it.getAllJobs().find{ job -> job.asItem().name == "master" }

    Run lastBuild = job?.getLastBuild();
    if (lastBuild == null) {
    println "${it.fullName} - this job has never been run"
    //Job has never run
    return
    }

    try {
    def log = lastBuild.getLog()
    //println log.length()

    Matcher matcher = p.matcher(log);

    while(matcher.find()) {
    println matcher.group(1)
    }
    } catch(Exception e) {
    println "Error on file read action: " + e.getMessage()
    }

    }

    return