0

I have several yaml files in my /resources directory and I would like to get a list of ALL of them.

I've set a shared library and I'm able to get a specific file by using libraryResource: libraryResource("some_file.yaml")

How can get a list of all yaml files from this path?

HagaiA
  • 193
  • 3
  • 15
  • maybe https://stackoverflow.com/questions/51170409/how-to-load-files-from-resources-folder-in-shared-library-without-knowing-their – Hadock Apr 22 '21 at 20:44
  • 1
    There is no supported way to do this. Workarounds like the linked one may or may not work in your scenario. The most robust way is to ZIP the files so you can use `libraryResource` to get the ZIP file, then use [`unzip`](https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#unzip-extract-zip-file) step to extract it. Another way would be to maintain a text file that contains the relative paths of the YAML files. – zett42 Apr 23 '21 at 15:50

1 Answers1

0

In order to solve this I used a simple git checkout instead of using the libraryResource, as follow:

node {
    checkout([$class: 'GitSCM', branches: [[name: "$branch_name"]], userRemoteConfigs: [[url: "git@github.com:${your_username}/${your_repo_name}.git"]]])
    
    ls_command_output = sh(script: 'ls -1 resources', returnStdout: true).trim()

    list_of_jobs = ls_command_output.split('\n').collect{it.trim().replaceAll(".yaml", "")}        
}

In words:

  1. Checkout to the desired Github repo
  2. Run a ls command to get all files
  3. Created a list of files (without the ".yaml" suffix)

If you use a declarative pipeline, you can run the above just before your pipeline and use the list_of_jobs variable inside it.

HagaiA
  • 193
  • 3
  • 15