如何通过Groovy脚本获取Jenkins FreeStyleProject参数并修改指定作业参数
Update Parameters for Freestyle Jenkins Jobs with Specific Prefix
Got it, let's walk through how to use Groovy in Jenkins to target your Freestyle projects, fetch their parameters (choice param "A" and string param "B"), and update their values. Since you already know how to get job names, we'll focus on the parameter handling and bulk update part.
Step 1: Fetch Target Freestyle Jobs
First, we'll grab all Freestyle projects whose names start with your specific prefix. Replace "my-target-prefix-" with your actual prefix:
// Get the Jenkins instance Jenkins jenkins = Jenkins.get() // Define your target job prefix def targetPrefix = "my-target-prefix-" // Filter Freestyle jobs that match the prefix def targetJobs = jenkins.getAllItems(FreeStyleProject.class).findAll { job -> job.name.startsWith(targetPrefix) } if (targetJobs.isEmpty()) { println "No Freestyle jobs found with prefix: ${targetPrefix}" return }
Step 2: Iterate Jobs and Modify Parameters
Next, loop through each target job, check if it has parameter definitions, then locate and update "A" and "B":
targetJobs.each { job -> println "Processing job: ${job.name}" // Get the parameter definitions property of the job def paramProperty = job.getProperty(ParametersDefinitionProperty.class) if (!paramProperty) { println "⚠️ Job ${job.name} has no parameters defined, skipping." return } // Fetch parameter definitions def paramDefinitions = paramProperty.getParameterDefinitions() // Find choice parameter "A" and update its options def choiceParamA = paramDefinitions.find { it.name == "A" } if (choiceParamA instanceof ChoiceParameterDefinition) { // Update with your new choice options choiceParamA.setChoices(["UpdatedOption1", "UpdatedOption2", "UpdatedOption3"]) println "✅ Updated choice parameter 'A' for job ${job.name}" } else { println "⚠️ Choice parameter 'A' not found in job ${job.name}" } // Find string parameter "B" and update its default value def stringParamB = paramDefinitions.find { it.name == "B" } if (stringParamB instanceof StringParameterDefinition) { // Set your new default value here stringParamB.setDefaultValue("new-default-value-for-B") println "✅ Updated string parameter 'B' for job ${job.name}" } else { println "⚠️ String parameter 'B' not found in job ${job.name}" } // Save the job configuration to persist changes job.save() println "✅ Saved changes for job ${job.name}\n" }
Key Things to Keep in Mind
- Permissions: You'll need administrative privileges (the
Jenkins.ADMINISTERpermission) to run this script via Jenkins' Script Console. - Backup First: Always back up your Jenkins job configurations before running bulk update scripts—better safe than sorry!
- Parameter Type Checks: The script explicitly checks if "A" is a
ChoiceParameterDefinitionand "B" is aStringParameterDefinition. Adjust these checks if your parameters use different types (likeBooleanParameterDefinitionfor example).
内容的提问来源于stack exchange,提问作者ocp1000
相关产品推荐
相关产品推荐

