我正在尝试管道脚本,在该脚本中,我需要打开文件并更改中的某些文本。所以我的脚本是这样的:import java.io.File
node {
stage('File settings') {
dir ('gitfile') {
dir('config') {
sh 'dir'
sh 'pwd > outFile'
curPath = readFile 'outFile'
echo "The current date is ${curPath}"
def file = new File("${curPath}/"+"const.js")
def lines = file.readLines()
println "${file} has ${lines.size()} lines of text"
println "Here is the first line: ${lines[0]}"
println "Here is the last line: ${lines[lines.size()-1]}"
}
}
}
}
但是我得到了类似的错误:
java.io.FileNotFoundException: /var/lib/jenkins/workspace/Daily/smoke/config
/const.js (No such file or directory)
但是该文件位于该位置。请让我知道为什么会发生此错误。
答案 0 :(得分:2)
您应该使用Jenkins的readFile()和writeFile()管道步骤来处理工作空间目录中的文件内容。参见https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/