Skip to content

Instantly share code, notes, and snippets.

@f-steff
Last active September 23, 2019 12:19
Show Gist options
  • Save f-steff/df96a6f7f8ae54de9a437c01982f7956 to your computer and use it in GitHub Desktop.
Save f-steff/df96a6f7f8ae54de9a437c01982f7956 to your computer and use it in GitHub Desktop.
Jenkins Groovy: List folders located in a particular FTP folder.
// Created by f-steff 2019. Use and abuse as you wish.
@Grab(group='commons-net', module='commons-net', version='2.0')
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPFile
//def path="pub/"
println("Fetching reverse sorted list of ftp folders from $path");
FTPClient ftpClient = new FTPClient()
ftpClient.connect "arnold.c64.org",21
ftpClient.enterLocalPassiveMode()
ftpClient.login "anonymous", "donald@duckburg.com"
ftpClient.changeWorkingDirectory(path)
FTPFile[] entries = ftpClient.listDirectories().sort { it.getTimestamp().getTime() }.reverse()
def BuildMap = [:]
for (FTPFile directory : entries) {
BuildMap.put(path+directory.getName(),directory.getName() )
}
ftpClient.logout()
ftpClient.disconnect()
println("Done!");
BuildMap
@f-steff
Copy link
Author

f-steff commented Sep 23, 2019

List folders loacted in a particular FTP folder.

Used by Jenkins using the Active Choices Parameter plugin

FTP URL and port, as well as username and password are hardcoded in the script.
FTP path needs to be supplied in a variable.

Instructions for uploading the script.

  1. Upload script to Scriptler
  2. Approve the script to be run from jobs.
  3. Add a required parameter, named path.
  4. Add a comment, such as "List folders loacted in a particular FTP folder"
  5. A path specified must end with / - only if you want to reach root, it should be omitted.

Instructions for using the script.

  1. Select This project is parameterised
  2. Add Active Choices Parameter
  3. Add a name, such as "MySelectedFolder"
  4. Select a Scriptler Script.
  5. Select ParameterDescription
  6. Add the Parameter named "path", and specify it's value, such as "My/Folder/"
  7. Add a Description such as "This is where you select MyFolder"
  8. Select Choice Type, Filters etc. as needed. If you just want one folder, select Single Select and enable filters.

When run access the selected value through the ${MySelectedFolder} variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment