You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实例重启后Jenkins可锁定资源锁残留,如何用Groovy控制台解锁?

Fixing Stuck Jenkins Locked Resources via Groovy Console

Absolutely! This is a super common headache when Jenkins restarts mid-lock—since the instance shuts down abruptly, it never gets a chance to clean up the lock state, leaving tasks stuck waiting for a lock that’s "permanently" marked as held. The Groovy Script Console is exactly the right tool to fix this.

Here’s how to resolve it step by step:

Step 1: Access the Groovy Script Console

Head to your Jenkins instance’s script console at http://your-jenkins-server-url/script (you’ll need admin-level permissions to use this tool).

Step 2: Unlock the Stuck Resource

First, you’ll need the exact name of the locked resource. If you can’t remember it, skip to the "Find Locked Resources" section below to list all resources and their statuses.

Paste this script into the console, replace YOUR_LOCK_RESOURCE_NAME with your resource’s actual name, then click "Run":

import jenkins.model.Jenkins
import org.jenkinsci.plugins.locksandlatches.LockableResourcesManager

// Update this to your stuck resource's name
def resourceName = 'YOUR_LOCK_RESOURCE_NAME'

def lockManager = Jenkins.get().getExtensionList(LockableResourcesManager.class)[0]
def targetResource = lockManager.getResource(resourceName)

if (targetResource) {
    if (targetResource.isLocked()) {
        targetResource.unlock()
        println "✅ Successfully unlocked resource: ${resourceName}"
    } else {
        println "ℹ️ Resource ${resourceName} isn't locked right now."
    }
} else {
    println "❌ Couldn't find a resource named ${resourceName}—double-check the name!"
}

Step 3: (Optional) Find All Locked Resources

If you’re unsure which resource is causing the block, run this script to list every lockable resource along with its lock status and owner (if any):

import jenkins.model.Jenkins
import org.jenkinsci.plugins.locksandlatches.LockableResourcesManager

def lockManager = Jenkins.get().getExtensionList(LockableResourcesManager.class)[0]
println "=== Lockable Resources Status ==="
lockManager.getResources().each { resource ->
    def lockStatus = resource.isLocked() ? "🔒 LOCKED by ${resource.getLockOwnerDisplayName()}" : "🔓 UNLOCKED"
    println "${resource.name}: ${lockStatus}"
}

Quick Tips

  • Double-check you’re not unlocking a resource that’s actually being used by a running task (though in your post-restart scenario, this is almost certainly a stale, orphaned lock).
  • After unlocking, most stuck tasks will automatically retry grabbing the lock and resume. If some don’t, manually trigger a re-run of those tasks.
  • As with any Jenkins script console operation, test this on a non-production instance first if you can—better safe than sorry!

内容的提问来源于stack exchange,提问作者thisguy123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:59:11