【问题标题】:Linux check if file exists Jenkins PipelineLinux 检查文件是否存在 Jenkins Pipeline
【发布时间】:2020-01-21 16:27:21
【问题描述】:

我已经尝试了所有方法,但我似乎无法检查我的系统上是否存在目录。这是我的代码

ws("${env.WORKSPACE}") {
 def test = fileExists '/proj'
                sh("ls")
                sh("echo ${test}")
                if(test) {
                sh("echo test")
                }
                else {
//this gets hit
                sh("echo not test")
                }

}

这个和这么多的组合都不起作用。

当我运行 ls 命令时,proj 目录在那里,我不明白为什么它不起作用

【问题讨论】:

  • 当你在什么目录下ls?在/?
  • fileExists '/proj' 将根据需要成功运行。其他原因可能会导致此问题。

标签: linux jenkins jenkins-pipeline jenkins-groovy


【解决方案1】:

/proj 是您要查找的文件夹,还是要检查当前工作空间内的目录是否存在?在这种情况下,它将是./proj

您也可以使用 bash 进行检查

def test = sh script: '[ -d "/proj" ]', returnStatus: true
if (test == 0) {
  println 'exist'
}

【讨论】:

    【解决方案2】:

    试试这个 - 这对我有用

        stage("test") {
            sh 'mkdir proj'
            sh 'ls'
            if (fileExists('./proj')) {
                sh 'echo found'
            }
            else {
                sh 'echo Notfound'
            } 
        }
    

    输出 -

    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (test)
    [Pipeline] sh
    20:53:45  + mkdir proj
    [Pipeline] sh
    20:53:45  + ls
    20:53:45  proj
    [Pipeline] fileExists
    [Pipeline] sh
    20:53:45  + echo found
    20:53:45  found
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] }
    [Pipeline] // timestamps
    [Pipeline] End of Pipeline
    Finished: SUCCESS
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多