如何一次性为Jenkins多个任务添加特定构建后操作?含插件咨询
Hey there, let's break down how to add post-build actions to multiple Jenkins jobs at once—specifically addressing your need to add the "Deploy to container" step to 23 jobs. Here are the most reliable approaches:
1. 使用专门的批量配置插件(最适合你的场景)
If you want a GUI-based, no-code solution, the Configuration Slicing Plugin is perfect for this. It's built explicitly for bulk-editing Jenkins job configurations.
Step 1: Install the plugin
Go to Manage Jenkins > Plugins > Available plugins, search for "Configuration Slicing Plugin", install it, and restart Jenkins if prompted.Step 2: Bulk add the "Deploy to container" action
- From the Jenkins homepage, click the Configuration Slicing link in the left sidebar.
- In the "Slice Type" dropdown, select Post-build Actions.
- Use the job selector to pick all 23 jobs you need to update—you can filter by name, view, or tag to make this faster.
- In the slicing configuration panel, click Add post-build action and choose "Deploy to container".
- Fill in all required deployment details (like container server address, credentials, war file path, etc.) just as you would for a single job.
- Save the changes, and the plugin will automatically apply this new post-build action to every selected job.
2. 使用Groovy脚本(灵活定制选项)
If you prefer code-based control or can't install plugins, Jenkins' built-in Script Console lets you write Groovy scripts to batch-modify jobs.
Here's a sample script tailored to adding the "Deploy to container" action:
// Replace this list with your 23 job names, or use a regex to match them def targetJobs = ["Job-01", "Job-02", "...", "Job-23"] // Configure the Deploy to container action details def deployPublisher = new hudson.plugins.deploy.DeployPublisher() deployPublisher.war = "**/*.war" // Adjust to your artifact path deployPublisher.containers = [new hudson.plugins.deploy.tomcat.Tomcat8xAdapter("My-Tomcat-Server")] // Match your container type deployPublisher.credentialsId = "tomcat-deploy-creds" // Replace with your credential ID // Iterate through jobs and add the action targetJobs.each { jobName -> def job = Jenkins.instance.getItem(jobName) if (!job) { println "Job ${jobName} not found—skipping" return } // Avoid adding duplicate actions def hasExistingDeploy = job.publishersList.any { it instanceof hudson.plugins.deploy.DeployPublisher } if (hasExistingDeploy) { println "Job ${jobName} already has Deploy to container action—skipping" return } job.publishersList.add(deployPublisher) job.save() println "Successfully added Deploy action to ${jobName}" }
- Note: The class names (like
hudson.plugins.deploy.DeployPublisher) might vary slightly based on your version of the "Deploy to container" plugin. If you hit errors, you can validate the correct class path via the Script Console usingJenkins.instance.pluginManager.plugins.find { it.shortName == 'deploy' }.classLoader.loadClass('hudson.plugins.deploy.DeployPublisher').
Pro Tip
If your 23 jobs share a common prefix, belong to the same view, or have matching tags, both the plugin and script can leverage that to select jobs in bulk—no need to list each one manually.
内容的提问来源于stack exchange,提问作者user7827445

