120

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.

I'm using Jenkins v 1.642 and 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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Balualways
  • 4,250
  • 10
  • 38
  • 51

2 Answers2

247

You need to use brackets when using the fileExists step in an if condition or assign the returned value to a variable

Using variable:

def exists = fileExists 'file'

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

Using brackets:

if (fileExists('file')) {
    echo 'Yes'
} else {
    echo 'No'
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Gergely Toth
  • 6,638
  • 2
  • 38
  • 40
  • 1
    def reportPath = build.getWorkspace().child("Report.txt") which one is correct? def exists = fileExists reportPath OR def exists = fileExists 'reportPath'. I'm getting a message [Could not find content token (check your usage): fileExists] in both case. – Sharon May 14 '18 at 05:08
  • 2
    I'm getting an error when I try this syntax (`fileExists` with brackets) -- "WorkflowScript: 90: Expected a step @ line 90, column 21", with a caret pointing at the `if`. – Marius Gedminas Jun 11 '18 at 11:24
  • 2
    What about file name comparison? Does this method use case sensitive comparison when comparing file names? If yes, then how it is possible to compare in case insensitive manner? – Viktors Telle Aug 06 '19 at 07:02
  • 4
    For declarative Jenkinsfile you can use fileExists(file: 'src/test/java') (eg. for an when-expression for steps in a stage) – gkephorus Oct 18 '19 at 19:07
  • 5
    @MariusGedminas The if statement cannot be at top-level within a "step" in a pipeline. Enclose the if statement within a script tag to fix this. E.g, step { script { if (condition) { doSomething() } } } – John Phu Nguyen Jan 15 '20 at 00:59
  • 1
    If I use your code (in variable) it returns always 'false'. I had to declare it like this: `def exists = fileExists ("${mypath}/${myfile}")` perhaps it's because I use variables ${} inside. – рüффп Apr 01 '20 at 12:48
  • I think it is worth noting that there is a difference when checking if file exists on a Jenkins master or agent (formerly slave). For example, consider this Q&A: https://stackoverflow.com/q/53048193/1971003 – Guy Avraham Jun 01 '21 at 10:57
15

The keyword "return" must be used

stage('some stage') {
    when { expression { return fileExists ('myfile') } }
    steps {
           echo "file exists"
          }
    }
Roman
  • 173
  • 1
  • 5
  • This worked fine at my side. I think this is a cleaner solution if one is writing pure Jenkins Pipeline DSL not using any 'script' parts. In my case I needed to test if a file does not exist so I did: when { not { expression { return fileExists ('myfile') } } } – Fholst Nov 17 '21 at 16:32
  • not working for me https://stackoverflow.com/questions/74843448/jenkins-pipeline-when-is-evaluating-to-false-though-file-exits – AhmFM Dec 18 '22 at 18:21