Pankaj Shinde
Pankaj Shinde

Reputation: 3689

How to list all directories from within directory in jenkins pipeline script

I want to get all directories present in particular directory from jenkins pipeline script.

How can we do this?

Upvotes: 18

Views: 43571

Answers (7)

Tsahi Elkayam
Tsahi Elkayam

Reputation: 11

def findSubFolder(String base_folder) {
    dir(base_folder) {
        findFiles().findAll { item -> item.directory }
    }
}

Upvotes: 0

ycr
ycr

Reputation: 14574

Recursively getting all the Directores within a directory.

pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
            def directories = getDirectories("$WORKSPACE")
            echo "$directories"
        }
      }
    }
  }
}

@NonCPS
def getDirectories(path) {
    def dir = new File(path)
    def dirs = []
    dir.traverse(type: groovy.io.FileType.DIRECTORIES, maxDepth: -1) { d ->
        dirs.add(d) 
    }
    return dirs
}

Upvotes: 1

xbmono
xbmono

Reputation: 2316

If you want a list of all directories under a specific directory e.g. mydir using Jenkins Utility plugin you can do this:

Assuming mydir is under the current directory:

 dir('mydir') {
   def files = findFiles() 
 
   files.each{ f -> 
      if(f.directory) {
        echo "This is directory: ${f.name} "
      }
   }
 }

Just make sure you do NOT provide glob option. Providing that makes findFiles to return file names only.

More info: https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/

Upvotes: 27

Pankaj Shinde
Pankaj Shinde

Reputation: 3689

I didn't find any plugin to list folders, so I used sh/bat script in pipeline, and also this will work irrespective of operating system.

pipeline {
  stages {
    stage('Find all fodlers from given folder') {
      steps {
        script {
                    
          def foldersList = []
                    
          def osName = isUnix() ? "UNIX" : "WINDOWS"
          echo "osName: " + osName
    
          echo ".... JENKINS_HOME: ${JENKINS_HOME}"
    
          if(isUnix()) {
            def output = sh returnStdout: true, script: "ls -l ${JENKINS_HOME} | grep ^d | awk '{print \$9}'"
            foldersList = output.tokenize('\n').collect() { it }
          } else {
            def output = bat returnStdout: true, script: "dir \"${JENKINS_HOME}\" /b /A:D"
            foldersList = output.tokenize('\n').collect() { it }
            foldersList = foldersList.drop(2)
                     
          }
          echo ".... " + foldersList
        }            
      }
    }
  }
}

Upvotes: 6

cowlinator
cowlinator

Reputation: 8777

If you just want to log them, use

sh("ls -A1 ${myDir}")

for Linux/Unix. (Note: that's a capital letter A and the number one.)

Or, use

bat("dir /B ${myDir}")

for Windows.

If you want the list of files in a variable, you'll have to use

def dirOutput = sh("ls -A1 ${myDir}", returnStdout: true)

or

def dirOutput = bat("dir /B ${myDir}", returnStdout: true)

and then parse the output.

Upvotes: 2

Lovato
Lovato

Reputation: 2310

A suggestion for the very end of Jenkinsfile:

post {
    always {
        echo '\n\n-----\nThis build process has ended.\n\nWorkspace Files:\n'
        sh 'find ${WORKSPACE} -type d -print'
    }
}

Place the find wherever you think is better. Check more alternatives at here

Upvotes: 0

Rich Duncan
Rich Duncan

Reputation: 1923

I haven't tried this, but I would look at the findFiles step provided by the Jenkins Pipeline Utility Steps Plugin and set glob to an ant-style directory patter, something like '**/*/'

Upvotes: 2

Related Questions