sirineBEJI
sirineBEJI

Reputation: 1220

Is it possible to include calls in Declarative Pipelines in the shared library (Jenkins)?

As you know in shared libraries in Jenkins, it is possible to call in the Jenkinsfile a content of a file in vars folder.

For example in vars folder in the shared library we can have a file named build.groovy and in the Jenkinsfile we can call it by :

build { parameter1 = "some param1" parameter2 = "some param2" }

As it is described in this section . I have no problems having groovy files and calling them with call() method in Jenkinsfile.

But I want to customize a pipeline and make it as generic as possible. So I want to call a groovy file contained in vars folder but in the same pipeline : Calling a genericStage.groovy in an other file contained in the same vars folder in the shared library.

So what I have is a groovy file in vars folder : genericStage.groovy What I have is :

Pipeline{
 agent{label myNode}

 stages{

  stage("init"){
   //steps

  }

  genericStage{
   parameter1 = "some param"
  }

 }

}

And in the genericStage :

def call(Closure body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()

stage(config.parameter1){
    steps{
        //steps
    }
}   

}

But I get the error :

Expected a stage @ line 125, column 6.
                genericStage{
    ^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)

So how to make calls in the same shared library as we do for the Jenkinsfile ?

Upvotes: 0

Views: 3147

Answers (1)

macg33zr
macg33zr

Reputation: 1805

In your shared library method defining a DSL style step like this you can either define a block of steps and script that does some common processing you need to share between jobs or stages in a job or a whole complete pipeline. So your genericStage.groovy can only contain what comes after the "// steps" comment and cannot contain the stage and steps definition like this. I do this type of library custom step quite a lot in the style you have here but without trying to define the stage / steps inside it and it works fine. What is happening for you here is the pipeline validation / parser is failing the main pipeline syntax before it even gets to handling your custom step because you don't have it wrapped in a stage and steps definition.

If you read the documentation link you have at the end there is a section on defining a complete declarative pipeline in a shared library. It says at the end "Only entire pipelines can be defined in shared libraries as of this time. This can only be done invars/*.groovy, and only in a call method. Only one Declarative Pipeline can be executed in a single build, and if you attempt to execute a second one, your build will fail as a result." This suggests that a partial pipeline like you have won't work, you should have just steps / script or a complete pipeline.

Upvotes: 1

Related Questions