import hudson.matrix.* import jenkins.model.*; import com.cloudbees.hudson.plugins.folder.* Jenkins.getInstance().getAllItems().each { // MavenModule is superfluous project returned by getAllItems() if (!(it instanceof MavenModule || it instanceof MatrixConfiguration)) { println it } }
Can you help if I want to fetch count for Number of Jobs which are running as specific time interval - For example After each our i will fetch values for Number of Jobs Running
Seems as though the Disabled variable does not exist or is not loaded!
groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.job.WorkflowJob.isDisabled() is applicable for argument types: () values: []
Possible solutions: isBuildable(), isBuildable()
If I change it to "isBuildable" it shows me what looks like all enabled jobs that are able to run. Do I need to load a module to get "isDisabled" to work?
Support for disabling Pipeline jobs has been added in the Pipeline Job plugin version 2.11. So the method WorkflowJob#isDisabled() is only available since this version. Improvement captured as JENKINS-27299.
I need help on groovy. I'm looking for groovy code for stop/start Linux procedures using Jenkins. Here is my scenario, when I select "stop" and "start" choice parameters in Jenkins, respective(stop/start) shell script or command should be invoked.
My use case is similar to this topic so posting this here.
we need to scan all jobs on server and see if they have SCM configured. If job exist with scm configured, Then print job URL & SCM URL. If job doesn't have scm, no need to print anything.
I am trying to list all our jenkins (Freestyle and Pipeline) job's git (URL, branch name) and perforce (port no, workspace name, view spec) details using below groovy, But for both freestyle and pipeline i could able to list the job names. Help needed to list SCM details for each job.
The above script is not listing out all the pipelines. I'm trying to get a list of all the pipelines, which includes multibranch pipelines and declarative pipelines. Any help would be much appreciated!
Comments
19 comments
import jenkins.model.*
import hudson.model.*
Jenkins.instance.getAllItems(AbstractItem.class).each { println(it.fullName) };
Because of it doesn't show jobs in folders
There are 2 methods:
1) Recursion
import com.cloudbees.hudson.plugins.folder.*
void processFolder(Item folder) {
folder.getItems().each{
if(it instanceof Folder){
processFolder(it)
}else{
processJob(it)
}
}
}
void processJob(Item job){
println job.name
}
Jenkins.instance.getItems().each{
if(it instanceof Folder){
processFolder(it)
}else{
processJob(it)
}
}
2) Using getAllItems which is recursive by itself
import hudson.matrix.*
import jenkins.model.*;
import com.cloudbees.hudson.plugins.folder.*
Jenkins.getInstance().getAllItems().each {
// MavenModule is superfluous project returned by getAllItems()
if (!(it instanceof MavenModule || it instanceof MatrixConfiguration)) {
println it
}
}
Can you help if I want to fetch count for Number of Jobs which are running as specific time interval - For example After each our i will fetch values for Number of Jobs Running
Is there a way to only display "Disabled" jobs?
Hi
Something like this should do the job
HI Arnaud,
I am getting an exception error:
Hi Lochlainn,
Could you try isDisabled() instead ?
Hi Manuel,
I'm not sure this is what you mean, I still get an exception error:
jenkins.model.Jenkins.instance.getAllItems(jenkins.model.ParameterizedJobMixIn.ParameterizedJob.class).findAll{it -> isDisabled()}.each {it ->
println it.fullName;
}
Thanks for the help!
Could you try this simpler syntax?
Hi Lochlainn,
You can also try this:
Hi Manuel,
Seems as though the Disabled variable does not exist or is not loaded!
Lochlainn,
Support for disabling Pipeline jobs has been added in the Pipeline Job plugin version 2.11. So the method WorkflowJob#isDisabled() is only available since this version. Improvement captured as JENKINS-27299.
Thanks Allan, good to know.
We managed to get what we needed with the following:
jenkins.model.Jenkins.instance.getAllItems(jenkins.model.ParameterizedJobMixIn.ParameterizedJob.class).findAll{job -> !(job.metaClass.methods*.name.findAll{method -> method == "isDisabled"}.isEmpty()) }.findAll{job -> job.disabled}.each{job ->
def className = job.getClass();
def lastBuild = job.getLastBuild()? job.getLastBuild().number: "Never built once";
def lastBuildOnDate = job.getLastBuild()? job.getLastBuild().getTime():"Never built once"
def url = job.getUrl()
println "$url~$lastBuild~$lastBuildOnDate~$job.disabled"
}
Hi Anyone,
I need help on groovy. I'm looking for groovy code for stop/start Linux procedures using Jenkins. Here is my scenario, when I select "stop" and "start" choice parameters in Jenkins, respective(stop/start) shell script or command should be invoked.
Please let me know if you need more information.
Appreciate your responses well in advance.
Thanks!
Holy crap, could you update this article? The article doesn't work. but the comments all do. Maybe fill it with all the working code?
Hi Everybody,
My use case is similar to this topic so posting this here.
we need to scan all jobs on server and see if they have SCM configured. If job exist with scm configured, Then print job URL & SCM URL. If job doesn't have scm, no need to print anything.
I have below code to list of jobs :
--------------------------------------
Jenkins.instance.getAllItems(AbstractProject.class).each {it ->
println it.fullName;
}
----------------------------------------
and i am using below code to list SCM URL :
----------------------------------------------
Jenkins.instance.getAllItems(hudson.model.AbstractProject.class).each {it ->
scm = it.getScm()
if(scm instanceof hudson.plugins.git.GitSCM)
{
println scm.getUserRemoteConfigs()[0].getUrl()
}
}
println "Done"
----------------------------------------------
Above code is giving me both job & SCM URL separately and i am not able to map what scm URL match to what job URL.
Appreciate help !
Anyone Noone:
println (it.fullName + ": " + scm.getUserRemoteConfigs()[0].getUrl() )
Instead of what you have... will show complete job path and the scmUrl.
I am trying to list all our jenkins (Freestyle and Pipeline) job's git (URL, branch name) and perforce (port no, workspace name, view spec) details using below groovy, But for both freestyle and pipeline i could able to list the job names. Help needed to list SCM details for each job.
import hudson.triggers.*
import org.jenkinsci.plugins.workflow.job.*
def logSpec = { it, getTrigger -> String spec = getTrigger(it)?.getSpec(); if (spec) println (it.getFullName() )}
println("--- Jenkins Pipeline jobs List ---")
Jenkins.getInstance().getAllItems(WorkflowJob.class).each() { logSpec(it, {it.getSCMTrigger()}) }
println("\n--- Jenkins FreeStyle jobs List ---")
Jenkins.getInstance().getAllItems(FreeStyleProject.class).each() { logSpec(it, {it.getSCMTrigger()}) }
println '\nDone.'
@M K
The above script is not listing out all the pipelines. I'm trying to get a list of all the pipelines, which includes multibranch pipelines and declarative pipelines. Any help would be much appreciated!
Please sign in to leave a comment.