Lachezar Balev
Lachezar Balev

Reputation: 12041

Jenkins - export a scripted pipeline into a shared lib

We have a few similar apps that are deployed with a scripted pipeline which is basically C&P over all apps. I would like to move the whole pipeline into a Jenkins shared lib as hinted in the Jenkins docs.

So let's suppose that I have the following "pipeline" in var/standardSpringPipeline.groovy:

#!groovy

def call() {

  node {
    echo "${env.BRANCH_NAME}"
  }
}

Then - the Jenkins file:

@Library('my-jenkins-lib@master') _

standardSpringPipeline

echo "Bye!"

Unfortunately this does not work for a reason that I do not understand. The Jenkins output is similar:

> git fetch --no-tags --progress ssh://git@***.com:7999/log/my-jenkins-lib.git +refs/heads/*:refs/remotes/origin/*
Checking out Revision 28900d4ed5bcece9451655f6f1b9a41a76256629 (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 28900d4ed5bcece9451655f6f1b9a41a76256629
Commit message: "NOJIRA: ...."
 > git rev-list --no-walk 28900d4ed5bcece9451655f6f1b9a41a76256629 # timeout=10
[Pipeline] echo
Bye!
[Pipeline] End of Pipeline

Any clue why this does not work (see the output above) and what is the correct way to do that?

Upvotes: 2

Views: 220

Answers (1)

mkobit
mkobit

Reputation: 47319

For no-arg methods, you cannot use optional parenthesis. From the Groovy documentation (emphasis mine):

Method calls can omit the parentheses if there is at least one parameter and there is no ambiguity:

println 'Hello World'
def maximum = Math.max 5, 10

Parentheses are required for method calls without parameters or ambiguous method calls:

println() 
println(Math.max(5, 10))

The standardSpringPipeline behaves like a method because of how it is compiled. If you add a echo "$standardSpringPipeline" it is a bit clearer that it is a compiled class that can be invoked.

To address your issue, just add parenthesis to the call:

standardSpringPipeline() 

Upvotes: 6

Related Questions