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)
- CloudBees CI (CloudBees Core) on modern cloud platforms - Managed Master
- CloudBees CI (CloudBees Core) on modern cloud platforms - Operations Center
- CloudBees CI (CloudBees Core) on traditional platforms - Client Master
- CloudBees CI (CloudBees Core) on traditional platforms - Operations Center
- CloudBees Jenkins Enterprise
- CloudBees Jenkins Enterprise - Managed Master
- CloudBees Jenkins Enterprise - Operations Center
- CloudBees Jenkins Platform - Client Master
- CloudBees Jenkins Platform - Operations Center
- CloudBees Jenkins Distribution
- Jenkins LTS
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
0 Comments