实例重启后Jenkins可锁定资源锁残留,如何用Groovy控制台解锁?
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

