groovy在mac slave sh命令上执行

时间:2019-02-27 10:21:27

标签: jenkins groovy sh

我有jenkins groovy脚本,并且使用此代码准备了一个jenkins文件 我的代码在节点上搜索具有可接受任务的奴隶,然后在iphone上寻找奴隶,但是由于某种原因,我的执行中出现错误。

这一行是问题之一:如果我在常规操作中执行命令,该命令就可以正常工作,但是由于某种原因,我得到了很长的stacktrace错误。

  

commit = sh(returnStdout:true,脚本:“ echo $(system_profiler   SPUSBDataType | grep iPhone)“)。split();

错误的开始:

an exception which occurred:
    in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@2ce111
    in field com.cloudbees.groovy.cps.impl.CallEnv.caller

这是我的代码:

//build grrovy DevSecOps architecture 
node('master'){
    // adds job parameters within jenkinsfile
    properties([
        parameters([
            choice(choices: ["iOS", "Android"].join("\n"),
            description: 'choice operation system', 
            name: 'OS'
            ),
            choice(choices: ["RenameLocationTest", "ExampleTest"].join("\n"),
            description: 'choice test to run', 
            name: 'tests'
            ),
            choice(choices: ["master"].join("\n"),
            description: 'choice branch from git', 
            name: 'Branch'
            ),
        ])
    ])
    //add to build description the params data to future use (sort)
    currentBuild.description =  params.OS + " " + params.tests + " " + params.Branch;

    //Locate fit slaves with the right configuration for specific test
    //def arrayLocateSlavesForJob = //matchedJobs();
    def jenkins = Jenkins.instance
    def computers = jenkins.computers
    def arrayLocateSlavesForJob = [];
    computers.each{ 
        println "${it.displayName} ${it.hostName}";
        arrayLocateSlavesForJob.push("${it.displayName}");// "${it.displayName} ${it.hostName}"
    }
    //find available nodes that are online and can accept task
    def arrayOnlineAcceptableSlaves = availableSlaves(arrayLocateSlavesForJob); 

    //group the array to erase duplicate results.
    def arrayOnlineAcceptableSlavesGroup = arrayOnlineAcceptableSlaves.groupBy{ it }.collect{ it.key }
    chooseSlaveWithDeviceToRun(arrayOnlineAcceptableSlavesGroup);

}

def matchedJobs(){
    //use paramater to nerrow the machines slaves have labels that can cross with job label
    //collect all fit nodes(slaves) to list and return the result
    Jenkins.instance.items.findAll{ job ->
        job.name =~ job.getAssignedLabel() != null
        }.collect { job ->
        "${job.getAssignedLabel().getNodes().collect({it.getNodeName()}).join(',')}\n"
        }.each { slaves ->
        return slaves
    }
}

def availableSlaves(arrayLocateSlavesForJob){
    //take a array of slaves and nerrow the slaves to only online and acceptable task.
    def arrSplit = "${arrayLocateSlavesForJob}".split(',') as List;
    def arrayList = [];
    for (int i=0;i<arrSplit.size(); i++) {
        def format = arrSplit.getAt(i).replaceAll("\\[|\\]","").trim()
        def slave = Jenkins.instance.slaves.find({it.name == format});
        if (slave != null)
        {
            computer = slave.getComputer();
            if (computer.isOffline())
            {
                println "Error! is offline.";
            }
            else
            {
                println "OK: is online";
                if(computer.isAcceptingTasks())
                {
                    println "in isAcceptingTasks";
                    def index = i;
                    String format2 = slave.getNodeName();
                    arrayList.push(format2);
                }
            }
        }
    }
    return arrayList;
}
def chooseSlaveWithDeviceToRun(arrayOnlineAcceptableSlavesGroup){
    if(params.OS == "iOS"){
        def slaveName = checkIOSDevicesOnSlave(arrayOnlineAcceptableSlavesGroup);
        if("${slaveName}" != "false"){
            checkout(slaveName);
            runJob(slaveName);
        }
    }
}
def checkIOSDevicesOnSlave(arrayOnlineAcceptableSlavesGroup){
    //check if iOS device connect to slave 
        def val = "false";
        println "before check on slave iphone: " + arrayOnlineAcceptableSlavesGroup;

        //for(slaveName in arrayOnlineAcceptableSlavesGroup){
            def result = action(arrayOnlineAcceptableSlavesGroup[0]);//slaveName);
            if(result != "false"){
                return result;
            }
        //}

}
def action(slaveName){
    node(slaveName) {
        stage('check device on slave'){
            def val = "false";

            def commit = "false";
            commit = sh (returnStdout: true, script: "echo $(system_profiler SPUSBDataType | grep iPhone)").split();
            println "iOS on slave: " + slaveName + "${commit}";
            if("${commit}" == "[]")
            {
                val = "false";
            }
            val =  slaveName;
            return val;

        } 
    }
}
def checkout(slvaeName){
    //clean folder before pull to folder the project.
    stage("Checkout"){
        node(slaveName){
            deleteDir();
            git branch: "remotes/origin/${params.Branch}", currentBuild: "38dfcd28-5204-41d8-b345-f500d6a4754a", url: "https://github.com/Automation/POC";
        }
    }
}
def runJob(slaveName){
    //run gradle command on slave, clean old build and sent parameters to build operation
    stage('run job') {
        node(slaveName){
            sh "gradle clean test --tests '*'${params.tests}  -DOS=${params.OS} -i";
        }
    }
}

0 个答案:

没有答案
相关问题