我有一个脚本来查找所有空闲的从属服务器,并使用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
答案 0 :(得分:1)
实际上文件尚未创建:)
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.";
}
希望它会有所帮助:)
答案 1 :(得分:0)
为什么不使用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) {