M. Antony
M. Antony

Reputation: 161

Jenkins Pipeline Exit if nothing in the folder

I'm looking for a way to tell Jenkins in a stage that if there is no file in a particular folder he will cancel the job and mark it as unstable.

Could someone possibly help me? I think the whole thing can be solved with an if else query.

stage('Building') {
    if nothing in the folder {
        exit
        echo '[FAILURE] Failed to build'
        currentBuild.result = 'FAILURE'
    }
}

Upvotes: 2

Views: 5671

Answers (2)

towel
towel

Reputation: 2214

This is a cross-platform method to check whether a directory is empty or not (you will need the pipeline utility steps plugin).

Be aware that this function cannot be used when multiple executors try to access the same directory on the same node simultaneously, as the dir step will create a new entry for the other executors to avoid workspace sharing.

def directoryEmpty(String directory) {
    dir(directory) {
        !findFiles().any()
    }
}

Upvotes: 4

etlsh
etlsh

Reputation: 701

You can try the following :

  def isDirEmpty() {
    def myDirectory = sh(script: "ls", returnStdout: true).trim()
    println(myDirectory)
    return null == myDirectory || "".equals(myDirectory)
  }

using the sh is the safest way of finding this thing out (from my experience)

Upvotes: 3

Related Questions