2

I have the Jenkins shared library with the following structure:

resources
  |-> config.yaml
  |-> projects.yaml
src
  |_ com
      |_ rathath
           |_ jenkins
                 |-> Configuration.groovy

In src/com/rathath/jenkins/Configuration.groovy , I want to read YAML files in resources directory.

I tried :

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
import hudson.FilePath
// ...
def readConfig() {
   def config = [:]
   def cwd = hudson.model.Executor.currentExecutor().getCurrentWorkspace().absolutize()
   new FilePath(cwd, 'resources').list('*.yaml').each {
     config << it.read()
   }
}       

Unfortunately, i got hudson.model.Executor.currentExecutor() is null.

I tried another way:

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
import hudson.FilePath
import groovy.transform.SourceURI
import java.nio.file.Paths

// ...
@SourceURI
URI source Uri

def readConfig() {
   def config = [:]
   def cwd = new FilePath(Paths.get(sourceUri).getParent().getParent().getParent().getParent().getParent().getParent().toFile());
   new FilePath(cwd, 'resources').list('*.yaml').each {
     config << it.read()
   }
}  

I got bigger issue,.. Jenkins was not able to load the file :

java.lang.NoClassDefFoundError: Could not initialize class com.rathath.jenkins.Configuration
   at java.io.ObjectStreamClass.hasStaticInitializer(Native Method).
.....
....
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
  • Does this answer your question? [How to load files from resources folder in Shared library without knowing their names (or number)?](https://stackoverflow.com/questions/51170409/how-to-load-files-from-resources-folder-in-shared-library-without-knowing-their) -- The upvoted answer works for me. – zett42 Apr 07 '20 at 21:16
  • My question is not duplicated. In the title of question, I said "from within a class in src directory". The question that looks like my question is answering differently,.. is answering from within the vars directory. – Abdennour TOUMI Apr 08 '20 at 03:18
  • I believe the solution can be applied to your problem, with some minor adjustments. – zett42 Apr 08 '20 at 07:40

2 Answers2

2

I guess you are calling your file src/com/rathath/jenkins/Configuration.groovy from vars/whateverPipelineFile.groovy so, when calling it, make sure you pass pipeline context and, from your class you will be able to use context.libraryResource()

Example:

Configuration.groovy

    class Configuration {
        def context


        Configuration(pipelineContext) {
            this.context = pipelineContext
        }

        def readConfig() {
            this.context.libraryResource("${PATH_OF_YOUR_FILE}")
            ...
        }
}  

/vars/myPipeline.groovy

   import com.rathath.jenkins.configuration.Configuration

   def call() {
      def configuration = new Configuration(this)
      configuration.readConfig()
      ...
   }

Documentation of libraryResource() here

OscarDOM
  • 716
  • 5
  • 15
  • Nope! I am calling it at initialization phase. the Map object ( reflects the yaml) must be available as literal Map object before using any step under vars/ – Abdennour TOUMI Apr 27 '20 at 01:41
  • Creating a new first step to take this initialization would be a solution valid for your use-case? I know it's not ideal but I think it would unblock you. – OscarDOM Apr 28 '20 at 20:08
2

It was not easy to get it working. But i did it:

In src/com/rathath/jenkins/Configuration.groovy :

@Grab(group='org.yaml', module='snakeyaml', version='1.17')
import org.yaml.snakeyaml.Yaml
import java.io.File
import groovy.transform.SourceURI
import java.nio.file.Path
import java.nio.file.Paths
import static groovy.io.FileType.*

class Configuration {

  Map config = [:]
  
  @SourceURI
  URI sourceUri

  Configuration() {
    new File(getRootLocation(), 'resources')
      .eachFileMatch(FILES, ~/.*\.yaml/) {
       this.config << yaml.load(it.text)
      }
  }
  @NonCPS
  File getRootLocation() {
    return Paths.get(sourceUri)
            .getParent() // jenkins
            .getParent() // rathath
            .getParent() // com
            .getParent() // src
            .getParent() // .
            .toFile()

  }

}
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254