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?

 Answers

4

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
 
zdav
 
5

Using java.io.File:

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

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 http://docs.jquery.com/Ajax/ajaxComplete#callback

Test here http://pastebin.me/48f32a74927bb

e.g

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

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

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: 
Scripts not permitted to use method java.net.InetAddress getHostAddress
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:178)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor$6.reject(SandboxInterceptor.java:243)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:363)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)

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

Wednesday, November 30, 2022
1

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.

skipDefaultCheckout

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 {
        skipDefaultCheckout()
    }
    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
 
emanuel
 
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 :