简体   繁体   English

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

[英]Check if a file exists in jenkins pipeline

I am trying to run a block if a directory exists in my jenkins workspace and the pipeline step "fileExists: Verify file exists" in workspace doesn't seem to work correctly.如果我的 jenkins 工作区中存在目录并且工作区中的管道步骤“fileExists:验证文件存在”似乎无法正常工作,我正在尝试运行一个块。

I'm using Jenkins v 1.642 and Pipeline v 2.1.我正在使用 Jenkins v 1.642 和 Pipeline v 2.1。 and trying to have a condition like并试图有这样的条件

if ( fileExists 'test1' ) {
  //Some block
}

What are the other alternatives I have within the pipeline?我在管道中还有哪些其他选择?

You need to use brackets when using the fileExists step in an if condition or assign the returned value to a variableif条件中使用fileExists步骤或将返回值分配给变量时需要使用方括号

Using variable:使用变量:

def exists = fileExists 'file'

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

Using brackets:使用括号:

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

The keyword "return" must be used必须使用关键字“return”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM