原来,Groovy
File命令被认为是不安全的,尽管它们将在主服务器上运行,但不会在从属服务器上运行。如果您从将代理设置为另一个节点的脚本中调用它们,它将仍然可以在主节点上而不是在代理上执行命令。这是文章发布的摘录https://support.cloudbees.com/hc/en-
us/articles/230922508-Pipeline-Files-
manipulation
File类的操作在master上运行,因此仅当build在master上运行时才起作用,在此示例中,我创建了一个文件,并检查是否可以在存在方法的节点上访问它,该文件不存在,因为
newFile(file)在主机,要对此进行检查,我将搜索
Users主机上存在但节点中不存在的文件夹。
stage 'file move wrong way' //it only works on master node('slave') { def ws = pwd() def context = ws + "/testArtifact" def file = ws + '/file' sh 'touch ' + file sh 'ls ' + ws echo 'File on node : ' + new File(file).exists() echo 'Users : ' + new File('/Users').exists() sh 'mv ' + file + ' ' + context sh 'ls ' + ws }要执行文件操作命令,我们建议使用本机命令。
这是shell中操作的简单示例
stage 'Create file' sh 'touch test.txt'stage 'download file' def out='$(pwd)/download/maven.tgz' sh 'mkdir -p ./download' sh 'curl -L http://ftp.cixug.es/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz -o ' + outstage 'move/rename' def newName = 'mvn.tgz' sh 'mkdir -p $(pwd)/other' sh 'mv ' + out + ' ' + newName sh 'cp ' + newName + ' ' + out}



