Greetings. I’ve minimized the following snippet of Scripted Pipeline groovy code:
import org.jenkinsci.plugins.docker.workflow.Docker as Docker
Docker.Image buildBuilderImage() {
sh "mkdir -vp empty"
writeFile file:'empty/Dockerfile', text:'''FROM scratch'''
def img = docker.build "jenkins-local-image:test", "empty/"
return img
}
void test() {
Docker.Image img = buildBuilderImage()
println "img is of type ${img.getClass()}"
}
pipeline {
agent any
stages {
stage('Test') {
steps {
println GroovySystem.version
test()
}
}
}
}
When put into a dummy Pipeline Script
sandbox job, and after approvals of script signatures, it works as expected :
[...]
09:48:08 #3 writing image sha256:471a1b8817eefb6569017c1a76f288e0d4e5c8476eb199485c469d0b033168bf done
09:48:08 #3 naming to docker.io/library/jenkins-local-image:test done
09:48:08 #3 DONE 0.0s
09:48:08 [Pipeline] echo
09:48:08 img is of type class org.jenkinsci.plugins.docker.workflow.Docker$Image
Now, I need to move these two functions, test()
and buildBuilderImage()
into shared library (because in reality, they’re more complex than the FROM scratch
minimal sample).
So I do have a global library already set up, and I’m moving this code to a new file vars/temporaryScript.groovy
:
# vars/temporaryScript.groovy
import org.jenkinsci.plugins.docker.workflow.Docker
Docker.Image buildBuilderImage() {
sh "mkdir -vp empty"
writeFile file:'empty/Dockerfile', text:'''FROM scratch'''
def img = docker.build "jenkins-local-image:test", "empty/"
return img
}
def test() {
Docker.Image img = buildBuilderImage()
println "img is of type ${img.getClass()}"
}
def call() {
test()
}
And then, the next test also works
pipeline {
agent any
stages {
stage('Test') {
steps {
println GroovySystem.version
temporaryScript.test()
}
}
}
}
However, what does not work is if I create a git repository with a
Jenkinsfile
like so:
# Jenkinsfile
node {
temporaryScript.test()
}
Then a job automatically appears, in an org-wide job folder, /acme-org/job/jenkinsfile-sandbox/job/main
— which if triggered, fails like so:
10:08:19 org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
10:08:19 /var/lib/jenkins/jobs/acme-org/jobs/jenkinsfile-sandbox/branches/main/builds/1/libs/3f141a404141094f72f570245bea9bd286af78ea3be4efe6ff08a2ce65e943d4/vars/temporaryScript.groovy: 3: unable to resolve class Docker.Image
10:08:19 @ line 3, column 1.
10:08:19 Docker.Image buildBuilderImage() {
10:08:19 ^
10:08:19
10:08:19 1 error
10:08:19
10:08:19 at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
10:08:19 at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:981)
10:08:19 at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:626)
10:08:19 at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575)
10:08:19 at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)
10:08:19 at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)
[...]
The error disappears if I add import org.jenkinsci.plugins.docker.workflow.Docker
into the Jenkinsfile
— but unfortunately, editing Jenkinsfiles in multiple branches over hundreds of repos is not something I can do easily here. It also doesn’t look right.
Anything I’m missing? How to fix this? (without import in the Jenkinsfile)
I am aware about java classpath — and had seen advice to check it with unable to resolve class
errors. But I’m completely lost regarding where exactly am I supposed to check those classpaths; Jenkins User Documentation is super scarce around this.
Setup details:
- Jenkins 2.387.1 (this is old; I know)
- Among all the nearly 200 plugins, I’ve got these
docker-workflow:1.26
multiple-scms:0.8
github:1.34.5
github-api:1.303-400.v35c2d8258028
github-branch-source:1677.v731f745ea_0cf