詹金斯分阶段与多个代理商

发布于 2025-01-21 12:07:11 字数 8556 浏览 2 评论 0原文

如果我想为特定阶段定义代理,我对管道有问题。

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
                    '''
                }
            }
        }
    }
}

我会收到以下错误:

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

但是,如果我在阶段外定义代理,则不会遇到任何错误。

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
                    '''
                }
            }
        }
    }
}

我已经检查了有关Jenkins管道语法的文档,但是我的代码没有任何问题。你知道我做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2025-01-28 12:07:11

您会看到此错误,因为凭据 环境块中使用的辅助方法需要node node 上下文中的上下文。您可以通过临时评论代码的这一部分来看到这一点,并且您的初始管道不会出于这个原因而失败。

您可以通过定义环境阶段内的块来解决此问题,该阶段在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
                    '''
            }
        }
    }
}

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
                    '''
            }
        }
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文