Jenkins管道在文件上传时工作正常,在文件未上传时出错

hwamh0ep  于 2023-10-17  发布在  Jenkins
关注(0)|答案(1)|浏览(180)

插件[Stashed File Parameter][1]的名称为uploadfile [文件参数|Jenkins plugin] https://plugins.jenkins.io/file-parameters/
我使用这个插件来上传文件使用Jenkins管道。
当我上传任何文件时,管道工作正常/成功;但是当没有上传文件时失败并出错。
我的Jenkins声明性管道如下所示。

pipeline {

    agent {

        node {

            label 'Ansible_host'

        }

    }

    stages {

        stage('Precheck') {

            steps {

                unstash 'uploadfile'

                script{

                    if ( uploadfile_FILENAME != "" )

                    {

                        sh "mv uploadfile '$uploadfile_FILENAME'"

                    }

                if ( uploadfile_FILENAME != "" )

                {

                    echo "File is uploaded from localhost"  

                }

                else

                {

                    echo "File is uploaded from $source_host"

                }

                }

                sh "echo Im from Jenkins>/tmp/jenkinsmoht.txt"
             

            }

        }

    }

}

未上传文件时出错:

[Pipeline] End of Pipeline

Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 5243a730-ce5f-4f9e-af0b-9e3a99f84ef1

groovy.lang.MissingPropertyException: No such property: uploadfile_FILENAME for class: groovy.lang.Binding

                       at groovy.lang.Binding.getVariable(Binding.java:63)

                       at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:285)

                       at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:375)

                       at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:379)

                       at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355)

                       at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355)

                       at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355)

                       at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)

                       at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)

                       at WorkflowScript.run(WorkflowScript:15)

                       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.GeneratedMethodAccessor261.invoke(Unknown Source)

                       at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

                       at java.base/java.lang.reflect.Method.invoke(Method.java:568)

                       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.SandboxContinuable.access$001(SandboxContinuable.java:18)

                       at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)

                       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:539)

                       at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)

                       at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)

                       at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)

                       at java.base/java.lang.Thread.run(Thread.java:833)

Finished: FAILURE

在Jenkins的早期版本中,没有看到此错误。
你能建议我如何才能避免这个错误时,没有文件上传使用插件?

0tdrvxhp

0tdrvxhp1#

在阅读插件文档后,我了解到uploadfile是在作业配置页面中定义的stashedFile类型的构建参数,它返回原始文件名作为环境变量uploadfile_FILENAME
由于当有文件要上传时,脚本已经工作,因此可以将if条件重写为:

if (env.uploadfile_FILENAME) {
    sh "mv uploadfile '$uploadfile_FILENAME'"
    echo "File is uploaded from localhost" 
} else {
    echo "File is uploaded from $source_host"
}

在这种情况下,当没有文件要上传时,env.uploadfile_FILENAME的值将是null或空字符串,if条件将返回false,而不会抛出No such property错误。

相关问题