25

Given I have a directory path, how do I check if this directory exists outside of the workspace from a jenkins pipeline script.

5 Answers 5

23

im not sure that that was your question but fileExists() works for directories as well

1
  • Actually this should be marked as correct answer.
    – Karsten G.
    Commented Jun 2, 2021 at 13:16
21

fileExists(target_dir) in pipeline checks the given file in workspace, it is not permitted to access the outside.

to do this, the sh or bat in pipeline can help verify if the target directory exists by calling shell command.

res = sh(script: "test -d ${target_dir} && echo '1' || echo '0' ", returnStdout: true).trim()
if(res=='1'){
    echo 'yes'
} else {
    echo 'no'
}
3
  • Unfortunately that looks like too much work if I have to check for multiple directories. In my case, I had to check for 10.
    – KatariaA
    Commented Nov 13, 2017 at 6:57
  • It can be written as a method. So it can be called multiple times Commented Dec 31, 2019 at 8:04
  • 2
    I would do sh(script: "test -d ${target_dir}", returnStatus: true) == 0 no need for echo and string matching. Commented Apr 11, 2022 at 9:34
21

Tried a few things and turned out the answer was already in front of me. fileExists can check for directories as well. Here's how I got around the problem. In this example, I am creating a directory on Windows if it doesn't exist.

Step 1: dir into the directory

dir("C:/_Tests")

Step 2: Now, use fileExists without any file name.

if(!fileExists("/"))
{
  bat "mkdir \"C:/_Tests\""
}
1
  • 12
    The dir function will create the directory for you automatically once you try to do something in it, so the fileExists can be skipped. Commented Apr 30, 2018 at 19:38
1

for anyone interested a bit more universal (windows, mac, linux and shared library...):

def listDirectories(String Directory, String Pattern) {
  def dlist = []
  new File(Directory).eachDir {dlist << it.name }
  dlist.sort()
  dlist = dlist.findAll { it.contains Pattern }

  return dlist
}

usage (for this):

if (listDirectories(Directory: "C:/", Pattern: "Windows").size() == 1) { ... }
-1

None from suggestions above worked for me...

This is 100% working code:

                        efs_path = '~/efs-mount-point'
                        stdout = sh( script: '''#!/bin/bash
                        set -ex
                        if [ -d ''' + efs_path + ''' ]; then
                            echo "true"
                        else
                            echo "false"
                        fi
                        ''', returnStdout: true)

                        echo stdout

                        if (stdout.contains('false')) {
                            error('No such directory: ' + efs_path)
                        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.