I'm having an issue with the pipeline if I want to define the agent for a specific stage.
def call(Map pipelineParams) {
pipeline {
environment {
// Environment variables for Docker
DOCKERCREDS = credentials('odyssey-artifactory-token')
COMPONENT = 'listener-publisher-service'
REGISTRY_PATH = 'com.csg.ops.it.prf'
REGISTRY = 'docker-dev.odyssey.rowini.net'
}
options {
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
}
agent none
stages {
stage('Docker') {
agent {
label 'docker'
}
steps {
sh '''
docker build -t $REGISTRY/$REGISTRY_PATH/$COMPONENT:$POM_VERSION . --force-rm=true
docker login -u $DOCKERCREDS_USR -p $DOCKERCREDS_PSW $REGISTRY
docker push $REGISTRY/$REGISTRY_PATH/$COMPONENT:$VERSION
docker rmi $REGISTRY/$REGISTRY_PATH/$COMPONENT:$VERSION
docker logout $REGISTRY
'''
}
}
}
}
}
I get the following error:
11:03:09 org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
11:03:09 Perhaps you forgot to surround the code with a step that provides this, such as: node
11:03:09 at org.jenkinsci.plugins.workflow.steps.StepDescriptor.checkContextAvailability(StepDescriptor.java:266)
11:03:09 at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:296)
11:03:09 at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
11:03:09 at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
11:03:09 at sun.reflect.GeneratedMethodAccessor294.invoke(Unknown Source)
11:03:09 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:03:09 at java.lang.reflect.Method.invoke(Method.java:498)
11:03:09 at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
11:03:09 at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
11:03:09 at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
11:03:09 at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
11:03:09 at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
11:03:09 at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
11:03:09 at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
11:03:09 at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163)
11:03:09 at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
11:03:09 at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:158)
11:03:09 at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
11:03:09 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
11:03:09 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
11:03:09 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
11:03:09 at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
11:03:09 at runJenkinsPipeline.call(runJenkinsPipeline.groovy)
11:03:09 at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:431)
11:03:09 at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.callClosureForMapEntry(CpsDefaultGroovyMethods:5226)
11:03:09 at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.collect(CpsDefaultGroovyMethods:3446)
11:03:09 at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.collect(CpsDefaultGroovyMethods:3463)
11:03:09 at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:429)
11:03:09 at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(ModelInterpreter.groovy:78)
11:03:09 at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(ModelInterpreter.groovy:476)
11:03:09 at ___cps.transform___(Native Method)
11:03:09 at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
11:03:09 at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
11:03:09 at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:78)
11:03:09 at sun.reflect.GeneratedMethodAccessor288.invoke(Unknown Source)
11:03:09 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:03:09 at java.lang.reflect.Method.invoke(Method.java:498)
11:03:09 at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
11:03:09 at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
11:03:09 at com.cloudbees.groovy.cps.Next.step(Next.java:83)
11:03:09 at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
11:03:09 at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
11:03:09 at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
11:03:09 at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
11:03:09 at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
11:03:09 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
11:03:09 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
11:03:09 at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
11:03:09 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
11:03:09 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
11:03:09 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
11:03:09 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
11:03:09 at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
11:03:09 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
11:03:09 at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
11:03:09 at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
11:03:09 at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
11:03:09 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
11:03:09 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
11:03:09 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
11:03:09 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
11:03:09 at java.lang.Thread.run(Thread.java:748)
11:03:09 Finished: FAILURE
But if I define the agent outside the stages I don't get any error.
def call(Map pipelineParams) {
pipeline {
environment {
// Environment variables for Docker
DOCKERCREDS = credentials('odyssey-artifactory-token')
COMPONENT = 'listener-publisher-service'
REGISTRY_PATH = 'com.csg.ops.it.prf'
REGISTRY = 'docker-dev.odyssey.rowini.net'
}
options {
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
}
agent {
label 'docker'
}
stages {
stage('Docker') {
steps {
sh '''
docker build -t $REGISTRY/$REGISTRY_PATH/$COMPONENT:$POM_VERSION . --force-rm=true
docker login -u $DOCKERCREDS_USR -p $DOCKERCREDS_PSW $REGISTRY
docker push $REGISTRY/$REGISTRY_PATH/$COMPONENT:$VERSION
docker rmi $REGISTRY/$REGISTRY_PATH/$COMPONENT:$VERSION
docker logout $REGISTRY
'''
}
}
}
}
}
I have checked the documentation about the Jenkins pipeline syntax, but I don't see any issue with my code. Do you have any idea what I'm doing wrong?
CodePudding user response:
You see this error because the credentials
helper method used inside the environment
block requires the node
context in a given execution context. You can see this by temporarily commenting out this part of the code and your initial pipeline will not fail for that reason.
You can solve this problem by defining environment
block inside the stage
that runs on the docker
node:
pipeline {
environment {
COMPONENT = 'listener-publisher-service'
REGISTRY_PATH = 'com.csg.ops.it.prf'
REGISTRY = 'docker-dev.odyssey.rowini.net'
}
options {
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
}
agent none
stages {
stage('Docker') {
agent {
label 'docker'
}
environment {
// Environment variables for Docker
DOCKERCREDS = credentials('odyssey-artifactory-token')
}
steps {
sh '''
docker build -t $REGISTRY/$REGISTRY_PATH/$COMPONENT:$POM_VERSION . --force-rm=true
docker login -u $DOCKERCREDS_USR -p $DOCKERCREDS_PSW $REGISTRY
docker push $REGISTRY/$REGISTRY_PATH/$COMPONENT:$VERSION
docker rmi $REGISTRY/$REGISTRY_PATH/$COMPONENT:$VERSION
docker logout $REGISTRY
'''
}
}
}
}