为了检查 Jenkins 工作区中是否存在文件,可以使用以下方法:
example.txt
是否存在:if [ -f example.txt ]; then
echo "File exists"
else
echo "File does not exist"
fi
如果文件存在,将输出 "File exists",否则输出 "File does not exist"。可以根据需要进行相应的处理。
pipeline {
agent any
stages {
stage('Check File') {
steps {
script {
def fileExists = fileExists('example.txt')
if (fileExists) {
echo "File exists"
} else {
echo "File does not exist"
}
}
}
}
}
}
def fileExists(filePath) {
def file = new File(filePath)
return file.exists()
}
在上述示例中,通过调用 fileExists
函数来检查文件是否存在,并根据结果输出相应的消息。
请注意,上述方法只是示例,实际应根据你的需求和具体的环境来选择适合的方法。在实际应用中,你可能还需要考虑文件的路径、权限等因素。
领取专属 10元无门槛券
手把手带您无忧上云