Viewed   4.1k times

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?



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'
Wednesday, October 5, 2022


File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { 
    // do something
Saturday, December 24, 2022

You can use the ajaxComplete event, whis gives you access to the xhr object which you can query the status of the request e.g a status of 404 will mean the file does not exist.

More Info in the docs

Test here


$("#someDivId").ajaxComplete(function(request, settings){
    if (settings.status===404){
        //redirect here
Thursday, October 27, 2022

Not sure if there are better ways to do this, I am able to run

def masterIP = InetAddress.localHost.hostAddress
println "Master located at ${masterIP}"

in my Jenkinsfile. The first time I ran this code in my Jenkinsfile, the build failed with

Scripts not permitted to use method getHostAddress
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor$6.reject(
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(
at org.kohsuke.groovy.sandbox.impl.Checker$
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(

I had to approve the method signature in Jenkins by navigating to Manage Jenkins > In-process Script Approval.

Wednesday, November 30, 2022

As already mentioned in the comments the way to go is to use skipDefaultCheckout() (Source) in your pipeline-options to not checkout the repository if the pipeline starts.


Skip checking out code from source control by default in the agent directive.

To get the repository manually you can use checkout scm (Source)

pipeline {
    agent any
    options {
    stages {
        stage('Example') {
            steps {
                // Cleanup before starting the stage
                // deleteDir() / cleanWs() or your own way of cleaning up

                // Checkout the repository
                checkout scm 

                // do whatever you like
Tuesday, August 23, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :