Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins pipeline: can't pass build parameters to shared library vars

Basically I can't pass build properties to Library var call without extra nonsense.

jenkinsfile relevant chunk:

tc_test{
    repo = 'test1'
    folder = 'test2'
    submodules = true
    refs = params.GitCheckout
}

That results in error

java.lang.NullPointerException: Cannot get property 'GitCheckout' on null object

This, however, works:

def a1 = params.GitCheckout
tc_test{
    repo = 'test1'
    folder = 'test2'
    submodules = true
    refs = a1
}

The contents of the vars/tc_test.groovy in shared library :

def call ( body ) {

    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    try {
        body()
    } catch(e) {
        currentBuild.result = "FAILURE";
        throw e;
    } finally {

        config.each{ k, v -> println "${k}:${v}" }

    }
}

I'm not really good with groovy, so it might be something obvious.

like image 930
Alexei Kiryanov Avatar asked Jun 20 '17 17:06

Alexei Kiryanov


People also ask

How does Jenkins shared library work?

What is a Shared Library in Jenkins? A shared library is a collection of independent Groovy scripts which you pull into your Jenkinsfile at runtime. The best part is, the Library can be stored, like everything else, in a Git repository. This means you can version, tag, and do all the cool stuff you're used to with Git.

What is Multibranch pipeline in Jenkins?

What's a Jenkins Multibranch Pipeline? A multibranch job is simply a folder of pipeline jobs. For every branch you have, Jenkins will create a folder. So instead of creating a pipeline job for each of the branches you have in a git repository, you could use a multibranch job.


1 Answers

Got the answer from Jenkins JIRA.

Small workaround is using maps instead of closures:

tc_test ([
  repo: 'test1',
  folder: 'test2',
  submodules: true,
  refs = params.GitCheckout
])

May have drawbacks, but for me that worked perfectly.

Still have to transfer params as argument to have access to them, but at least the code makes more sense now.

like image 191
Alexei Kiryanov Avatar answered Sep 22 '22 02:09

Alexei Kiryanov