9

我将“Locable Resources Plugin”与 Jenkins 多管道功能一起使用:

lock('my-resource-name') {

}

我有一种情况,我需要在一个管道中锁定资源并在另一个管道中解锁它。我有解决方法,但如果可能的话,我更喜欢使用锁定插件。

我想象这样的事情

lock("my-resource-name")
..... 
unlock("my-resource-name)

它的原因

我正在实现金丝雀版本,并且通过我的管道有三种不同的路径(我考虑了其他解决方案,例如使用输入插件暂停管道 - 为我提供更多超出了这个问题的范围)。我想在评估金丝雀时锁定/暂停管道,然后在升级或回滚完成后再次解锁。省略lock声明的主体只是给了我java.lang.IllegalStateException: There is no body to invoke

4

4 回答 4

11

一种方法是:

curl -XGET https://your-jenkins/lockable-resources/reserve?resource=myresource
curl -XGET https://your-jenkins/lockable-resources/unreserve?resource=myresource

GET 不是错误,它确实是 GET。

等待锁

lock("my-resource-name") {}

我也知道 Jenkins 支持一种叫做“里程碑”的东西,显然是用来在构建之间进行通信的东西。但我不知道它是否可以用来以任何有意义的方式解决这个问题。

于 2018-07-27T11:02:03.490 回答
2

从 LockableResourcesManager 获取每个锁,然后 foreach 锁执行如果 lock.getName 匹配然后执行 lock.reset() 以释放锁

例如,一些 groovy 可以找到不属于任何构建的锁定锁并清理它们:

print "START\n"
def all_lockable_resources = org.jenkins.plugins.lockableresources.LockableResourcesManager.get().resources
all_lockable_resources.each { r->
   if (r.isLocked() || r.isReserved()) { 
        println "Lock " + r + " is locked or reserved by " + r.getBuild() + " B CARSE " + r.getLockCause()

        b = r.getBuild()

        if (b) {
           if (b.isBuilding()) { println "build:" + b + " is building" } 
           if (b.getResult().equals(null)) { println "build:" + b + " result is not in yet" }

           if ( ! b.isBuilding() && ! b.getResult().equals(null)) { 
              println "build:" + b + " is not building and result is " + b.getResult() + " yet the lock " + r + " is locked."
              println "ACTION RELEASE LOCK " + r

              println "getLockCause:" + r.getLockCause() 
              println "getDescription:" + r.getDescription() 
              println "getReservedBy:" + r.getReservedBy() 
              println "isReserved:" + r.isReserved() 
              println "isLocked:" + r.isLocked() 
              println "isQueued:" + r.isQueued() 

              //release the lock
              r.reset() 

              println "getLockCause:" + r.getLockCause() 
              println "getDescription:" + r.getDescription() 
              println "getReservedBy:" + r.getReservedBy() 
              println "isReserved:" + r.isReserved() 
              println "isLocked:" + r.isLocked() 
              println "isQueued:" + r.isQueued() 

           }
        }

   }
}

API:http: //javadoc.jenkins.io/plugin/lockable-resources/org/jenkins/plugins/lockableresources/LockableResource.html

于 2018-10-10T16:37:02.310 回答
1

锁可以这样完成

import org.jenkins.plugins.lockableresources.LockableResource
import org.jenkins.plugins.lockableresources.LockableResourcesManager

...

LockableResourcesManager lockableResourcesManager = LockableResourcesManager.get()
LockableResource lockableResource = lockableResourcesManager.fromName(resourceName)
// if the resource doesn't exists, create it
if (lockableResource == null) {
    lockableResourcesManager.createResource(resourceName)
    lockableResource = lockableResourcesManager.fromName(resourceName)
}

// wait until lock is successful
waitUntil { lockableResourcesManager.lock([lockableResource].toSet(), currentBuild.rawBuild, null) }

解锁要简单得多,因为您知道资源存在(如果不存在,则没有理由尝试解锁它)

LockableResourcesManager lockableResourcesManager = LockableResourcesManager.get()
LockableResource lockableResource = lockableResourcesManager.fromName(resourceName)
// do not pass currentBuild.rawBuild as in your case is not the build that locked this resource
// so I am passing null instead
lockableResourcesManager.unlock([lockableResource], null)

请注意,我曾经对此进行过测试org.6wind.jenkins:lockable-resources:2.7,实现细节可能会因使用的版本而异。

<dependency>
    <groupId>org.6wind.jenkins</groupId>
    <artifactId>lockable-resources</artifactId>
    <version>2.7</version>
    <scope>provided</scope>
</dependency>
于 2020-10-16T19:19:52.407 回答
0

或导航到 https://jenkins-host/lockable-resources/ 页面。在这里我们可以找到所有可锁定资源的列表。我们可以手动锁定或保留这些。

于 2021-11-10T12:42:29.047 回答