检查詹金斯管道中是否存在文件

Check if a file exists in jenkins pipeline

如果我的 jenkins 工作区和管道步骤中存在目录,我正在尝试 运行 一个块 "fileExists: Verify file exists" 在工作区中似乎无法正常工作。

我使用的是 Jenkins v 1.642 和 Pipeline v 2.1。并试图有一个像

这样的条件
if ( fileExists 'test1' ) {
  //Some block
}

我还有哪些备选方案?

if条件中使用fileExists步骤或将返回值赋给变量时需要使用括号

使用变量:

def exists = fileExists 'file'

if (exists) {
    echo 'Yes'
} else {
    echo 'No'
}

使用括号:

if (fileExists('file')) {
    echo 'Yes'
} else {
    echo 'No'
}

必须使用关键字“return”

stage('some stage') {
    when { expression { return fileExists ('myfile') } }
    steps {
           echo "file exists"
          }
    }