如何获取Jenkins中job-dsl生成的作业列表与手动创建的作业列表?
Great question! Your approach is spot-on—since your Job DSL-generated jobs have a clear marker from the seed job, we can absolutely filter those out from the full list of jobs to get the manually created ones. Here are a few reliable methods to do this in Jenkins:
Method 1: Use the Jenkins Script Console (Groovy)
This is the most direct and flexible way, especially for larger Jenkins instances. The Job DSL plugin automatically adds a GeneratedJobProperty to every job it creates, which we can check for.
- Navigate to your Jenkins instance's Script Console (usually at
http://<your-jenkins-url>/script) - Paste this Groovy script and run it:
import jenkins.model.Jenkins import javaposse.jobdsl.plugin.GeneratedJobProperty def jenkins = Jenkins.instance def allJobs = jenkins.getAllItems(jenkins.model.Job) def manualJobs = [] allJobs.each { job -> // Check if the job has the Job DSL-generated property boolean isDslGenerated = job.getProperty(GeneratedJobProperty) != null if (!isDslGenerated) { manualJobs.add(job.fullName) } } println "Manually created jobs:\n" manualJobs.each { println "- $it" }
The script will print a list of all jobs that weren't created by Job DSL.
Method 2: Use the Jenkins REST API
If you prefer to automate this outside the Jenkins UI, you can use the REST API to fetch and filter jobs:
- First, get the full list of all jobs:
curl -s http://<your-jenkins-url>/api/json?tree=jobs[fullName]
- For each job, check if it has the
GeneratedJobProperty(you'll needjqinstalled for JSON parsing):
# Fetch all job names JOBS=$(curl -s http://<your-jenkins-url>/api/json?tree=jobs[fullName] | jq -r '.jobs[].fullName') for JOB in $JOBS; do # Encode slashes for nested jobs ENCODED_JOB=$(echo "$JOB" | sed 's/\//%2F/g') # Check for the Job DSL property HAS_DSL=$(curl -s "http://<your-jenkins-url>/job/${ENCODED_JOB}/api/json?tree=properties[class]" | jq '.properties[]?.class' | grep -q "GeneratedJobProperty" && echo 1 || echo 0) if [ $HAS_DSL -eq 0 ]; then echo "Manual job: $JOB" fi done
Add --user <username>:<password> to the curl commands if your Jenkins instance requires authentication.
Method 3: UI-Based List View (For Smaller Instances)
If you want a persistent way to view manual jobs in the UI:
- Go to your Jenkins dashboard and click New View
- Create a List View
- Under Job Filters, add a Job Property Filter
- Configure the filter to exclude jobs that have the
javaposse.jobdsl.plugin.GeneratedJobProperty - Save the view—it will now only display manually created jobs.
Notes
- Ensure you have the necessary permissions (Script Console access, API access, or view creation rights) to use these methods.
- If some of your Job DSL jobs don't have the
GeneratedJobProperty(unlikely unless the seed job was customized to omit it), you can adjust the filter to check for a custom description or marker you added instead. - All methods handle nested jobs in folders correctly, as they traverse the full job hierarchy.
内容的提问来源于stack exchange,提问作者Amerousful

