The fileExists step accepts neither wildcards, nor absolute paths.
However, if you install the optional Pipeline Utility Steps plugin, you can make use of the findFiles step, which does accept wildcards. For example:
def files = findFiles glob: '**/*.zip'
boolean exists = files.length > 0
As an alternative, without that plugin, you could use a shell step to run find:
def exitCode = sh script: 'find -name "*.zip" | egrep .', returnStatus: true
boolean exists = exitCode == 0
Answer from Christopher Orr on Stack Overflow Top answer 1 of 3
1
The fileExist step does not accept wildcards.But, if you use the ‘Pipeline Utility Steps Plugin’, you can make use of the findFiles function which accepts wildcards.An example:def files = findFiles glob: '**/*.zip'
boolean exists = files.length > 0
2 of 3
1
If you do not have the plug-in, you can use the shell "find". def exitCode = sh script: 'find -name "*.zip" | egrep .', returnStatus: true boolean exists = exitCode == 0
Videos
Jenkins
jenkins.io › doc › pipeline › steps › workflow-basic-steps
Pipeline: Basic Steps
node('Linux') { if (fileExists('/usr/local/bin/jenkins.sh')) { sh '/usr/local/bin/jenkins.sh' } } node('Windows') { if (fileExists('C:/jenkins.exe')) { bat 'C:/jenkins.exe' } } When using a Windows path with the backslash ( \ ) separator, do not ...
Baeldung
baeldung.com › home › jenkins › check if a file exists in a jenkins pipeline
Check if a File Exists in a Jenkins Pipeline | Baeldung on Ops
January 30, 2025 - We often rely on Shared Libraries in larger Jenkins setups to keep pipeline code organized. Suppose we want a standard function, checkFileExists, that can be reused across pipelines: // vars/checkFileExists.groovy in our shared library def call(String filePath) { if (fileExists(filePath)) { return true } else { return false } }
Cloudbees
support.cloudbees.com › knowledge base › pipeline with conditional stages based on a file existing on the filesystem
Pipeline with conditional stages based on a file existing on the filesystem
May 1, 2022 - Jenkins LTS · Generally this use case may not a best practise, as relying on files on a filesystem can sometimes mean you have files that are outside of source control, but there are some situations where this is valid. pipeline{ agent any environment{ MY_FILE = fileExists '/tmp/myfile' } stages{ stage('conditional if exists'){ when { expression { MY_FILE == 'true' } } steps { echo "file exists" } } stage('conditional if not exists'){ when { expression { MY_FILE == 'false' } } steps { echo "file does not exist" } } } } Pipeline: Declarative 1.3.7 Pipeline: Groovy 2.65 ·
Jenkins
issues.jenkins.io › browse › JENKINS-38855
Loading...
October 10, 2016 - 📢 Jenkins core issues have been · migrated to GitHub, no new core issues can be created in Jira · Information: This issue is archived. You can view it, but you can't modify it. Learn more · Log In · Open · Export · null · XML · Word · Printable · Type: Improvement · Resolution: Unresolved · Priority: Minor · Component/s: pipeline-utility-steps-plugin · Labels: issue-exported-to-github · Hi - it would be really nice if the fileExists() step supported file globbing: if (fileExists('foo/bar*')) { // ...
Top answer 1 of 2
269
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'
}
2 of 2
20
The keyword "return" must be used
stage('some stage') {
when { expression { return fileExists ('myfile') } }
steps {
echo "file exists"
}
}
YouTube
youtube.com › watch
How To Check if a File Exists Using Jenkins - YouTube
Need help with your Jenkins questions?Visit https://community.jenkins.io/c/using-jenkins/support/8Timecodes ⏱:00:00 Introduction00:06 Overview00:12 Starting ...
Published June 16, 2022
Top answer 1 of 2
4
Use the below:-
def exists = fileExists '/root/elp/test.php'
if (exists) {
sh "LINUX SHELL COMMAND"
} else {
println "File doesn't exist"
}
And you can follow check-if-a-file-exists-in-jenkins-pipeline
And you can also use below:-
def exitCode = sh script: 'find -name "*.zip" | egrep .', returnStatus: true
boolean exists = exitCode == 0
2 of 2
1
The following example is when using Jenkins declarative pipeline:
pipeline{
agent any
environment{
MY_FILE = fileExists '/tmp/myfile'
}
stages{
stage('conditional if exists'){
when { expression { MY_FILE == 'true' } }
steps {
echo "file exists"
}
}
stage('conditional if not exists'){
when { expression { MY_FILE == 'false' } }
steps {
echo "file does not exist"
}
}
}
}
Alternatively for scripted pipeline you may find this bash syntax to check if file exists useful.
Jenkins
community.jenkins.io › t › using-fileexists-on-symlinks › 9855
Jenkins - The leading open source automation server, Jenkins provides ...
September 27, 2023 - The leading open source automation server
Stack Overflow
stackoverflow.com › questions › 16783366 › file-exist-condition-issue-in-jenkins
File Exist condition issue in Jenkins - Stack Overflow
May 28, 2013 - I am working on product testing automation. I am using Jenkins to create a job that will first browse some file in a directory. But I got a problem with Conditional Step in File Exist condition. It is not working when I search *.job file, it only works with specific file name I put.
Stack Overflow
stackoverflow.com › questions › 59255607 › how-to-wildcard-a-filename-when-executing-a-wget-in-jenkins
How to wildcard a filename when executing a wget in Jenkins - Stack Overflow
December 9, 2019 - You may want to either look into rsync --include='*.war' --exclude="*" or stash/unstash Jenkins functionality.
Javaer101
javaer101.com › en › article › 16243134.html
Can Jenkins pipeline function fileExist handle wildcards? - Javaer101
The fileExists step accepts neither wildcards, nor absolute paths.