Jenkins管道可锁定资源插件:如何基于定义的标签锁定特定资源

时间:2020-07-29 14:51:55

标签: jenkins-pipeline

我为以下资源定义了特定的标签。

Resource:Windows1
Labels:win10,64bit,firefox
Resource:Windows2
Labels:win10,32bit,chrome
Resource:Linux1
Labels:opensuse15.1,32bit,chrome
Resource:Linux2
Labels:opensuse15.1,64bit,chrome
Resource:Linux3
Labels:centos7,64bit,firefox

并在Jenkins项目中创建了以下管道:

import org.jenkins.plugins.lockableresources.LockableResourcesManager as manager
    pipeline {
        agent none
        stages {
            stage('Build') {
                agent none
                steps {
                    script {      
                      def myResources = manager.get().getResources()
                      def String myLabels = manager.get().getAllLabels()
                      def notLocked = myResources.find{rName-> 
                         manager.get().fromName(rName).with{ r-> !r.isLocked() && !r.isQueued() && myLabels.contains("opensuse15.1") && myLabels.contains("chrome") && myLabels.contains("64bit")}
                                   }  
                      if(notLocked){
                         lock(notLocked){         
                                    }
                               }
                    
                    }
                }
            
            }
        }
    }

尝试基于特定标签选择资源时,出现此错误:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkins.plugins.lockableresources.LockableResourcesManager.fromName() is applicable for argument types: (org.jenkins.plugins.lockableresources.LockableResource) values: [Windows1]
Possible solutions: fromName(java.lang.String)
            at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
...

有人遇到过类似情况并且可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

您的代码似乎假设var Application = angular.module('Ui', ['ui.bootstrap','easypiechart']); Application.controller('global', function($scope,$timeout,dateFilter,$interval) { $scope.data =1; $scope.percent = 65; $scope.options = { animate:{ duration:0, enabled:false }, barColor:'#2C3E50', scaleColor:false, lineWidth:2, lineCap:'circle' }; }); 的类型错误:它不是字符串,而是rName。 因此,不需要整个LockableResource。试试:

manager.get().fromName(rName).with{

答案 1 :(得分:0)

最适合我的代码是:

pipeline {
    agent none
    stages {
        stage('Build') {
            agent none
            steps {
                script {
                    def OS = "win10" 
                    def bit = "64bit"
                    def browser = ""
                    def manager = org.jenkins.plugins.lockableresources.LockableResourcesManager
                    def myResources = manager.get().resources
                    def myLabels = manager.get().allLabels
                    def checkLocked = myResources.find { r -> 
                            !r.isLocked() && !r.isQueued() && r.labels.contains(OS) && r.labels.contains(bit) && r.labels.contains(browser)}
                    if(checkLocked){
                        def myResource = checkLocked.toString()
                        lock(myResource){
                        println "Resource was succesfully locked"   
                        sleep 30
                                 }
                            } else {
                            println "Sorry!!! No resource is available in this moment"    
                            }

                }
            }
        
        }
    }
}

答案 2 :(得分:0)

对于随机抽取符合条件的免费资源也可以如下处理:

import org.jenkins.plugins.lockableresources.*

LockableResourcesManager manager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()

List criteria = [ '64bit', 'firefox' ]
List all = manager.resources.findAll { r ->
    !r.locked &&
    !r.reserved &&
    criteria.every{ c -> r.labels.split(',').collect{ it.toLowerCase() }.contains(c.toLowerCase()) }
  }
Collections.shuffle(all)
println (all?.first() ?: '')

顺便说一句,您可以尝试通过以下方式添加/删除测试标签:

import org.jenkins.plugins.lockableresources.*

LockableResourcesManager manager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()

// add
manager.createResourceWithLabel('marslo-test-1', 'windows,64bit,firefox')
manager.createResourceWithLabel('marslo-test-2', 'CentOS,32bit,chrome')
manager.createResourceWithLabel('marslo-test-3', 'RHEL,64bit,firefox')

// remove
(1..3).each {
  manager.resources.remove( manager.fromName("marslo-test-${it}") )
}