This example demonstrates how waiting jobs can automatically acquire newly added resources.
You have a pool of resources (e.g., test devices) labeled test-device. All devices are currently in use by running jobs. A new job starts and waits for a test-device. When you add a new device to the pool, the waiting job should automatically acquire it without needing to be restarted.
pipeline {
agent any
stages {
stage('Acquire Device') {
steps {
lock(label: 'test-device', quantity: 1, variable: 'DEVICE') {
echo "Acquired device: ${env.DEVICE}"
// Use the device
sh 'run-tests.sh'
}
}
}
}
}While the job is waiting, you can add a new resource via a management job or the Script Console using the updateLock step:
// In a pipeline job
updateLock(resource: 'new-test-device-5', addLabels: 'test-device')Or via Script Console:
import org.jenkins.plugins.lockableresources.LockableResourcesManager
def manager = LockableResourcesManager.get()
manager.createResourceWithLabel('new-test-device-5', 'test-device')The waiting job will automatically acquire new-test-device-5 once it is added.
For freestyle jobs configured with Required Resources (label: test-device), the same behavior applies. When a new resource with the matching label is added, the Jenkins queue is notified and the waiting freestyle job will be dispatched.
Important: Modifying labels on existing resources does NOT trigger re-evaluation.
Only adding new resources triggers waiting jobs to re-evaluate their resource requirements. If you:
- Change labels on an existing resource (e.g., add
test-devicelabel to an existing resource) - The waiting jobs will not be notified
To work around this limitation, you can manually trigger queue refresh via Script Console:
import org.jenkins.plugins.lockableresources.LockableResourcesManager
LockableResourcesManager.get().refreshQueue()This will invalidate the cached candidates and notify both pipeline and freestyle jobs to re-evaluate available resources.
- JENKINS-46744 - Original issue requesting this behavior
- GitHub #892 - Implementation tracking issue