如何通过Groovy在Jenkins中复制/克隆任务并修改Git配置?
Copy Jenkins Job & Modify Git Polling Paths via Groovy
Absolutely feasible! You can leverage Jenkins' Groovy script API to duplicate an existing job, rename it, and tweak the "Polling ignores commits in certain paths" setting all in one go. Here's how to do it step by step:
Prerequisites
- You need sufficient Jenkins permissions (like Job/Create, Job/Configure, and Script Approval if running via the script console).
- Know the exact name of your original job, the desired name for the new job, and the text pattern you want to replace in the excluded paths.
Step-by-Step Groovy Script
This script handles copying the job, then modifying the Git path restriction for polling:
// 1. Get Jenkins instance def jenkins = Jenkins.instance // 2. Define job names and replacement pattern def originalJobName = "YourOriginalJob" def newJobName = "YourNewJob" def oldPathPattern = "path/to/ignore/old" def newPathPattern = "path/to/ignore/new" // 3. Fetch original job def originalJob = jenkins.getItem(originalJobName) if (!originalJob) { println "Error: Original job '${originalJobName}' doesn't exist!" return } // 4. Check if new job already exists def existingNewJob = jenkins.getItem(newJobName) if (existingNewJob) { println "Error: Job '${newJobName}' already exists! Please choose another name." return } // 5. Copy the original job to create the new one def newJob = jenkins.copy(originalJob, newJobName) println "Successfully copied job '${originalJobName}' to '${newJobName}'" // 6. Modify Git polling path restrictions def gitScm = newJob.scm if (!(gitScm instanceof hudson.plugins.git.GitSCM)) { println "Warning: New job '${newJobName}' doesn't use Git SCM - skipping path modification." return } // Iterate through Git extensions to find PathRestriction gitScm.extensions.each { extension -> if (extension instanceof hudson.plugins.git.extensions.impl.PathRestriction) { // Replace the old path pattern with the new one in excluded regions def updatedExcludedRegions = extension.excludedRegions.replaceAll(oldPathPattern, newPathPattern) extension.excludedRegions = updatedExcludedRegions // Optional: If you need to modify included regions, uncomment below // def updatedIncludedRegions = extension.includedRegions.replaceAll(oldIncludePattern, newIncludePattern) // extension.includedRegions = updatedIncludedRegions println "Updated excluded polling paths for '${newJobName}':\nOld: ${extension.excludedRegions}\nNew: ${updatedExcludedRegions}" } } // 7. Save the changes to the new job newJob.save() println "All changes saved successfully!"
How to Execute
- Go to Manage Jenkins > Script Console.
- Paste the script above, update the variables (
originalJobName,newJobName,oldPathPattern,newPathPattern) to match your needs. - Click Run.
Notes
- If your job uses multiple Git SCMs, the script will only modify the first one. You'd need to adjust the script to loop through all SCMs if that's your case.
- Always test the script on a non-production job first to avoid unintended changes.
- If you get permission errors, check your Jenkins security settings to ensure your user has the required permissions, and approve any script signatures if prompted.
内容的提问来源于stack exchange,提问作者James
相关产品推荐
相关产品推荐

