I have two jenkins declarative pipeline jobs A and B. Job A needs to have concurrent builds but Job B cannot be run when Job A is running and Job B cannot be run concurrently. I dont want to block concurrent build of Job A by locking resource using Lockable Resources plugin. How can I make this scenario work. Any ideas or suggestions?
2 Answers
There isn't a great solution to this. The "Right" solution IMO is a read/write mutex, where B
can lock for R/W (so neither A nor B can use the resource), but A
can lock only for W (so A
can continue to acquire Read locks, but B
cannot acquire its Write lock).
The best workaround I've seen so far is to manually create a large pool of lockable resources with the same label. Have A
request a lock on one resource of that label at a time, while B
locks the entire pool (note that the lock
step has a parameter quantity
). This allows the same functionality, but is a lot more arcane and doesn't scale as well (if you suddenly need 100 runs of A
concurrently but only created 50 resources, you no longer have full parallelism)
Can you have Job B monitor execution of Job A, so Job B is only triggered after Job A has completed?
Update.
I understand that part, but by having one job trigger another, he will ensure they don't run concurrently. Other solution would be adding a small pipeline to Job B, which checks whether Job A is still running:
def jenkins = Jenkins.getInstance()
def jobName = "Job A"
def job = jenkins.getItem(jobName)
if(job.isBuilding()) {
<insert your logic>
}