Jenkins:groovy.lang.MissingPropertyException:无此属性:App for class:javaposse.jobdsl.dsl.helpers.workflow.CpsContext

iih3973s  于 2023-04-19  发布在  Jenkins
关注(0)|答案(1)|浏览(143)

我有一个奇怪的问题。我正在使用Jenkins CasC管道在启动时自动填充作业。一切都很好,直到我开始添加参数。运行从作业添加的以下代码失败了第一次,然后,当我随后重新运行时,它可以工作。我发现的一个论坛指出,运行Groovy代码需要注册参数,这就是为什么从第二次构建开始工作的原因。它还指出,通过CasC加载这个第一次错误不是一个问题。通过CasC加载但是产生下面的错误。请注意,当从代码中保存并具有第一次错误时,确切的代码工作正常。

错误:

hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: app for class: WorkflowScript
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:471)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:19)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:395)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:393)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:665)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:288)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:544)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:543)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:276)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:443)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:442)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:275)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(ModelInterpreter.groovy:481)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(ModelInterpreter.groovy:480)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:274)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(ModelInterpreter.groovy:595)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript.checkoutAndRun(CheckoutScript.groovy:64)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript.doCheckout(CheckoutScript.groovy:40)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript.run(LabelScript.groovy:43)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:73)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:65)
    at jdk.internal.reflect.GeneratedMethodAccessor288.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:152)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:146)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:146)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:187)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:420)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
    at jenkins.util.ErrorLoggingExecutorService.lambda$wrap$0(ErrorLoggingExecutorService.java:51)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE

Jenkins版本:2.387.1
插件:
配置为代码:1569.vb_72405b_80249
config as code groovy extension:1.1
流水线:596.v8c21c963d92d
流水线槽:3653.v07ea_433c90b_4
管道groovy库:629.vb_5627b_ee2104
这是我的剧本:

pipeline {
            agent none
            parameters {
                choice(name: 'app', choices: ['app1', 'app2'], description: 'Select the app.')
                choice(name: 'app_year', choices: ['', '2023', '2022', '2021'], description: 'Select the app year.')
                choice(name: 'deploy_env', choices: ['builds'], description: 'Default is builds.')
                choice(name: 'feature', choices: ['', 'feat1', 'feat2', 'feat3'], description: 'Select the feature, leave blank for all.')
            }
            triggers {
                cron('H/15 * * * *')
            }
            stages {
                stage ('App Healthcheck') {
                    agent { label 'test-deploy01' }
                    options {
                        timeout(time: 3600, unit: 'SECONDS')
                    }
                    steps {
                        sh "cd /opt/tools/ansible/${app}/scripts/; \
                        PYTHONPATH=shared python app_service_check.py -y $app_year -e $deploy_env -p $feature"
                    }
                }
            }
        }

我通过GUI粘贴了上面的内容。点击New item --〉Pipeline --〉pasted groovy script on page并保存。
第一次运行并得到上面的错误。但是这确实注册了作业中的参数选项,现在我可以运行作业而没有问题,并得到每个参数的提示,脚本工作没有问题。
我需要通过CasC加载许多作业,但是由于上述错误,Jenkins无法启动,因此我无法在GUI中重新运行作业以解决此问题。
我该如何解决这个问题?

krugob8w

krugob8w1#

是的@grzegorzgrzegorz这就是Jenkins的工作方式,这对我来说很奇怪。我通过设置一个拉管道作业来解决它。
例如:
在我的Jenkins.yaml文件中,我有:

jobs:
  - script: >
      folder('App')
  - file: /var/jenkins_home/pipelines/pull_stop_job

在我的pull_job脚本中,我有:

pipelineJob("App/Server Stop") {
  definition {
    cpsScm {
      scm {
        git {
          remote {
            url("https://gitlab.domian.com/Repo/Common/tools/jenkins.git")
            credentials("<gitlab_token>")
          }
          branch('develop')
        }
      }
      scriptPath("jobs/*")
    }
  }
  triggers {
    scm('H/15 * * * *')
  }
}

然后我可以使用我最初发布的脚本:

pipeline {
        agent none
        parameters {
            choice(name: 'app', choices: ['app1', 'app2'], description: 'Select the app.')
            choice(name: 'app_year', choices: ['', '2023', '2022', '2021'], description: 'Select the app year.')
            choice(name: 'deploy_env', choices: ['builds'], description: 'Default is builds.')
            choice(name: 'feature', choices: ['', 'feat1', 'feat2', 'feat3'], description: 'Select the feature, leave blank for all.')
        }
        triggers {
            cron('H/15 * * * *')
        }
        stages {
            stage ('App Healthcheck') {
                agent { label 'test-deploy01' }
                options {
                    timeout(time: 3600, unit: 'SECONDS')
                }
                steps {
                    sh "cd /opt/tools/ansible/${app}/scripts/; \
                    PYTHONPATH=shared python app_service_check.py -y $app_year -e $deploy_env -p $feature"
                }
            }
        }
    }

如前所述,作业第一次会失败,但在此之后,作业会正确提示参数并可以使用。不完美,但它可以工作。

相关问题