3

Recently I tried to add Shared Libraries to my Jenkins instance to make it easier to maintain all code that runs in each project.

  1. I have a repo myorg/jenkins

Inside of it I have Jenkinsfile and groovy file meant to contain libraries, Jenkinsfile looks like this:

@Library('utils@develop')_
import com.*

jenkinsSlave = "slave.myorg.com"

node(jenkinsSlave) {
    stage('Test') {
        def var = "kek"
        myorg.echoTest(var)
    }
}

I try to execute echoTest method there and I'm passing one argument.

Shared libs look like this (Jenkinsfile is in root directory of this repo):

src
└── com
    └── myorg
        └── Utils.groovy

Utils.groovy:

#!/usr/bin/groovy
package com.myorg


def echoTest(test) {
  println("Test + ${test}")
}

return this

In Jenkins I've added this repo to global libraries section and I pointed to main branch, which is develop in this case.

Unfortuantely, when I run build jenkins throws this:

groovy.lang.MissingPropertyException: No such property: myorg for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)
    at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
    at org.kohsuke.groovy.sandbox.impl.Checker.checke

[...]

I tried various configuration but so far I cannot make this work. I've tried to follow few tutorials about implementing this and I cannot pass through here.

Does someone notice any misconfiguration in this setup?

2 Answers2

1

You have not instantiated the Utils class in the myorg classpath so you cannot reference the method echoTest(). Try Utils.echoTest or just echoTest()

casey vega
  • 708
  • 7
  • 12
0

When you see scriptsecurity.sandbox error, that means you need to allow the script from Jenkins script approval page https:///scriptApproval/ Also I would recommend you not to import your package directly within your pipeline. Instead I would define a step under ./vars folder and only there import the custom package

fredericrous
  • 181
  • 2
  • 1
    Shared libraries are considered trusted and would not throw a sandbox error. See https://jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries for more details. – casey vega Apr 23 '19 at 23:14