Issue
I would like to have a Jenkins Pipeline that will conditionally run specific stages depending if a file exists on the filesystem.
Environment
-
CloudBees CI (CloudBees Core) on modern cloud platforms - Managed controller
-
CloudBees CI (CloudBees Core) on modern cloud platforms - Operations Center
-
CloudBees CI (CloudBees Core) on traditional platforms - Client controller
-
CloudBees CI (CloudBees Core) on traditional platforms - Operations Center
-
CloudBees Jenkins Enterprise
-
CloudBees Jenkins Enterprise - Managed controller
-
CloudBees Jenkins Enterprise - Operations center
Resolution
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" } } } }
Tested product/plugin versions
Pipeline: Declarative 1.3.7 Pipeline: Groovy 2.65