You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Jenkins定时构建配置:分钟级任务与两小时任务互斥执行方案

Let's tackle your Jenkins configuration questions step by step—these are common scenarios, so I'll walk you through practical, actionable steps:

1. Setting Up Two Jenkins Jobs, with One Running Every Minute

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):

  1. Open Project A's configuration page
  2. Scroll down to the Build Triggers section
  3. Check the box for Build periodically
  4. 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
  5. Scroll to the bottom and click Save
2. Configuring Project B to Run Every 2 Hours, with Project A Disabled/Stopped During Execution

First, set up Project B's 2-hour trigger:

  1. Open Project B's configuration page
  2. Go to Build Triggers, check Build periodically
  3. Enter the cron expression 0 */2 * * *
    • Breakdown: 0 means run at the 0th minute of the hour, */2 means every 2 hours, and the rest are wildcards for all days/months/weeks. This will run at 00:00, 02:00, 04:00, etc.
  4. 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

  1. In Project B's configuration, scroll to the Build section
  2. Click Add build step > Select Execute system Groovy script
  3. 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

  1. Scroll down to the Post-build Actions section
  2. Click Add post-build action > Select Execute system Groovy script
  3. 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

火山引擎 最新活动