pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
echo "whoami".execute().text
script {
File f = new File('/home/jenkins/test2.txt');
f.createNewFile();
}
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
Jenkins控制台日志:(有例外:由用户Edgar Yu Running开始 在耐久性级别:MAX_SURVIVABILITY [Pipeline]节点运行 Jenkins在/ var / jenkins_home / workspace / test2 [Pipeline] {[Pipeline] stage [Pipeline] {(Build)[Pipeline] echo Building .. [Pipeline] echo 詹金斯
[Pipeline] script [Pipeline] {[Pipeline]} [Pipeline] // script [Pipeline]} [Pipeline] // stage [Pipeline] stage [Pipeline] {(Test) 阶段'测试'因早期故障而被跳过[管道]} [管道] // stage [Pipeline] stage [Pipeline] {(Deploy)Stage' Deploy'跳过 由于早期失败[Pipeline]} [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] Pipeline End java.io.IOException:Permission denied at java.io.UnixFileSystem.createFileExclusively(Native Method)at at java.io.File.createNewFile(File.java:1012)
答案 0 :(得分:6)
这是因为Jenkins没有实现Groovy本身,而是一个解释器(CPS) - https://github.com/cloudbees/groovy-cps
为了帮助解决所引入的复杂问题,我们采用了一些常见的步骤来解决创建文件等任务的问题。
要使用开箱即用的Jenkins管道步骤,请使用writeFile: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-writefile-code-write-file-to-workspace
writeFile([file: 'file.txt', text: filetxt])
如果您自己编写死机,我建议将其拆分为共享库,请注意这可能会导致需要批准的ScriptSecurity警报:
final class PipelineUtils implements Serializable {
private script=null
private static final PipelineUtils instance = new PipelineUtils()
@NonCPS
String saveFile(String filename, String text) {
String PWD = script.pwd()
String filePath = "${PWD}/${filename}"
File file = new File(filePath)
file.text = text
}
}
有关@NonCPS和非可序列化对象的信息,请参阅https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md。