I (https://github.com/opensearch-project/opensearch-build) have some jenkins pipelines that rely on YAML manifest files. So they load much YAML. I added classes such as Manifest into groovy code, in which I tried to load such YAML. Given that it's not trivial to add external libraries to Jenkins (os is it? how do I?). My simplest attempt was to use readYaml provided by the pipeline, like so:

      class Manifest implements Serializable {
          def data
          Manifest(steps, String filename) {
              steps.echo "this works"
              this.data = steps.readYaml(file: filename)
              // this line is never reached
          }
      }
      

      This aborts the script without error in my Jenkins. No security problem, no exception, nothing. Just bails.

          [JENKINS-67152] readYaml aborts silently

          Workaround from https://gitter.im/jenkinsci/jenkins?at=6194102fb5e111299dc02f53

          You are falling into the CPS trap - steps can only be called in CPS context, and constructors are implicitly NonCPS.

          class Manifest implements Serializable {
              final steps
              def dataStore
              def filename    Manifest(steps, String filename) {
                this.steps = steps
                 this.filename = filename
              }
              def getData(){
                  if (dataStore == null){
                     dataStore = steps.readYaml(filename)
                  }
                  return dataStore
              }
          }
          

          You can do something like that ^ - its a bit hacky but it will lazy load filename whenever you need it first - HOWEVER there is a catch, if you change directory between initialization and load, things may break
          so maybe something like this is better:

             class Manifest implements Serializable {
                  final data
                  protected Manifest(data){
                    this.data=data
                  }
                  static Manifest fromFile(steps, filename){
                      return new Manifest(steps.readYaml(filename))
                  }
               }
          

          This will load it right away and avoid the CPS trap.

          Usage : `Manifest manifest = Manifest.fromFile(this, "manifest.yaml")`

          Daniel Doubrovkine added a comment - Workaround from  https://gitter.im/jenkinsci/jenkins?at=6194102fb5e111299dc02f53 You are falling into the CPS trap - steps can only be called in CPS context, and constructors are implicitly NonCPS. class Manifest implements Serializable { final steps def dataStore def filename Manifest(steps, String filename) { this .steps = steps this .filename = filename } def getData(){ if (dataStore == null ){ dataStore = steps.readYaml(filename) } return dataStore } } You can do something like that ^ - its a bit hacky but it will lazy load filename whenever you need it first - HOWEVER there is a catch, if you change directory between initialization and load, things may break so maybe something like this is better: class Manifest implements Serializable { final data protected Manifest(data){ this .data=data } static Manifest fromFile(steps, filename){ return new Manifest(steps.readYaml(filename)) } } This will load it right away and avoid the CPS trap. Usage : `Manifest manifest = Manifest.fromFile(this, "manifest.yaml")`

          Please read https://www.jenkins.io/doc/book/pipeline/cps-method-mismatches/ to understand the behavior you are seeing. 

          For followup - read docs here: https://github.com/cloudbees/groovy-cps/blob/master/doc/

          Michael Lasevich added a comment - Please read https://www.jenkins.io/doc/book/pipeline/cps-method-mismatches/  to understand the behavior you are seeing.  For followup - read docs here:  https://github.com/cloudbees/groovy-cps/blob/master/doc/

            vjuranek vjuranek
            dblock Daniel Doubrovkine
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: