Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-49135

Support for reusable sharing Declarative directives

    Details

    • Similar Issues:

      Description

      I'd like to be able to share recommended defaults for options directives. My initial use case was sharing steps through shared libraries.

       

      // vars/defaultOptionsConfig.groovy
      call() {
          timestamps()
      }
      
      options {
         defaultOptionsConfig()
         ansiColor('xterm')
      }

       

      At the same time, It'd also be nice to be able to be able to shared directives generally, so I didn't want to scope the request down too far.

       

      // vars/defaultOptions.groovy
      call() {
         options {
             timestamps()
         }
      }
      
      pipeline {
      ...
         defaultOptions()
         stages {
             ...
         }
      }

       

        Attachments

          Issue Links

            Activity

            Hide
            abayer Andrew Bayer added a comment -

            So fwiw, https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/260 may end up being the beginning of a way to kinda do this. I'm doing it for a more complex proprietary use case, and it does require an actual Jenkins plugin to implement a new directive to do the replacement, but I suppose it's theoretically possible to build something off the new extension point there to provide a new directive like, say, stageFromLibrary library: "some-lib@master", method: "someMethodInLib" that would (through some more complex tooling than is available currently) go get and run that method and insert the result into the model before validation and transformation occur... We'll see. This is very early prototype stuff at this point - I threw the PR up mainly because I like to have a PR to point to when I'm doing downstream prototyping work, but I think this would open options for someone to implement tooling to satisfy the requests behind this ticket.

            Show
            abayer Andrew Bayer added a comment - So fwiw, https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/260 may end up being the beginning of a way to kinda do this. I'm doing it for a more complex proprietary use case, and it does require an actual Jenkins plugin to implement a new directive to do the replacement, but I suppose it's theoretically possible to build something off the new extension point there to provide a new directive like, say, stageFromLibrary library: "some-lib@master", method: "someMethodInLib" that would (through some more complex tooling than is available currently) go get and run that method and insert the result into the model before validation and transformation occur... We'll see. This is very early prototype stuff at this point - I threw the PR up mainly because I like to have a PR to point to when I'm doing downstream prototyping work, but I think this would open options for someone to implement tooling to satisfy the requests behind this ticket.
            Hide
            dadom85 Dominik Heim added a comment -

            This would help absolutely to clean up Jenkinsfile and make also common code possible outside of steps within stages. Really looking forward to this. 

            Show
            dadom85 Dominik Heim added a comment - This would help absolutely to clean up Jenkinsfile and make also common code possible outside of steps within stages. Really looking forward to this. 
            Hide
            dageissl Daniel Geißler added a comment -

            The linked PR is closed since Nov 2018. Have there been any other ideas or actions on this ticket?

            Show
            dageissl Daniel Geißler added a comment - The linked PR is closed since Nov 2018. Have there been any other ideas or actions on this ticket?
            Hide
            tcole Tavin Cole added a comment -

            Well if you want to mess around with Groovy AST, it does work to write something like this:

            pipeline {
                @MyAnnotation options
                ...
            }
            

            You can define @MyAnnotation just before calling the pipeline, e.g.:

            @org.codehaus.groovy.transform.GroovyASTTransformationClass(['mypkg.MyTransformation'])
            @interface MyAnnotation {}
            

            MyTransformation will need to be in a global shared library, loaded automatically or with @Library. Then you have to replace the annotated statement with valid pipeline syntax. If the transformation is written in Groovy, it must be @NonCPS, and I've noticed that any errors don't seem to be logged – Jenkins just goes on and tries to execute the untransformed pipeline, which then fails syntax validation.

            Show
            tcole Tavin Cole added a comment - Well if you want to mess around with Groovy AST, it does work to write something like this: pipeline { @MyAnnotation options ... } You can define @MyAnnotation just before calling the pipeline, e.g.: @org.codehaus.groovy.transform.GroovyASTTransformationClass([ 'mypkg.MyTransformation' ]) @ interface MyAnnotation {} MyTransformation will need to be in a global shared library, loaded automatically or with @Library . Then you have to replace the annotated statement with valid pipeline syntax. If the transformation is written in Groovy, it must be @NonCPS , and I've noticed that any errors don't seem to be logged – Jenkins just goes on and tries to execute the untransformed pipeline, which then fails syntax validation.
            Hide
            tcole Tavin Cole added a comment -

            Here is a test case which demonstrates the technique: https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/387

            Show
            tcole Tavin Cole added a comment - Here is a test case which demonstrates the technique:  https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/387

              People

              • Assignee:
                Unassigned
                Reporter:
                rpocase Robby Pocase
              • Votes:
                22 Vote for this issue
                Watchers:
                31 Start watching this issue

                Dates

                • Created:
                  Updated: