Jenkins定时构建配置:分钟级任务与两小时任务互斥执行方案
Let's tackle your Jenkins configuration questions step by step—these are common scenarios, so I'll walk you through practical, actionable steps:
First, create your two jobs (I'll use Freestyle projects as an example, but the trigger logic works for Pipeline too):
- Head to your Jenkins dashboard, click New Item
- Name your first job (e.g., "Project A"), pick Freestyle project, then hit OK
- Repeat the process to create your second job (e.g., "Project B")
Now, configure the minute-by-minute trigger for one job (let's use Project A):
- Open Project A's configuration page
- Scroll down to the Build Triggers section
- Check the box for Build periodically
- In the schedule field, enter the cron expression
* * * * *- Quick cron breakdown: Each
*represents "every" for minute, hour, day of month, month, and day of week—so this runs once every minute, 24/7
- Quick cron breakdown: Each
- Scroll to the bottom and click Save
First, set up Project B's 2-hour trigger:
- Open Project B's configuration page
- Go to Build Triggers, check Build periodically
- Enter the cron expression
0 */2 * * *- Breakdown:
0means run at the 0th minute of the hour,*/2means every 2 hours, and the rest are wildcards for all days/months/weeks. This will run at 00:00, 02:00, 04:00, etc.
- Breakdown:
- Save this part first
Now, the tricky part: stopping any running builds of Project A and disabling it when Project B starts, then re-enabling it once B finishes. We'll use the Groovy Plugin (install it first via Manage Jenkins > Plugins > Available Plugins if you don't have it) to run scripts that modify Jenkins jobs:
Pre-Build Step: Stop and Disable Project A
- In Project B's configuration, scroll to the Build section
- Click Add build step > Select Execute system Groovy script
- Paste this script into the text box:
// Fetch Project A by name (use full path if it's in a folder, e.g., "MyFolder/Project A") def projectA = Jenkins.instance.getItemByFullName("Project A") // Stop any in-progress builds of Project A projectA.builds.each { build -> if (build.isBuilding()) { println "Stopping running build #${build.number} of Project A" build.doStop() } } // Disable Project A to block new builds from starting projectA.disabled = true projectA.save() println "Disabled Project A successfully"
Add Your Project B Build Steps
Next, add whatever actual work Project B needs to do (e.g., run shell commands, execute tests, deploy code) right after the Groovy script step.
Post-Build Step: Re-Enable Project A
- Scroll down to the Post-build Actions section
- Click Add post-build action > Select Execute system Groovy script
- Paste this script:
// Re-enable Project A once B's build is done def projectA = Jenkins.instance.getItemByFullName("Project A") projectA.disabled = false projectA.save() println "Re-enabled Project A successfully"
Quick Notes to Avoid Issues:
- Ensure the user running these scripts has enough permissions (you might need to adjust Jenkins' global security settings to allow script execution, or use a service account with admin-level privileges)
- Test this manually first: trigger Project B and check if Project A stops running, gets disabled, and then re-enables once B finishes
- If your jobs are in nested folders, make sure to use the full job path in
getItemByFullName
内容的提问来源于stack exchange,提问作者DeltaBluee




