0

I'm working on the groovy script to get all active multibranch pipelines and disabled multibranch pipeline list

Below is the groovy script and I was able to get this

To get the list of multibranch pipelines

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).each {multibranchpipelines->
println("multibranchpipelines Job Names: " + multibranchpipelines.getFullName())
}
To get the list of disabled multibranch pipelines
Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob).each {multibranchpipelines->
if(multibranchpipelines.isDisabled()) {
println("multibranchpipelines Job Names: " + multibranchpipelines.getFullName())
}
}

Now I want to get all the jobs list which is in multibranch pipelines active and disabled separately

I have tried multiple scripts, but still not able to get the required, can someone has ideas or groovy scripts that I can use to get this

Also, once we get the list, can I get the count of those active and disabled

And finally, I want to get the list of all jobs which exclude disabled multibranch pipelines and their branches

KNCK
  • 103
  • 2
  • 12

1 Answers1

0

I am not sure I understand 100% present what you are try to achieve but I'll try to answer. First, I suggest to look at the javadoc of each class that you need. Second,To get the list of all jobs within active multibranch pipelines, you can modify the first script as follows:

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).each { multibranchpipelines ->
    println("Active jobs within " + multibranchpipelines.getFullName())
    multibranchpipelines.getItems().each { branch ->
        branch.getAllJobs().each { job ->
            if (job.isBuildable()) {
                println("\t" + job.getFullName())
            }
        }
    }
}

To get the count of active and disabled multibranch pipelines, you can use the following scripts:

def activeCount = 0
def disabledCount = 0

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).each { multibranchpipelines ->
    if (multibranchpipelines.isDisabled()) {
        disabledCount++
    } else {
        activeCount++
    }
}

println("Active multibranch pipelines: " + activeCount)
println("Disabled multibranch pipelines: " + disabledCount)

To get the list of all jobs excluding disabled multibranch pipelines and their branches, you can modify the first script as follows:

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).findAll { !it.isDisabled() }.each { multibranchpipelines ->
    println("Active jobs within " + multibranchpipelines.getFullName())
    multibranchpipelines.getItems().each { branch ->
        if (branch.isBuildable()) {
            branch.getAllJobs().each { job ->
                if (job.isBuildable()) {
                    println("\t" + job.getFullName())
                }
            }
        }
    }
}

Note that in the last script, we are using the findAll method to filter out disabled multibranch pipelines before processing them.

Also, I didn't test the code I provided

Barel elbaz
  • 426
  • 3
  • 8