繁体   English   中英

Jenkins groovy插件文件未找到异常

[英]Jenkins groovy plugin file not found exception

我有一个脚本来查找所有空闲从站并使用Jenkins系统groovy创建一个文本文件。 我能够创建一个空文件,并找到所有空闲的从属服务器,如果要附加到文件“idle_slaves.list”,我得到了java.io.FileNotFoundException异常。 有人可以帮我吗?

import jenkins.*
import hudson.*
import jenkins.model.Jenkins

jenkins = jenkins.model.Jenkins

File file1 = new File(build.workspace.toString() + "/idle_slaves.list")

if(build.workspace.isRemote())
{
    channel = build.workspace.channel;
    fp = new FilePath(channel, build.workspace.toString() + "/idle_slaves.list")
} else {
    fp = new FilePath(new File(build.workspace.toString() + "/idle_slaves.list"))
}

if(fp != null)
{
    fp.write("", null); //writing to file
} 

for (node in Jenkins.instance.nodes) {
  computer = node.toComputer()
  if (computer.getChannel() == null) {
    continue
  }
if (computer.isIdle()) {
 slave = node.name.toString()
file1.append( slave )
}
}

詹金斯2.46

groovy 2.0

在此输入图像描述

实际上文件本身还没有创建:)

File file1 = new File(build.workspace.toString() + "/idle_slaves.list")

如果要创建文件,只需创建指向路径的指针,然后添加以下行

file1.createNewFile() 

还要确保您创建的文件具有权限访问权限,否则您将获得权限被拒绝。

您添加以下代码以进行确认。

// check if the file exists
        boolean exists = file.exists();
        if(exists == true)
        {
            // printing the permissions associated with the file
            println "Executable: " + file.canExecute();
            println "Readable: " + file.canRead();
            println "Writable: "+ file.canWrite();
        }
        else
        {
            println "File not found.";
        }

希望能帮助到你 :)

而不是手动读取,为什么不使用jenkins / hudson库:

https://serverfault.com/a/889954

pipeline {
    agent {label 'slave'}
    stages {
        ...
    }
}

https://stackoverflow.com/a/43111789/3957754

def jenkins = Jenkins.instance
def computers = jenkins.computers

computers.each{ 
  println "${it.displayName} ${it.hostName}"
}

https://github.com/jenkinsci/jenkins-scripts/blob/master/scriptler/disableSlaveNodeStartsWith.groovy

https://wiki.jenkins.io/display/JENKINS/Display+Information+About+Nodes

for (aSlave in hudson.model.Hudson.instance.slaves) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM